-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
RestoreCommand.php
131 lines (116 loc) · 3.76 KB
/
RestoreCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @file
* Contains \Drupal\Console\Command\Database\RestoreCommand.
*/
namespace Drupal\Console\Command\Database;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ConnectTrait;
class RestoreCommand extends Command
{
use ConnectTrait;
/**
* @var string
*/
protected $appRoot;
/**
* RestoreCommand constructor.
*
* @param string $appRoot
*/
public function __construct($appRoot)
{
$this->appRoot = $appRoot;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('database:restore')
->setDescription($this->trans('commands.database.restore.description'))
->addArgument(
'database',
InputArgument::OPTIONAL,
$this->trans('commands.database.restore.arguments.database'),
'default'
)
->addOption(
'file',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.database.restore.options.file')
)
->setHelp($this->trans('commands.database.restore.help'))
->setAliases(['dbr']);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$database = $input->getArgument('database');
$file = $input->getOption('file');
$learning = $input->getOption('learning');
$databaseConnection = $this->resolveConnection($database);
if (!$file) {
$this->getIo()->error(
$this->trans('commands.database.restore.messages.no-file')
);
return 1;
}
if (strpos($file, '.sql.gz') !== false) {
$catCommand = 'gunzip -c %s | ';
} else {
$catCommand = 'cat %s | ';
}
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']
);
} 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']
);
}
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();
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
$this->getIo()->success(
sprintf(
'%s %s',
$this->trans('commands.database.restore.messages.success'),
$file
)
);
return 0;
}
}