Skip to content
Permalink
Browse files

[database:restore] Fixed conflicts of #3856 PR (#4037)

* #2174 Make command available without a site installed

* #2174 Recreate database before importing the dump

* Fixing tabs
  • Loading branch information...
harold20 authored and enzolutions committed May 15, 2019
1 parent 02726f5 commit 62faf57a262d2692518a83aae8a3d9887c6eb966
Showing with 60 additions and 36 deletions.
  1. +0 −5 config/services/database.yml
  2. +55 −31 src/Command/Database/RestoreCommand.php
  3. +5 −0 uninstall.services.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 }
@@ -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
)
);
@@ -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']

0 comments on commit 62faf57

Please sign in to comment.
You can’t perform that action at this time.