Skip to content

Commit 6f96719

Browse files
Merge pull request #20 from Nikschavan/skip-tables
Add option to skip tables when exporting a database
2 parents 6041b7a + 79ab26d commit 6f96719

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

features/db-export.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ Feature: Export a WordPress database
1919
wp_cli_test.sql
2020
"""
2121
And the wp_cli_test.sql file should exist
22+
23+
Scenario: Exclude tables when exporting the dtabase
24+
Given a WP install
25+
26+
When I run `wp db export wp_cli_test.sql --exclude_tables=wp_users --porcelain`
27+
Then the wp_cli_test.sql file should exist
28+
And the wp_cli_test.sql file should not contain:
29+
"""
30+
wp_users
31+
"""
32+
And the wp_cli_test.sql file should contain:
33+
"""
34+
wp_options
35+
"""

src/DB_Command.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ public function query( $args, $assoc_args ) {
264264
*
265265
* [--tables=<tables>]
266266
* : The comma separated list of specific tables to export. Excluding this parameter will export all tables in the database.
267+
*
268+
* [--exclude_tables=<tables>]
269+
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
267270
*
268271
* [--porcelain]
269272
* : Output filename for the exported database.
@@ -285,6 +288,18 @@ public function query( $args, $assoc_args ) {
285288
* # Export all tables matching prefix
286289
* $ wp db export --tables=$(wp db tables --all-tables-with-prefix --format=csv)
287290
* Success: Exported to 'wordpress_dbase.sql'.
291+
*
292+
* # Skip certain tables from the exported database
293+
* $ wp db export --exclude_tables=wp_options,wp_users
294+
* Success: Exported to 'wordpress_dbase.sql'.
295+
*
296+
* # Skip all tables matching a wildcard from the exported database
297+
* $ wp db export --exclude_tables=$(wp db tables 'wp_user*' --format=csv)
298+
* Success: Exported to 'wordpress_dbase.sql'.
299+
*
300+
* # Skip all tables matching prefix from the exported database
301+
* $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
302+
* Success: Exported to 'wordpress_dbase.sql'.
288303
*
289304
* @alias dump
290305
*/
@@ -320,6 +335,17 @@ public function export( $args, $assoc_args ) {
320335
}
321336
}
322337

338+
$exclude_tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'exclude_tables' );
339+
if ( isset( $exclude_tables ) ) {
340+
$tables = explode( ',', trim( $assoc_args['exclude_tables'], ',' ) );
341+
unset( $assoc_args['exclude_tables'] );
342+
foreach ( $tables as $table ) {
343+
$command .= ' --ignore-table';
344+
$command .= ' %s';
345+
$command_esc_args[] = trim( DB_NAME . '.' . $table );
346+
}
347+
}
348+
323349
$escaped_command = call_user_func_array( '\WP_CLI\Utils\esc_cmd', array_merge( array( $command ), $command_esc_args ) );
324350

325351
// Remove parameters not needed for SQL run.

0 commit comments

Comments
 (0)