diff --git a/README.md b/README.md index 899e6a34..1c5af334 100644 --- a/README.md +++ b/README.md @@ -438,7 +438,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] [--defaults] +wp db export [] [--dbuser=] [--dbpass=] [--=] [--tables=] [--exclude_tables=] [--exclude_tables_data=] [--include-tablespaces] [--porcelain] [--defaults] ~~~ **Alias:** `dump` @@ -467,6 +467,10 @@ 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. + Requires a dump client that supports --ignore-table-data (mariadb-dump from MariaDB 10.1.45, 10.2.32, 10.3.23, 10.4.13, 10.5.3 or later); MySQL's mysqldump does not. Cannot be combined with --tables. + [--include-tablespaces] Skips adding the default --no-tablespaces option to mysqldump. @@ -514,6 +518,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 91061357..dad27f15 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -70,6 +70,87 @@ Feature: Export a WordPress database PRAGMA foreign_keys=OFF """ + # Support depends on the dump client, not the server (e.g. mariadb-dump works against a MySQL server), + # so fake a dump client without --ignore-table-data, like MySQL's mysqldump. + @skip-sqlite + Scenario: Exclude data of certain tables is not supported by the dump client + Given a WP install + And a fake-bin/mysqldump file: + """ + #!/bin/sh + case "$1" in + --version) echo "mysqldump Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)" ;; + --help) echo " --ignore-table=name Do not dump the specified table." ;; + esac + exit 0 + """ + And a fake-bin/mariadb-dump file: + """ + #!/bin/sh + case "$1" in + --version) echo "mysqldump Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)" ;; + --help) echo " --ignore-table=name Do not dump the specified table." ;; + esac + exit 0 + """ + + When I run `chmod +x fake-bin/mysqldump fake-bin/mariadb-dump` + And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db export wp_cli_test.sql --exclude_tables_data=wp_users` + Then the return code should be 1 + And STDERR should contain: + """ + Error: The --exclude_tables_data option requires a dump client that supports --ignore-table-data + """ + And the wp_cli_test.sql file should not exist + + # Only the MariaDB dump client supports --ignore-table-data. + @require-mariadb + 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,wp_options --porcelain` + Then the wp_cli_test.sql file should exist + And the wp_cli_test.sql file should contain: + """ + CREATE TABLE `wp_users` + """ + And the wp_cli_test.sql file should contain: + """ + CREATE TABLE `wp_options` + """ + And the wp_cli_test.sql file should contain: + """ + INSERT INTO `wp_posts` + """ + And the wp_cli_test.sql file should not contain: + """ + INSERT INTO `wp_users` + """ + And the wp_cli_test.sql file should not contain: + """ + INSERT INTO `wp_options` + """ + + @skip-sqlite + Scenario: Exclude data of certain tables cannot be combined with --tables + Given a WP install + + When I try `wp db export wp_cli_test.sql --tables=wp_users,wp_posts --exclude_tables_data=wp_users` + Then the return code should be 1 + And STDERR should contain: + """ + Error: The --exclude_tables_data option cannot be combined with --tables. + """ + + @require-sqlite + Scenario: Exclude data of certain tables when exporting a SQLite database + Given a WP install + + When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain` + Then the contents of the wp_cli_test.sql file should match /CREATE TABLE [`"]?wp_users[`"]?/ + And the contents of the wp_cli_test.sql file should match /INSERT INTO [`"]?wp_posts[`"]?/ + And the contents of the wp_cli_test.sql file should not match /INSERT INTO [`"]?wp_users[`"]?/ + @skip-sqlite Scenario: Export database to STDOUT Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index e283ae6c..f04f18b3 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -684,6 +684,10 @@ 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. + * Requires a dump client that supports --ignore-table-data (mariadb-dump from MariaDB 10.1.45, 10.2.32, 10.3.23, 10.4.13, 10.5.3 or later); MySQL's mysqldump does not. Cannot be combined with --tables. + * * [--include-tablespaces] * : Skips adding the default --no-tablespaces option to mysqldump. * @@ -731,6 +735,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) @@ -814,6 +822,8 @@ public function export( $args, $assoc_args ) { $command_esc_args = [ DB_NAME ]; + $has_tables = isset( $assoc_args['tables'] ); + if ( isset( $assoc_args['tables'] ) ) { $tables = explode( ',', trim( $assoc_args['tables'], ',' ) ); unset( $assoc_args['tables'] ); @@ -835,6 +845,31 @@ public function export( $args, $assoc_args ) { } } + $exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' ); + unset( $assoc_args['exclude_tables_data'] ); + if ( is_string( $exclude_tables_data ) && '' !== trim( $exclude_tables_data, ', ' ) ) { + // The dump client ignores --ignore-table-data for tables named with --tables. + if ( $has_tables ) { + WP_CLI::error( 'The --exclude_tables_data option cannot be combined with --tables.' ); + } + + // --ignore-table-data is a dump client option, so check the client rather than the server. + if ( ! $this->command_supports_option( $mysqldump_binary, 'ignore-table-data' ) ) { + WP_CLI::error( sprintf( 'The --exclude_tables_data option requires a dump client that supports --ignore-table-data (mariadb-dump from MariaDB 10.1.45, 10.2.32, 10.3.23, 10.4.13, 10.5.3 or later). "%s" does not.', $mysqldump_binary ) ); + } + + $tables = explode( ',', trim( $exclude_tables_data, ',' ) ); + foreach ( $tables as $table ) { + $table = trim( $table ); + if ( '' === $table ) { + continue; + } + $command .= ' --ignore-table-data'; + $command .= ' %s'; + $command_esc_args[] = DB_NAME . '.' . $table; + } + } + $escaped_command = Utils\esc_cmd( $command, ...$command_esc_args ); // Remove parameters not needed for SQL run. diff --git a/src/DB_Command_SQLite.php b/src/DB_Command_SQLite.php index 978c2190..37bd6ac3 100644 --- a/src/DB_Command_SQLite.php +++ b/src/DB_Command_SQLite.php @@ -397,6 +397,18 @@ protected function sqlite_export( $file, $assoc_args ) { $drop_statements[] = sprintf( 'DROP TABLE %s;', $escaped_identifier ); } + // Keep the structure but not the rows of these tables. This only affects the temporary copy. + if ( isset( $assoc_args['exclude_tables_data'] ) && is_string( $assoc_args['exclude_tables_data'] ) ) { + foreach ( explode( ',', $assoc_args['exclude_tables_data'] ) as $table ) { + $table = trim( $table ); + if ( '' === $table || in_array( $table, $exclude_tables, true ) ) { + continue; + } + $drop_statements[] = sprintf( 'DELETE FROM %s;', '"' . str_replace( '"', '""', $table ) . '"' ); + } + } + unset( $assoc_args['exclude_tables_data'] ); + $init_file = tempnam( sys_get_temp_dir(), 'export_init' ); if ( false === $init_file ) { if ( file_exists( $temp_db ) ) {