Skip to content

Commit e88242c

Browse files
Copilotswissspidy
andcommitted
Support table-qualified column names in --skip-columns and --include-columns
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 776542c commit e88242c

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,13 @@ class Search_Replace_Command extends WP_CLI_Command {
184184
*
185185
* [--skip-columns=<columns>]
186186
* : Do not perform the replacement on specific columns. Use commas to
187-
* specify multiple columns.
187+
* specify multiple columns. Table-qualified column names ("table.column")
188+
* are supported to apply the skip to a specific table only.
188189
*
189190
* [--include-columns=<columns>]
190191
* : Perform the replacement on specific columns. Use commas to
191-
* specify multiple columns.
192+
* specify multiple columns. Table-qualified column names ("table.column")
193+
* are supported to apply the inclusion to a specific table only.
192194
*
193195
* [--precise]
194196
* : Force the use of PHP (instead of SQL) for all columns. By default, the command
@@ -510,11 +512,11 @@ public function __invoke( $args, $assoc_args ) {
510512
}
511513

512514
foreach ( $columns as $col ) {
513-
if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) ) {
515+
if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) && ! in_array( $table . '.' . $col, $this->include_columns, true ) ) {
514516
continue;
515517
}
516518

517-
if ( in_array( $col, $this->skip_columns, true ) ) {
519+
if ( in_array( $col, $this->skip_columns, true ) || in_array( $table . '.' . $col, $this->skip_columns, true ) ) {
518520
continue;
519521
}
520522

@@ -613,7 +615,11 @@ private function php_export_table( $table, $old, $new ) {
613615
$row_fields = array();
614616
foreach ( $all_columns as $col ) {
615617
$value = $row->$col;
616-
if ( $value && ! in_array( $col, $primary_keys, true ) && ! in_array( $col, $this->skip_columns, true ) ) {
618+
if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) && ! in_array( $table . '.' . $col, $this->include_columns, true ) ) {
619+
$row_fields[ $col ] = $value;
620+
continue;
621+
}
622+
if ( $value && ! in_array( $col, $primary_keys, true ) && ! in_array( $col, $this->skip_columns, true ) && ! in_array( $table . '.' . $col, $this->skip_columns, true ) ) {
617623
$new_value = $replacer->run( $value );
618624
if ( $new_value !== $value ) {
619625
++$col_counts[ $col ];

0 commit comments

Comments
 (0)