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
18 changes: 18 additions & 0 deletions features/db-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,21 @@ Feature: Import a WordPress database
"""
🍣
"""

Scenario: Import MariaDB sandbox dump
Given a WP install
And a sandbox.sql file:
"""
/*!999999\- enable the sandbox mode */
INSERT INTO wp_terms (name, slug, term_group) VALUES ('Test Term', 'test-term', 0);
"""

When I run `wp db import sandbox.sql`
Then STDOUT should contain:
"""
MariaDB sandbox mode directive detected. Skipping it by piping the file content.
"""
And STDOUT should contain:
"""
Success: Imported from 'sandbox.sql'.
"""
42 changes: 42 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,48 @@ public function import( $args, $assoc_args ) {
WP_CLI::error( sprintf( 'Import file missing or not readable: %s', $result_file ) );
}

// Check for MariaDB sandbox mode directive in the first line.
// This directive can interfere with standard imports by enabling a sandbox mode
// that restricts certain operations. We skip it by piping the file content
// and skipping the first line.
$fp = fopen( $result_file, 'r' );
if ( ! $fp ) {
WP_CLI::error( sprintf( 'Unable to read import file: %s', $result_file ) );
}
$first_line = fgets( $fp );
fclose( $fp );

if ( false !== $first_line && strpos( $first_line, '999999' ) !== false && strpos( $first_line, 'sandbox mode' ) !== false ) {
WP_CLI::log( 'MariaDB sandbox mode directive detected. Skipping it by piping the file content.' );
$preamble = $this->get_sql_mode_query( $assoc_args ) . "\n";
if ( ! Utils\get_flag_value( $assoc_args, 'skip-optimization' ) ) {
$preamble .= "SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0;\n";
}

$postamble = Utils\get_flag_value( $assoc_args, 'skip-optimization' ) ? '' : "\nCOMMIT;\n";

// Use a shell pipeline to skip the first line and wrap the rest in transaction/optimizations.
$command = sprintf(
'sh -c \'p="$1"; f="$2"; s="$3"; shift 3; ( printf "%%s" "$p"; tail -n +2 "$f"; printf "%%s" "$s" ) | %s %s --no-auto-rehash "$@"\' sh %s %s %s',
$this->get_mysql_command(),
$this->get_defaults_flag_string( $assoc_args ),
escapeshellarg( $preamble ),
escapeshellarg( $result_file ),
escapeshellarg( $postamble )
);

// Ensure we don't pass 'execute' which would conflict with STDIN.
unset( $mysql_args['execute'] );

$result = self::run( $command, $mysql_args );

self::run( $command, $mysql_args );

WP_CLI::success( sprintf( "Imported from '%s'.", $result_file ) );

return;
}

$query = Utils\get_flag_value( $assoc_args, 'skip-optimization' )
? 'SOURCE %s;'
: 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %s; COMMIT;';
Expand Down
Loading