diff --git a/README.md b/README.md index 547fa52a..d7f5a4e3 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ To confirm the ID for the site you want to query, you can use the `wp site list` Exports the database to a file or to STDOUT. ~~~ -wp db export [] [--dbuser=] [--dbpass=] [--=] [--tables=] [--exclude_tables=] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults] +wp db export [] [--dbuser=] [--dbpass=] [--=] [--tables=] [--exclude_tables=] [--exclude_tables_data=] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults] ~~~ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and @@ -456,6 +456,9 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and [--exclude_tables=] The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database. + [--exclude_tables_data=] + The comma separated list of specific tables for which only the structure will be exported. Excluding this parameter will export data for all tables in the export. + [--include-tablespaces] Skips adding the default --no-tablespaces option to mysqldump. @@ -506,6 +509,10 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv) Success: Exported to 'wordpress_dbase-db72bb5.sql'. + # Skip data of certain tables from the exported database + $ wp db export --exclude_tables_data=wp_actionscheduler_logs + Success: Exported to 'wordpress_dbase-db72bb5.sql'. + # Export database to STDOUT. $ wp db export - -- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64) diff --git a/features/db-export.feature b/features/db-export.feature index 370ae1df..dea9219d 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -34,6 +34,20 @@ Feature: Export a WordPress database wp_options """ + Scenario: Exclude data of certain tables when exporting the database + Given a WP install + + When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain` + Then the wp_cli_test.sql file should exist + And the wp_cli_test.sql file should contain: + """ + wp_users + """ + And the wp_cli_test.sql file should not contain: + """ + INSERT INTO `wp_users` + """ + Scenario: Export database to STDOUT Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index 06781cee..3d37441b 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -567,6 +567,9 @@ public function query( $args, $assoc_args ) { * [--exclude_tables=] * : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database. * + * [--exclude_tables_data=] + * : The comma separated list of specific tables for which only the structure will be exported. Excluding this parameter will export data for all tables in the export. + * * [--include-tablespaces] * : Skips adding the default --no-tablespaces option to mysqldump. * @@ -617,6 +620,10 @@ public function query( $args, $assoc_args ) { * $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv) * Success: Exported to 'wordpress_dbase-db72bb5.sql'. * + * # Skip data of certain tables from the exported database + * $ wp db export --exclude_tables_data=wp_actionscheduler_logs + * Success: Exported to 'wordpress_dbase-db72bb5.sql'. + * * # Export database to STDOUT. * $ wp db export - * -- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64) @@ -708,6 +715,17 @@ public function export( $args, $assoc_args ) { } } + $exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data' ); + if ( isset( $exclude_tables_data ) ) { + $tables = explode( ',', trim( $assoc_args['exclude_tables_data'], ',' ) ); + unset( $assoc_args['exclude_tables_data'] ); + foreach ( $tables as $table ) { + $command .= ' --ignore-table-data'; + $command .= ' %s'; + $command_esc_args[] = trim( DB_NAME . '.' . $table ); + } + } + $escaped_command = Utils\esc_cmd( $command, ...$command_esc_args ); // Remove parameters not needed for SQL run.