diff --git a/config/services/database.yml b/config/services/database.yml index 59a2c2f95..23a91609f 100644 --- a/config/services/database.yml +++ b/config/services/database.yml @@ -35,8 +35,3 @@ services: arguments: ['@database', '@date.formatter', '@entity_type.manager', '@string_translation'] tags: - { name: drupal.command } - console.database_restore: - class: Drupal\Console\Command\Database\RestoreCommand - arguments: ['@app.root'] - tags: - - { name: drupal.command } diff --git a/src/Command/Database/RestoreCommand.php b/src/Command/Database/RestoreCommand.php index 0dbc3a7b4..7ee368d20 100644 --- a/src/Command/Database/RestoreCommand.php +++ b/src/Command/Database/RestoreCommand.php @@ -83,55 +83,79 @@ protected function execute(InputInterface $input, OutputInterface $output) ); return 1; } + if (strpos($file, '.sql.gz') !== false) { $catCommand = 'gunzip -c %s | '; } else { $catCommand = 'cat %s | '; } - $command = NULL; + $commands = array(); if ($databaseConnection['driver'] == 'mysql') { - $command = sprintf( - $catCommand . 'mysql --user=%s --password=%s --host=%s --port=%s %s', - $file, - $databaseConnection['username'], - $databaseConnection['password'], - $databaseConnection['host'], - $databaseConnection['port'], - $databaseConnection['database'] + // Drop database first. + $commands[] = sprintf( + 'mysql --user=%s --password=%s --host=%s --port=%s -e"DROP DATABASE IF EXISTS %s"', + $databaseConnection['username'], + $databaseConnection['password'], + $databaseConnection['host'], + $databaseConnection['port'], + $databaseConnection['database'] + ); + + // Recreate database. + $commands[] = sprintf( + 'mysql --user=%s --password=%s --host=%s --port=%s -e"CREATE DATABASE %s"', + $databaseConnection['username'], + $databaseConnection['password'], + $databaseConnection['host'], + $databaseConnection['port'], + $databaseConnection['database'] + ); + + // Import dump. + $commands[] = sprintf( + $catCommand . 'mysql --user=%s --password=%s --host=%s --port=%s %s', + $file, + $databaseConnection['username'], + $databaseConnection['password'], + $databaseConnection['host'], + $databaseConnection['port'], + $databaseConnection['database'] ); } elseif ($databaseConnection['driver'] == 'pgsql') { - $command = sprintf( - $catCommand . 'PGPASSWORD="%s" psql -w -U %s -h %s -p %s -d %s', - $file, - $databaseConnection['password'], - $databaseConnection['username'], - $databaseConnection['host'], - $databaseConnection['port'], - $databaseConnection['database'] + $commands[] = sprintf( + 'PGPASSWORD="%s" ' . $catCommand . 'psql -w -U %s -h %s -p %s -d %s', + $file, + $databaseConnection['password'], + $databaseConnection['username'], + $databaseConnection['host'], + $databaseConnection['port'], + $databaseConnection['database'] ); } - if ($learning) { - $this->getIo()->commentBlock($command); - } + foreach ($commands as $command) { + if ($learning) { + $this->getIo()->commentBlock($command); + } - $processBuilder = new ProcessBuilder(['-v']); - $process = $processBuilder->getProcess(); - $process->setWorkingDirectory($this->appRoot); - $process->setTty($input->isInteractive()); - $process->setCommandLine($command); - $process->run(); + $processBuilder = new ProcessBuilder(['-v']); + $process = $processBuilder->getProcess(); + $process->setWorkingDirectory($this->appRoot); + $process->setTty($input->isInteractive()); + $process->setCommandLine($command); + $process->run(); - if (!$process->isSuccessful()) { - throw new \RuntimeException($process->getErrorOutput()); + if (!$process->isSuccessful()) { + throw new \RuntimeException($process->getErrorOutput()); + } } $this->getIo()->success( sprintf( - '%s %s', - $this->trans('commands.database.restore.messages.success'), - $file + '%s %s', + $this->trans('commands.database.restore.messages.success'), + $file ) ); diff --git a/uninstall.services.yml b/uninstall.services.yml index 51c6ea63c..c6ff8f052 100644 --- a/uninstall.services.yml +++ b/uninstall.services.yml @@ -11,6 +11,11 @@ services: arguments: ['@app.root', '@console.configuration_manager'] tags: - { name: drupal.command } + console.database_restore: + class: Drupal\Console\Command\Database\RestoreCommand + arguments: ['@app.root'] + tags: + - { name: drupal.command } console.site_install: class: Drupal\Console\Command\Site\InstallCommand arguments: ['@console.extension_manager', '@console.site', '@console.configuration_manager', '@app.root']