From b61d0d9f7179e3d2de9e024d481d66836ee91cad Mon Sep 17 00:00:00 2001 From: Oleh Astappiev Date: Fri, 10 Oct 2025 11:05:22 +0200 Subject: [PATCH 1/8] feat: add support for excluding table data during export Introduces the `--exclude_tables_data` option to the `wp db export` command, allowing users to export only the structure of specified tables while excluding their data. This enhances flexibility in database management and export processes. --- README.md | 9 ++++++++- features/db-export.feature | 10 ++++++++++ src/DB_Command.php | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 547fa52a5..d7f5a4e34 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 370ae1df8..9bdbb7a85 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -34,6 +34,16 @@ 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_actionscheduler_logs --porcelain` + Then the wp_cli_test.sql file should exist + And the wp_cli_test.sql file should contain: + """ + wp_actionscheduler_logs + """ + Scenario: Export database to STDOUT Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index 06781ceed..3d37441bb 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. From 7bba65109b0a063f3aa877891607ac6bd40a4b8f Mon Sep 17 00:00:00 2001 From: Oleh Astappiev Date: Fri, 10 Oct 2025 11:47:45 +0200 Subject: [PATCH 2/8] test: update test for --exclude_tables_data --- features/db-export.feature | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/features/db-export.feature b/features/db-export.feature index 9bdbb7a85..dea9219d0 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -37,11 +37,15 @@ Feature: Export a WordPress database 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_actionscheduler_logs --porcelain` + 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_actionscheduler_logs + wp_users + """ + And the wp_cli_test.sql file should not contain: + """ + INSERT INTO `wp_users` """ Scenario: Export database to STDOUT From 3b6a2f389a9cd00bc38031ee34fbf10035c8e03a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 22 Jan 2026 10:41:49 +0100 Subject: [PATCH 3/8] MariaDB only --- features/db-export.feature | 2 ++ src/DB_Command.php | 1 + 2 files changed, 3 insertions(+) diff --git a/features/db-export.feature b/features/db-export.feature index dea9219d0..9c9edef3e 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -34,6 +34,8 @@ Feature: Export a WordPress database wp_options """ + # Only MariaDB currently supports this feature. + @require-mariadb Scenario: Exclude data of certain tables when exporting the database Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index 3d37441bb..dba1f1755 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -569,6 +569,7 @@ public function query( $args, $assoc_args ) { * * [--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. + * Note: currently only supported by MariaDB. * * [--include-tablespaces] * : Skips adding the default --no-tablespaces option to mysqldump. From c79679e3f87c4cf2412b5bf1dbcdfbe1d19b8f06 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 19 Mar 2026 18:17:36 +0100 Subject: [PATCH 4/8] Update src/DB_Command.php --- src/DB_Command.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/DB_Command.php b/src/DB_Command.php index 0b0a747fb..569ad0fa8 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -737,12 +737,22 @@ 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 ); + if ( is_string( $exclude_tables_data ) && '' !== trim( $exclude_tables_data ) ) { + if ( 'mariadb' !== Utils\get_db_type() ) { + WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' ); + } + + $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; + } } } From becf8d3073fa0f8c8c77c3ec5dd4c5ac82bf985b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2026 10:26:47 +0200 Subject: [PATCH 5/8] Add test, simplify code --- features/db-export.feature | 11 +++++++++++ src/DB_Command.php | 29 +++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/features/db-export.feature b/features/db-export.feature index 71d9da0a6..63c121026 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -70,6 +70,17 @@ Feature: Export a WordPress database PRAGMA foreign_keys=OFF """ + @skip-mariadb @skip-sqlite + Scenario: Exclude data of certain tables is not supported by MySQL + Given a WP install + + When I try `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 is only supported by MariaDB. + """ + # Only MariaDB currently supports this feature. @require-mariadb Scenario: Exclude data of certain tables when exporting the database diff --git a/src/DB_Command.php b/src/DB_Command.php index 0add51571..6c3b50662 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -812,24 +812,21 @@ public function export( $args, $assoc_args ) { } } - $exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data' ); - if ( isset( $exclude_tables_data ) ) { - unset( $assoc_args['exclude_tables_data'] ); - if ( is_string( $exclude_tables_data ) && '' !== trim( $exclude_tables_data ) ) { - if ( 'mariadb' !== Utils\get_db_type() ) { - WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' ); - } + $exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' ); + if ( ! empty( $exclude_tables_data ) ) { + if ( 'mariadb' !== Utils\get_db_type() ) { + WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' ); + } - $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; + $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; } } From 5a5b28d9895e95e6b896da00261ff0a79a973a25 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2026 11:07:55 +0200 Subject: [PATCH 6/8] Add back `unset` --- src/DB_Command.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DB_Command.php b/src/DB_Command.php index 6c3b50662..9dc15118b 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -814,6 +814,8 @@ public function export( $args, $assoc_args ) { $exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' ); if ( ! empty( $exclude_tables_data ) ) { + unset( $assoc_args['exclude_tables_data'] ); + if ( 'mariadb' !== Utils\get_db_type() ) { WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' ); } From 5ef282bd50013d78e53c58387d4f0865f0667554 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 13:21:58 +0000 Subject: [PATCH 7/8] Check the dump client for --ignore-table-data, reject --tables, support SQLite - Detect support with command_supports_option() on the dump binary instead of get_db_type(), since --ignore-table-data is a dump client option. - Error when combined with --tables, which mariadb-dump silently ignores. - Empty the listed tables in the temporary SQLite copy before .dump. - Always unset the option, and only process string values. - Document exact MariaDB versions; extend tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- README.md | 2 +- features/db-export.feature | 41 ++++++++++++++++++++++++++++++++++---- src/DB_Command.php | 17 +++++++++++----- src/DB_Command_SQLite.php | 12 +++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 24caf6b7f..1c5af3348 100644 --- a/README.md +++ b/README.md @@ -469,7 +469,7 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and [--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. - Note: currently only supported by MariaDB. + 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. diff --git a/features/db-export.feature b/features/db-export.feature index 5f208b162..3b3bffebf 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -78,24 +78,57 @@ Feature: Export a WordPress database Then the return code should be 1 And STDERR should contain: """ - Error: The --exclude_tables_data option is only supported by MariaDB. + 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 MariaDB currently supports this feature. + # 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 --porcelain` + 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: """ - wp_users + 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 diff --git a/src/DB_Command.php b/src/DB_Command.php index 8e5cc251a..79c7ac86b 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -686,7 +686,7 @@ public function query( $args, $assoc_args ) { * * [--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. - * Note: currently only supported by MariaDB. + * 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. @@ -822,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'] ); @@ -844,11 +846,16 @@ public function export( $args, $assoc_args ) { } $exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' ); - if ( ! empty( $exclude_tables_data ) ) { - unset( $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.' ); + } - if ( 'mariadb' !== Utils\get_db_type() ) { - WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' ); + // --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.4.13, 10.5.3 or later). "%s" does not.', $mysqldump_binary ) ); } $tables = explode( ',', trim( $exclude_tables_data, ',' ) ); diff --git a/src/DB_Command_SQLite.php b/src/DB_Command_SQLite.php index 978c21903..37bd6ac33 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 ) ) { From 21802b0b67c0710f3f856d258206e90a399f3b0f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 15:13:07 +0000 Subject: [PATCH 8/8] Test the unsupported dump client case with a fake dump client Support for --ignore-table-data depends on the dump client, not the server. The MySQL CI jobs run with mariadb-dump against a MySQL server, so the export succeeded and the @skip-mariadb scenario failed. Fake a dump client without the option instead, so the error path is tested on every server. Also list the same MariaDB versions in the error message as in the docs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- features/db-export.feature | 27 ++++++++++++++++++++++++--- src/DB_Command.php | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/features/db-export.feature b/features/db-export.feature index 3b3bffebf..dad27f151 100644 --- a/features/db-export.feature +++ b/features/db-export.feature @@ -70,11 +70,32 @@ Feature: Export a WordPress database PRAGMA foreign_keys=OFF """ - @skip-mariadb @skip-sqlite - Scenario: Exclude data of certain tables is not supported by MySQL + # 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 try `wp db export wp_cli_test.sql --exclude_tables_data=wp_users` + 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: """ diff --git a/src/DB_Command.php b/src/DB_Command.php index 79c7ac86b..2e81b76ca 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -855,7 +855,7 @@ public function export( $args, $assoc_args ) { // --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.4.13, 10.5.3 or later). "%s" does not.', $mysqldump_binary ) ); + 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, ',' ) );