Skip to content

Commit b61d0d9

Browse files
committed
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.
1 parent abeefa5 commit b61d0d9

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

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

431431
~~~
432-
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults]
432+
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--exclude_tables_data=<tables>] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults]
433433
~~~
434434

435435
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
456456
[--exclude_tables=<tables>]
457457
The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
458458

459+
[--exclude_tables_data=<tables>]
460+
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.
461+
459462
[--include-tablespaces]
460463
Skips adding the default --no-tablespaces option to mysqldump.
461464

@@ -506,6 +509,10 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
506509
$ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
507510
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
508511

512+
# Skip data of certain tables from the exported database
513+
$ wp db export --exclude_tables_data=wp_actionscheduler_logs
514+
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
515+
509516
# Export database to STDOUT.
510517
$ wp db export -
511518
-- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)

features/db-export.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ Feature: Export a WordPress database
3434
wp_options
3535
"""
3636

37+
Scenario: Exclude data of certain tables when exporting the database
38+
Given a WP install
39+
40+
When I run `wp db export wp_cli_test.sql --exclude_tables_data=wp_actionscheduler_logs --porcelain`
41+
Then the wp_cli_test.sql file should exist
42+
And the wp_cli_test.sql file should contain:
43+
"""
44+
wp_actionscheduler_logs
45+
"""
46+
3747
Scenario: Export database to STDOUT
3848
Given a WP install
3949

src/DB_Command.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ public function query( $args, $assoc_args ) {
567567
* [--exclude_tables=<tables>]
568568
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
569569
*
570+
* [--exclude_tables_data=<tables>]
571+
* : 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.
572+
*
570573
* [--include-tablespaces]
571574
* : Skips adding the default --no-tablespaces option to mysqldump.
572575
*
@@ -617,6 +620,10 @@ public function query( $args, $assoc_args ) {
617620
* $ wp db export --exclude_tables=$(wp db tables --all-tables-with-prefix --format=csv)
618621
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
619622
*
623+
* # Skip data of certain tables from the exported database
624+
* $ wp db export --exclude_tables_data=wp_actionscheduler_logs
625+
* Success: Exported to 'wordpress_dbase-db72bb5.sql'.
626+
*
620627
* # Export database to STDOUT.
621628
* $ wp db export -
622629
* -- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
@@ -708,6 +715,17 @@ public function export( $args, $assoc_args ) {
708715
}
709716
}
710717

718+
$exclude_tables_data = Utils\get_flag_value( $assoc_args, 'exclude_tables_data' );
719+
if ( isset( $exclude_tables_data ) ) {
720+
$tables = explode( ',', trim( $assoc_args['exclude_tables_data'], ',' ) );
721+
unset( $assoc_args['exclude_tables_data'] );
722+
foreach ( $tables as $table ) {
723+
$command .= ' --ignore-table-data';
724+
$command .= ' %s';
725+
$command_esc_args[] = trim( DB_NAME . '.' . $table );
726+
}
727+
}
728+
711729
$escaped_command = Utils\esc_cmd( $command, ...$command_esc_args );
712730

713731
// Remove parameters not needed for SQL run.

0 commit comments

Comments
 (0)