Skip to content

Commit 281a49c

Browse files
CopilotswissspidyCopilot
authored
Support table-qualified column names in --skip-columns and --include-columns (#214)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 940b2d4 commit 281a49c

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

features/search-replace.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ Feature: Do global search/replace
4141
| Table | Column | Replacements | Type |
4242
| wp_posts | post_content | 0 | SQL |
4343

44+
When I run `wp search-replace foo bar --skip-columns=wp_posts.guid`
45+
Then STDOUT should not contain:
46+
"""
47+
guid
48+
"""
49+
50+
When I run `wp search-replace foo bar --include-columns=wp_posts.post_content`
51+
Then STDOUT should be a table containing rows:
52+
| Table | Column | Replacements | Type |
53+
| wp_posts | post_content | 0 | SQL |
54+
4455
@require-mysql
4556
Scenario: Multisite search/replace
4657
Given a WP multisite install

src/Search_Replace_Command.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,13 @@ class Search_Replace_Command extends WP_CLI_Command {
192192
*
193193
* [--skip-columns=<columns>]
194194
* : Do not perform the replacement on specific columns. Use commas to
195-
* specify multiple columns.
195+
* specify multiple columns. Table-qualified column names ("table.column")
196+
* are supported to apply the skip to a specific table only.
196197
*
197198
* [--include-columns=<columns>]
198199
* : Perform the replacement on specific columns. Use commas to
199-
* specify multiple columns.
200+
* specify multiple columns. Table-qualified column names ("table.column")
201+
* are supported to apply the inclusion to a specific table only.
200202
*
201203
* [--precise]
202204
* : Force the use of PHP (instead of SQL) for all columns. By default, the command
@@ -530,11 +532,11 @@ public function __invoke( $args, $assoc_args ) {
530532
}
531533

532534
foreach ( $columns as $col ) {
533-
if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) ) {
535+
if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) && ! in_array( $table . '.' . $col, $this->include_columns, true ) ) {
534536
continue;
535537
}
536538

537-
if ( in_array( $col, $this->skip_columns, true ) ) {
539+
if ( in_array( $col, $this->skip_columns, true ) || in_array( $table . '.' . $col, $this->skip_columns, true ) ) {
538540
continue;
539541
}
540542

@@ -633,7 +635,12 @@ private function php_export_table( $table, $old, $new ) {
633635
$row_fields = array();
634636
foreach ( $all_columns as $col ) {
635637
$value = $row->$col;
636-
if ( $value && ! in_array( $col, $primary_keys, true ) && ! in_array( $col, $this->skip_columns, true ) ) {
638+
if ( $value
639+
&& ! in_array( $col, $primary_keys, true )
640+
&& ! in_array( $col, $this->skip_columns, true )
641+
&& ! in_array( $table . '.' . $col, $this->skip_columns, true )
642+
&& ( empty( $this->include_columns ) || in_array( $col, $this->include_columns, true ) || in_array( $table . '.' . $col, $this->include_columns, true ) )
643+
) {
637644
$new_value = $replacer->run( $value );
638645
if ( $new_value !== $value ) {
639646
++$col_counts[ $col ];

0 commit comments

Comments
 (0)