From 7523e1a167317129fa70326b581092290f5589ac Mon Sep 17 00:00:00 2001 From: Martin Price Date: Thu, 19 Sep 2024 10:03:41 +0100 Subject: [PATCH] Issue #384: Make mariadb fix compatible with unzipped sql file and fix docs (#442) --- commands/db.bee.inc | 41 +++++++++++++++++++++++++++++++++++++---- docs/Usage.md | 5 ++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index c4a0156..a3e4eea 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -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) { @@ -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); diff --git a/docs/Usage.md b/docs/Usage.md index 6d701f9..a749e4a 100644 --- a/docs/Usage.md +++ b/docs/Usage.md @@ -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`