Skip to content

Commit 15acb52

Browse files
feat: add support for excluding table data during export (#298)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 7f759b0 commit 15acb52

4 files changed

Lines changed: 137 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ To confirm the ID for the site you want to query, you can use the `wp site list`
442442
Exports the database to a file or to STDOUT.
443443

444444
~~~
445-
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
445+
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--exclude_tables_data=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
446446
~~~
447447

448448
**Alias:** `dump`
@@ -472,6 +472,10 @@ Requires `mysqldump` or `mariadb-dump` client binary to be available.
472472
[--exclude_tables=<tables>]
473473
The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
474474

475+
[--exclude_tables_data=<tables>]
476+
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.
477+
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.
478+
475479
[--include-tablespaces]
476480
Skips adding the default --no-tablespaces option to mysqldump.
477481

@@ -519,6 +523,10 @@ Requires `mysqldump` or `mariadb-dump` client binary to be available.
519523
$ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
520524
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
521525

526+
# Skip data of certain tables from the exported database
527+
$ wp db export --exclude_tables_data=wp_actionscheduler_logs
528+
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
529+
522530
# Export database to STDOUT.
523531
$ wp db export -
524532
-- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)

‎features/db-export.feature‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,87 @@ Feature: Export a WordPress database
7070
PRAGMA foreign_keys=OFF
7171
"""
7272

73+
# Support depends on the dump client, not the server (e.g. mariadb-dump works against a MySQL server),
74+
# so fake a dump client without --ignore-table-data, like MySQL's mysqldump.
75+
@skip-sqlite
76+
Scenario: Exclude data of certain tables is not supported by the dump client
77+
Given a WP install
78+
And a fake-bin/mysqldump file:
79+
"""
80+
#!/bin/sh
81+
case "$1" in
82+
--version) echo "mysqldump Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)" ;;
83+
--help) echo " --ignore-table=name Do not dump the specified table." ;;
84+
esac
85+
exit 0
86+
"""
87+
And a fake-bin/mariadb-dump file:
88+
"""
89+
#!/bin/sh
90+
case "$1" in
91+
--version) echo "mysqldump Ver 8.0.40 for Linux on x86_64 (MySQL Community Server - GPL)" ;;
92+
--help) echo " --ignore-table=name Do not dump the specified table." ;;
93+
esac
94+
exit 0
95+
"""
96+
97+
When I run `chmod +x fake-bin/mysqldump fake-bin/mariadb-dump`
98+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db export wp_cli_test.sql --exclude_tables_data=wp_users`
99+
Then the return code should be 1
100+
And STDERR should contain:
101+
"""
102+
Error: The --exclude_tables_data option requires a dump client that supports --ignore-table-data
103+
"""
104+
And the wp_cli_test.sql file should not exist
105+
106+
# Only the MariaDB dump client supports --ignore-table-data.
107+
@require-mariadb
108+
Scenario: Exclude data of certain tables when exporting the database
109+
Given a WP install
110+
111+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users,wp_options --porcelain`
112+
Then the wp_cli_test.sql file should exist
113+
And the wp_cli_test.sql file should contain:
114+
"""
115+
CREATE TABLE `wp_users`
116+
"""
117+
And the wp_cli_test.sql file should contain:
118+
"""
119+
CREATE TABLE `wp_options`
120+
"""
121+
And the wp_cli_test.sql file should contain:
122+
"""
123+
INSERT INTO `wp_posts`
124+
"""
125+
And the wp_cli_test.sql file should not contain:
126+
"""
127+
INSERT INTO `wp_users`
128+
"""
129+
And the wp_cli_test.sql file should not contain:
130+
"""
131+
INSERT INTO `wp_options`
132+
"""
133+
134+
@skip-sqlite
135+
Scenario: Exclude data of certain tables cannot be combined with --tables
136+
Given a WP install
137+
138+
When I try `wp db export wp_cli_test.sql --tables=wp_users,wp_posts --exclude_tables_data=wp_users`
139+
Then the return code should be 1
140+
And STDERR should contain:
141+
"""
142+
Error: The --exclude_tables_data option cannot be combined with --tables.
143+
"""
144+
145+
@require-sqlite
146+
Scenario: Exclude data of certain tables when exporting a SQLite database
147+
Given a WP install
148+
149+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_users --porcelain`
150+
Then the contents of the wp_cli_test.sql file should match /CREATE TABLE [`"]?wp_users[`"]?/
151+
And the contents of the wp_cli_test.sql file should match /INSERT INTO [`"]?wp_posts[`"]?/
152+
And the contents of the wp_cli_test.sql file should not match /INSERT INTO [`"]?wp_users[`"]?/
153+
73154
@skip-sqlite
74155
Scenario: Export database to STDOUT
75156
Given a WP install

‎src/DB_Command.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ public function query( $args, $assoc_args ) {
710710
* [--exclude_tables=<tables>]
711711
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
712712
*
713+
* [--exclude_tables_data=<tables>]
714+
* : 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.
715+
* 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.
716+
*
713717
* [--include-tablespaces]
714718
* : Skips adding the default --no-tablespaces option to mysqldump.
715719
*
@@ -757,6 +761,10 @@ public function query( $args, $assoc_args ) {
757761
* $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
758762
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
759763
*
764+
* # Skip data of certain tables from the exported database
765+
* $ wp db export --exclude_tables_data=wp_actionscheduler_logs
766+
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
767+
*
760768
* # Export database to STDOUT.
761769
* $ wp db export -
762770
* -- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
@@ -840,6 +848,8 @@ public function export( $args, $assoc_args ) {
840848

841849
$command_esc_args = [ DB_NAME ];
842850

851+
$has_tables = isset( $assoc_args['tables'] );
852+
843853
if ( isset( $assoc_args['tables'] ) ) {
844854
$tables = explode( ',', trim( $assoc_args['tables'], ',' ) );
845855
unset( $assoc_args['tables'] );
@@ -861,6 +871,31 @@ public function export( $args, $assoc_args ) {
861871
}
862872
}
863873

874+
$exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data', '' );
875+
unset( $assoc_args['exclude_tables_data'] );
876+
if ( is_string( $exclude_tables_data ) && '' !== trim( $exclude_tables_data, ', ' ) ) {
877+
// The dump client ignores --ignore-table-data for tables named with --tables.
878+
if ( $has_tables ) {
879+
WP_CLI::error( 'The --exclude_tables_data option cannot be combined with --tables.' );
880+
}
881+
882+
// --ignore-table-data is a dump client option, so check the client rather than the server.
883+
if ( ! $this->command_supports_option( $mysqldump_binary, 'ignore-table-data' ) ) {
884+
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 ) );
885+
}
886+
887+
$tables = explode( ',', trim( $exclude_tables_data, ',' ) );
888+
foreach ( $tables as $table ) {
889+
$table = trim( $table );
890+
if ( '' === $table ) {
891+
continue;
892+
}
893+
$command .= ' --ignore-table-data';
894+
$command .= ' %s';
895+
$command_esc_args[] = DB_NAME . '.' . $table;
896+
}
897+
}
898+
864899
$escaped_command = Utils\esc_cmd( $command, ...$command_esc_args );
865900

866901
// Remove parameters not needed for SQL run.

‎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)