Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--exclude_tables_data=<tables>] [--include-tablespaces] [--porcelain] [--defaults]
~~~

**Alias:** `dump`
Expand Down Expand Up @@ -467,6 +467,10 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
[--exclude_tables=<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=<tables>]
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.

Expand Down Expand Up @@ -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)
Expand Down
81 changes: 81 additions & 0 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ public function query( $args, $assoc_args ) {
* [--exclude_tables=<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=<tables>]
* : 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.
*
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
* [--include-tablespaces]
* : Skips adding the default --no-tablespaces option to mysqldump.
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'] );
Expand All @@ -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;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
$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.
Expand Down
12 changes: 12 additions & 0 deletions src/DB_Command_SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down