Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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'.
"""
45 changes: 45 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,51 @@
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 ( 0 === strpos( $first_line, '/*!999999\- enable the sandbox mode */' ) ) {

Check failure on line 830 in src/DB_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Parameter #1 $haystack of function strpos expects string, string|false given.
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 );

if ( 0 === $result['exit_code'] ) {
WP_CLI::success( sprintf( "Imported from '%s'.", $result_file ) );
} else {
WP_CLI::error( sprintf( "Failed to import from '%s'.", $result_file ) );
}

return;
}
Comment on lines 819 to 859

Choose a reason for hiding this comment

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

medium

This new logic for handling MariaDB sandbox dumps adds a significant amount of code to the import() method. To improve readability and maintainability, consider extracting this block into a new private helper method, for example handle_mariadb_sandbox_import(). The import() method would then just contain the detection logic and a call to this new method.


$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