Skip to content

feat: add support for excluding table data during export#298

Open
astappiev wants to merge 5 commits intowp-cli:mainfrom
astappiev:patch-1
Open

feat: add support for excluding table data during export#298
astappiev wants to merge 5 commits intowp-cli:mainfrom
astappiev:patch-1

Conversation

@astappiev
Copy link

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.

Warning: only supported by MariaDB 10.1 and later. As far as I know, not supported by MySQL.

Resolves #289

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.
@astappiev astappiev requested a review from a team as a code owner October 10, 2025 09:09
@swissspidy swissspidy requested a review from Copilot November 2, 2025 14:59

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

@swissspidy swissspidy added this to the 2.1.5 milestone Jan 22, 2026
@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

❌ Patch coverage is 25.00000% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/DB_Command.php 25.00% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy

This comment was marked as resolved.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request successfully introduces the --exclude_tables_data option to the wp db export command, which is a valuable addition for users needing to export table structures without their data. The implementation follows the existing patterns in the codebase. I have suggested a few improvements to make the logic more robust, specifically regarding input sanitization (handling spaces in the comma-separated list) and providing a clearer error message when the option is used on unsupported database systems (MySQL), as noted in the PR description.

Comment on lines +738 to +747
$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 );
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation has a few robustness issues:

  1. Database Compatibility: As noted in the PR description, this flag is MariaDB-specific. Adding an explicit check for the database type allows us to provide a helpful WP-CLI error message instead of a cryptic shell error from mysqldump.
  2. Input Sanitization: The trim() on line 745 is applied to the concatenated DB_NAME . '.' . $table string. If the user provides a list like table1, table2 (with a space), the resulting argument becomes dbname. table2, which will cause mysqldump to fail. The trim() should be applied to the $table variable individually.
  3. Type Safety: If the flag is passed without a value (e.g., --exclude_tables_data), get_flag_value returns true. The subsequent trim() and explode() calls on a boolean can lead to unexpected behavior (e.g., trying to ignore a table named dbname.1).
		$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.' );
				}

				$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;
				}
			}
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Giving multiple same arguments to mysqldump (via wp db export) only forwards the latest

2 participants

Comments