Skip to content

Commit

Permalink
[database:restore] Fixed conflicts of #3856 PR (#4037)
Browse files Browse the repository at this point in the history
* #2174 Make command available without a site installed

* #2174 Recreate database before importing the dump

* Fixing tabs
  • Loading branch information
hjuarez20 authored and enzolutions committed May 15, 2019
1 parent 02726f5 commit 62faf57
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
5 changes: 0 additions & 5 deletions config/services/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
86 changes: 55 additions & 31 deletions src/Command/Database/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);

Expand Down
5 changes: 5 additions & 0 deletions uninstall.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 62faf57

Please sign in to comment.