Skip to content

Commit f45d5ad

Browse files
Merge branch 'master' into issue/doc-verbs
2 parents c08dc31 + d358907 commit f45d5ad

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contr
1010
## Using
1111

1212
~~~
13-
wp search-replace <old> <new> [<table>...] [--dry-run] [--network] [--all-tables-with-prefix] [--all-tables] [--export[=<file>]] [--export_insert_size=<rows>] [--skip-columns=<columns>] [--include-columns=<columns>] [--precise] [--recurse-objects] [--verbose] [--regex] [--regex-flags=<regex-flags>] [--regex-delimiter=<regex-delimiter>] [--format=<format>] [--report] [--report-changed-only] [--log[=<file>]] [--before_context=<num>] [--after_context=<num>]
13+
wp search-replace <old> <new> [<table>...] [--dry-run] [--network] [--all-tables-with-prefix] [--all-tables] [--export[=<file>]] [--export_insert_size=<rows>] [--skip-tables=<tables>] [--skip-columns=<columns>] [--include-columns=<columns>] [--precise] [--recurse-objects] [--verbose] [--regex] [--regex-flags=<regex-flags>] [--regex-delimiter=<regex-delimiter>] [--format=<format>] [--report] [--report-changed-only] [--log[=<file>]] [--before_context=<num>] [--after_context=<num>]
1414
~~~
1515

1616
Searches through all rows in a selection of tables and replaces
@@ -61,6 +61,10 @@ change primary key values.
6161
You might want to change this depending on your database configuration
6262
(e.g. if you need to do fewer queries). Default: 50
6363

