5 * Contains \Drupal\Console\Command\Database\RestoreCommand.
8 namespace Drupal\Console\Command\Database;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Process\ProcessBuilder;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Command\Shared\ConnectTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
20 class RestoreCommand extends Command
31 * RestoreCommand constructor.
33 * @param string $appRoot
35 public function __construct($appRoot)
37 $this->appRoot = $appRoot;
38 parent::__construct();
44 protected function configure()
47 ->setName('database:restore')
48 ->setDescription($this->trans('commands.database.restore.description'))
51 InputArgument::OPTIONAL,
52 $this->trans('commands.database.restore.arguments.database'),
58 InputOption::VALUE_REQUIRED,
59 $this->trans('commands.database.restore.options.file')
61 ->setHelp($this->trans('commands.database.restore.help'));
67 protected function execute(InputInterface $input, OutputInterface $output)
69 $io = new DrupalStyle($input, $output);
71 $database = $input->getArgument('database');
72 $file = $input->getOption('file');
73 $learning = $input->getOption('learning');
75 $databaseConnection = $this->resolveConnection($io, $database);
79 $this->trans('commands.database.restore.messages.no-file')
83 if ($databaseConnection['driver'] == 'mysql') {
85 'mysql --user=%s --password=%s --host=%s --port=%s %s < %s',
86 $databaseConnection['username'],
87 $databaseConnection['password'],
88 $databaseConnection['host'],
89 $databaseConnection['port'],
90 $databaseConnection['database'],
93 } elseif ($databaseConnection['driver'] == 'pgsql') {
95 'PGPASSWORD="%s" psql -w -U %s -h %s -p %s -d %s -f %s',
96 $databaseConnection['password'],
97 $databaseConnection['username'],
98 $databaseConnection['host'],
99 $databaseConnection['port'],
100 $databaseConnection['database'],
106 $io->commentBlock($command);
109 $processBuilder = new ProcessBuilder(['-v']);
110 $process = $processBuilder->getProcess();
111 $process->setWorkingDirectory($this->appRoot);
112 $process->setTty('true');
113 $process->setCommandLine($command);
116 if (!$process->isSuccessful()) {
117 throw new \RuntimeException($process->getErrorOutput());
123 $this->trans('commands.database.restore.messages.success'),