Skip to content

Commit

Permalink
Issue #384: Make mariadb fix compatible with unzipped sql file and fi…
Browse files Browse the repository at this point in the history
…x docs (#442)
  • Loading branch information
yorkshire-pudding authored Sep 19, 2024
1 parent 25acca6 commit 7523e1a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
41 changes: 37 additions & 4 deletions commands/db.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,18 @@ function db_import_bee_callback($arguments, $options) {
$gzip = TRUE;
}

// Import the database.
// Start preparing the import command.
$import_command = '';

// Define the mysql executable.
$mysql_bin = bee_get_executable_path('mysql');
if (!$mysql_bin) {
bee_message(bt("The MySQL client command 'mysql' cannot be found in this system. Please install it and try again."), 'error');
return FALSE;
}
$import_command = '';

// If the file is a gzip file, define the gunzip executable and add the
// command at the start.
if ($gzip) {
$gunzip_bin = bee_get_executable_path('gunzip');
if (!$gunzip_bin) {
Expand All @@ -219,18 +224,46 @@ function db_import_bee_callback($arguments, $options) {
// so.
$mariadb_compatibility = (!empty($options['mariadb-compatibility'])) ? TRUE : FALSE;
if ($mariadb_compatibility) {
// Define the grep executable.
$grep_bin = bee_get_executable_path('grep');

// Define the grep command.
$mariadb_compatibility_command = $grep_bin . ' -vF \'/*!999999\- enable the sandbox mode */ \'';
$import_command .= "$mariadb_compatibility_command | ";

if ($gzip) {
// If the sql file is zipped then we execute the command on the output of
// the unzipped file and pipe to mysql.
$import_command .= "$mariadb_compatibility_command | ";
}
else {
// If the sql file is not zipped then we execute the command on the file
// and pipe to mysql.
$import_command .= "$mariadb_compatibility_command $filename | ";
}
}

// Add the mysql command with connection string.
$import_command .= $mysql_bin . ' ' . $connection_string;
if (!$gzip) {
$import_command .= " < $filename";
if (!$mariadb_compatibility) {
// If the MariaDB compatibility fix is not used, then add filename to the
// end.
$import_command .= " < $filename";
}
}

// Allow errors from mysql to go to stdout so we can store in a variable.
$import_command .= " 2>&1";

// Output the compiled import command as a debug message.
bee_instant_message('$import_command: ' . $import_command, 'debug');

// Execute the command.
exec($import_command, $output, $result);

// Output the result code and any output as a debug message.
bee_instant_message('Result: ' . $result, 'debug', $output);

// Remove the temporary mysql options file.
bee_delete($connection_file);

Expand Down
5 changes: 4 additions & 1 deletion docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,20 @@ Note: The path is always relative to the Backdrop root so if you want to export

#### `db-import`
*Description:* Import an SQL file into the current database.
*Aliases:*`dbim`, `sql-import`
*Aliases:* `dbim`, `sql-import`

*Arguments:*
- `file` - The SQL file to import into the database. Can be gzipped (i.e. *.sql.gz).

Note: The path is always relative to the Backdrop root so if you want to import from a folder above this, use `../` and the filename.

*Options:*
- `--mariadb-compatibility`, `-mdbc`` - Recent MariaDB versions have started including a command to enable sandbox mode, which can cause issues for importing on older versions or into MySQL. Use this option to remove that string before import.

*Examples:*
- `bee db-import backup.sql` - Import backup.sql into the current database.
- `bee db-import db.sql.gz` - Extract and import db.sql into the current database.
- `bee db-import ../db/db_export.sql.gz` - Extract and import db_export.sql from folder ../db into the current database
- `bee db-import db.sql.gz --mariadb-compatibility` - Extract and import db.sql into the current database removing any MariaDB sandbox switch first.

#### `db-drop`
Expand Down

0 comments on commit 7523e1a

Please sign in to comment.