64+
[--skip-tables=<tables>]
65+
Do not perform the replacement on specific tables. Use commas to
66+
specify multiple tables.
67+
6468
[--skip-columns=<columns>]
6569
Do not perform the replacement on specific columns. Use commas to
6670
specify multiple columns.
@@ -135,9 +139,9 @@ change primary key values.
135139
# Bash script: Search/replace production to development url (multisite compatible)
136140
#!/bin/bash
137141
if $(wp --url=http://example.com core is-installed --network); then
138-
wp search-replace --url=http://example.com 'http://example.com' 'http://example.dev' --recurse-objects --network --skip-columns=guid
142+
wp search-replace --url=http://example.com 'http://example.com' 'http://example.dev' --recurse-objects --network --skip-columns=guid --skip-tables=wp_users
139143
else
140-
wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid
144+
wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid --skip-tables=wp_users
141145
fi
142146

143147
## Installing

features/search-replace-export.feature

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Feature: Search / replace with file export
2020
http://example.com
2121
"""
2222

23+
When I run `wp search-replace example.com example.net --skip-tables=wp_options --export`
24+
Then STDOUT should not contain:
25+
"""
26+
INSERT INTO `wp_options`
27+
"""
28+
2329
When I run `wp search-replace example.com example.net --skip-columns=option_value --export`
2430
Then STDOUT should contain:
2531
"""
@@ -33,7 +39,7 @@ Feature: Search / replace with file export
3339
('1', 'siteurl', 'http://example.com', 'yes');
3440
INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES
3541
"""
36-
42+
3743
When I run `wp search-replace foo bar --export | tail -n 1`
3844
Then STDOUT should not contain:
3945
"""

features/search-replace.feature

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Feature: Do global search/replace
99
guid
1010
"""
1111

12+
When I run `wp search-replace foo bar --skip-tables=wp_posts`
13+
Then STDOUT should not contain:
14+
"""
15+
wp_posts
16+
"""
17+
1218
When I run `wp search-replace foo bar --skip-columns=guid`
1319
Then STDOUT should not contain:
1420
"""
@@ -657,7 +663,7 @@ Feature: Do global search/replace
657663
When I run `wp post create --post_title='Title_baz__baz_' --post_content='Content_baz_12345678901234567890_baz_12345678901234567890' --porcelain`
658664
Then save STDOUT as {POST_ID}
659665

660-
When I run `wp search-replace '_baz_' '_' --dry-run --log --before_context=10 --after_context=10`
666+
When I run `wp search-replace '_baz_' '_' wp_posts --dry-run --log --before_context=10 --after_context=10`
661667
Then STDOUT should contain:
662668
"""
663669
Success: 2 replacements to be made.
@@ -681,7 +687,7 @@ Feature: Do global search/replace
681687
"""
682688
And STDERR should be empty
683689

684-
When I run `wp search-replace '_baz_' '' --dry-run --log=replace.log`
690+
When I run `wp search-replace '_baz_' '' wp_posts --dry-run --log=replace.log`
685691
Then STDOUT should contain:
686692
"""
687693
Success: 2 replacements to be made.
@@ -705,7 +711,7 @@ Feature: Do global search/replace
705711
And STDERR should be empty
706712

707713
# kana with diacritic and decomposed "a" + umlaut.
708-
When I run `wp search-replace '_baz_' '_バäz_' --log=- --before_context=10 --after_context=20`
714+
When I run `wp search-replace '_baz_' '_バäz_' wp_posts --log=- --before_context=10 --after_context=20`
709715
Then STDOUT should contain:
710716
"""
711717
Success: Made 2 replacements.
@@ -719,7 +725,7 @@ Feature: Do global search/replace
719725
And STDERR should be empty
720726

721727
# Testing UTF-8 context
722-
When I run `wp search-replace 'z_' 'zzzz_' --log --before_context=2 --after_context=1`
728+
When I run `wp search-replace 'z_' 'zzzz_' wp_posts --log --before_context=2 --after_context=1`
723729
Then STDOUT should contain:
724730
"""
725731
Success: Made 2 replacements.
@@ -810,7 +816,7 @@ Feature: Do global search/replace
810816
When I run `wp post create --post_title='Title_baz__boz_' --post_content='Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_' --porcelain`
811817
Then save STDOUT as {POST_ID}
812818

813-
When I run `wp search-replace '_b[aeiou]z_' '_bz_' --regex --dry-run --log --before_context=11 --after_context=11`
819+
When I run `wp search-replace '_b[aeiou]z_' '_bz_' wp_posts --regex --dry-run --log --before_context=11 --after_context=11`
814820
Then STDOUT should contain:
815821
"""
816822
Success: 2 replacements to be made.
@@ -834,7 +840,7 @@ Feature: Do global search/replace
834840
"""
835841
And STDERR should be empty
836842

837-
When I run `wp search-replace '_b([aeiou])z_' '_$1b\\1z_\0' --regex --log --before_context=11 --after_context=11`
843+
When I run `wp search-replace '_b([aeiou])z_' '_$1b\\1z_\0' wp_posts --regex --log --before_context=11 --after_context=11`
838844
Then STDOUT should contain:
839845
"""
840846
Success: Made 2 replacements.

src/Search_Replace_Command.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Search_Replace_Command extends WP_CLI_Command {
99
private $regex;
1010
private $regex_flags;
1111
private $regex_delimiter;
12+
private $skip_tables;
1213
private $skip_columns;
1314
private $include_columns;
1415
private $format;
@@ -74,6 +75,10 @@ class Search_Replace_Command extends WP_CLI_Command {
7475
* You might want to change this depending on your database configuration
7576
* (e.g. if you need to do fewer queries). Default: 50
7677
*
78+
* [--skip-tables=<tables>]
79+
* : Do not perform the replacement on specific tables. Use commas to
80+
* specify multiple tables.
81+
*
7782
* [--skip-columns=<columns>]
7883
* : Do not perform the replacement on specific columns. Use commas to
7984
* specify multiple columns.
@@ -148,9 +153,9 @@ class Search_Replace_Command extends WP_CLI_Command {
148153
* # Bash script: Search/replace production to development url (multisite compatible)
149154
* #!/bin/bash
150155
* if $(wp --url=http://example.com core is-installed --network); then
151-
* wp search-replace --url=http://example.com 'http://example.com' 'http://example.dev' --recurse-objects --network --skip-columns=guid
156+
* wp search-replace --url=http://example.com 'http://example.com' 'http://example.dev' --recurse-objects --network --skip-columns=guid --skip-tables=wp_users
152157
* else
153-
* wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid
158+
* wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid --skip-tables=wp_users
154159
* fi
155160
*/
156161
public function __invoke( $args, $assoc_args ) {
@@ -195,6 +200,7 @@ public function __invoke( $args, $assoc_args ) {
195200
}
196201

197202
$this->skip_columns = explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-columns' ) );
203+
$this->skip_tables = explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-tables' ) );
198204
$this->include_columns = array_filter( explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'include-columns' ) ) );
199205

200206
if ( $old === $new && ! $this->regex ) {
@@ -269,8 +275,13 @@ public function __invoke( $args, $assoc_args ) {
269275

270276
// Get table names based on leftover $args or supplied $assoc_args
271277
$tables = \WP_CLI\Utils\wp_get_table_names( $args, $assoc_args );
278+
272279
foreach ( $tables as $table ) {
273280

281+
if ( in_array( $table, $this->skip_tables ) ) {
282+
continue;
283+
}
284+
274285
$table_sql = self::esc_sql_ident( $table );
275286

276287
if ( $this->export_handle ) {

0 commit comments

Comments
 (0)