Skip to content

Commit fc33700

Browse files
committed
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 8cb57b6 commit fc33700

4 files changed

Lines changed: 62 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
469469

470470
[--exclude_tables_data=<tables>]
471471
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.
472-
Note: currently only supported by MariaDB.
472+
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.
473473

474474
[--include-tablespaces]
475475
Skips adding the default --no-tablespaces option to mysqldump.

‎features/db-export.feature‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,57 @@ Feature: Export a WordPress database
7878
Then the return code should be 1
7979
And STDERR should contain:
8080
"""
81-
Error: The --exclude_tables_data option is only supported by MariaDB.
81+
Error: The --exclude_tables_data option requires a dump client that supports --ignore-table-data
8282
"""
83+
And the wp_cli_test.sql file should not exist
8384

84-
# Only MariaDB currently supports this feature.
85+
# Only the MariaDB dump client supports --ignore-table-data.
8586
@require-mariadb
8687
Scenario: Exclude data of certain tables when exporting the database
8788
Given a WP install
8889

89-
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain`
90+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users,wp_options --porcelain`
9091
Then the wp_cli_test.sql file should exist
9192
And the wp_cli_test.sql file should contain:
9293
"""
93-
wp_users
94+
CREATE TABLE `wp_users`
95+
"""
96+
And the wp_cli_test.sql file should contain:
97+
"""
98+
CREATE TABLE `wp_options`
99+
"""
100+
And the wp_cli_test.sql file should contain:
101+
"""
102+
INSERT INTO `wp_posts`
94103
"""
95104
And the wp_cli_test.sql file should not contain:
96105
"""
97106
INSERT INTO `wp_users`
98107
"""
108+
And the wp_cli_test.sql file should not contain:
109+
"""
110+
INSERT INTO `wp_options`
111+
"""
112+
113+
@skip-sqlite
114+
Scenario: Exclude data of certain tables cannot be combined with --tables
115+
Given a WP install
116+
117+
When I try `wp db export wp_cli_test.sql --tables=wp_users,wp_posts --exclude_tables_data=wp_users`
118+
Then the return code should be 1
119+
And STDERR should contain:
120+
"""
121+
Error: The --exclude_tables_data option cannot be combined with --tables.
122+
"""
123+
124+
@require-sqlite
125+
Scenario: Exclude data of certain tables when exporting a SQLite database
126+
Given a WP install
127+
128+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain`
129+
Then the contents of the wp_cli_test.sql file should match /CREATE TABLE [`"]?wp_users[`"]?/
130+
And the contents of the wp_cli_test.sql file should match /INSERT INTO [`"]?wp_posts[`"]?/
131+
And the contents of the wp_cli_test.sql file should not match /INSERT INTO [`"]?wp_users[`"]?/
99132

100133
@skip-sqlite
101134
Scenario: Export database to STDOUT

‎src/DB_Command.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ public function query( $args, $assoc_args ) {
686686
*
687687
* [--exclude_tables_data=<tables>]
688688
* : 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.
689-
* Note: currently only supported by MariaDB.
689+
* 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.
690690
*
691691
* [--include-tablespaces]
692692
* : Skips adding the default --no-tablespaces option to mysqldump.
@@ -822,6 +822,8 @@ public function export( $args, $assoc_args ) {
822822

823823
$command_esc_args = [ DB_NAME ];
824824

825+
$has_tables = isset( $assoc_args['tables'] );
826+
825827
if ( isset( $assoc_args['tables'] ) ) {
826828
$tables = explode( ',', trim( $assoc_args['tables'], ',' ) );
827829
unset( $assoc_args['tables'] );
@@ -844,11 +846,16 @@ public function export( $args, $assoc_args ) {
844846
}
845847

846848
$exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' );
847-
if ( ! empty( $exclude_tables_data ) ) {
848-
unset( $assoc_args['exclude_tables_data'] );
849+
unset( $assoc_args['exclude_tables_data'] );
850+
if ( is_string( $exclude_tables_data ) && '' !== trim( $exclude_tables_data, ', ' ) ) {
851+
// The dump client ignores --ignore-table-data for tables named with --tables.
852+
if ( $has_tables ) {
853+
WP_CLI::error( 'The --exclude_tables_data option cannot be combined with --tables.' );
854+
}
849855

850-
if ( 'mariadb' !== Utils\get_db_type() ) {
851-
WP_CLI::error( 'The --exclude_tables_data option is only supported by MariaDB.' );
856+
// --ignore-table-data is a dump client option, so check the client rather than the server.
857+
if ( ! $this->command_supports_option( $mysqldump_binary, 'ignore-table-data' ) ) {
858+
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 ) );
852859
}
853860

854861
$tables = explode( ',', trim( $exclude_tables_data, ',' ) );

‎src/DB_Command_SQLite.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ protected function sqlite_export( $file, $assoc_args ) {
397397
$drop_statements[] = sprintf( 'DROP TABLE %s;', $escaped_identifier );
398398
}
399399

400+
// Keep the structure but not the rows of these tables. This only affects the temporary copy.
401+
if ( isset( $assoc_args['exclude_tables_data'] ) && is_string( $assoc_args['exclude_tables_data'] ) ) {
402+
foreach ( explode( ',', $assoc_args['exclude_tables_data'] ) as $table ) {
403+
$table = trim( $table );
404+
if ( '' === $table || in_array( $table, $exclude_tables, true ) ) {
405+
continue;
406+
}
407+
$drop_statements[] = sprintf( 'DELETE FROM %s;', '"' . str_replace( '"', '""', $table ) . '"' );
408+
}
409+
}
410+
unset( $assoc_args['exclude_tables_data'] );
411+
400412
$init_file = tempnam( sys_get_temp_dir(), 'export_init' );
401413
if ( false === $init_file ) {
402414
if ( file_exists( $temp_db ) ) {

0 commit comments

Comments
 (0)