Skip to content

Commit 25a580a

Browse files
Merge pull request #48 from marksabbath/#31
Add `--skip-tables=<tables>` argument to exclude tables when performing search-replace
2 parents 8dd8cd2 + 282570e commit 25a580a

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
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: 6 additions & 0 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
"""

src/Search_Replace_Command.php

Lines changed: 16 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,16 @@ 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+
279+
// Removes from $tables tables provided by skip-tables argument
280+
$tables = array_diff( $tables, $this->skip_tables );
281+
272282
foreach ( $tables as $table ) {
273283

284+
if ( in_array( $table, $this->skip_tables ) ) {
285+
continue;
286+
}
287+
274288
$table_sql = self::esc_sql_ident( $table );
275289

276290
if ( $this->export_handle ) {

0 commit comments

Comments
 (0)