Skip to content

Commit 14dead1

Browse files
hjuarez20enzolutions
authored andcommitted
[database:dump | database:restore] Implement the symfony process and remove escapeshellcmd (#4092)
* [update:execute] Fixed update table * Revert "Merge remote-tracking branch 'upstream/master'" This reverts commit ddf7739, reversing changes made to a95b7e6. * [database:dump] Implemented symfony process and remove escapeshellcmd
1 parent e076e83 commit 14dead1

File tree

3 files changed

+101
-92
lines changed

3 files changed

+101
-92
lines changed

src/Command/Database/DumpCommand.php

Lines changed: 93 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Drupal\Console\Command\Shared\ConnectTrait;
1616
use Drupal\Console\Core\Utils\ShellProcess;
1717
use Drupal\Core\Database\Connection;
18+
use Symfony\Component\Process\Process;
1819

1920
class DumpCommand extends Command
2021
{
@@ -82,10 +83,10 @@ protected function configure()
8283
$this->trans('commands.database.dump.options.gz')
8384
)
8485
->addOption(
85-
'exclude-cache',
86-
null,
87-
InputOption::VALUE_NONE,
88-
$this->trans('commands.database.dump.options.exclude.cache')
86+
'exclude-cache',
87+
null,
88+
InputOption::VALUE_NONE,
89+
$this->trans('commands.database.dump.options.exclude.cache')
8990
)
9091
->setHelp($this->trans('commands.database.dump.help'))
9192
->setAliases(['dbdu']);
@@ -140,97 +141,106 @@ protected function execute(InputInterface $input, OutputInterface $output)
140141

141142
$command = null;
142143

143-
if ($databaseConnection['driver'] == 'mysql') {
144-
$command = sprintf(
145-
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
146-
$databaseConnection['username'],
147-
$databaseConnection['password'],
148-
$databaseConnection['host'],
149-
$databaseConnection['port'],
150-
$databaseConnection['database'],
151-
$file
152-
);
153-
154-
if ($excludeCache) {
155-
$ignoreTable = '';
156-
foreach ($excludeTables as $table) {
157-
$ignoreTable .= "--ignore-table=\"{$table}\" ";
158-
}
159-
160-
$command = sprintf(
161-
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" %s "%s"> "%s"',
162-
$databaseConnection['username'],
163-
$databaseConnection['password'],
164-
$databaseConnection['host'],
165-
$databaseConnection['port'],
166-
$ignoreTable,
167-
$databaseConnection['database'],
168-
$file
169-
);
170-
171-
}
144+
if ($databaseConnection['driver'] == 'mysql') {
145+
$command = sprintf(
146+
"mysqldump --user='%s' --password='%s' --host='%s' --port='%s' '%s' > '%s'",
147+
$databaseConnection['username'],
148+
$databaseConnection['password'],
149+
$databaseConnection['host'],
150+
$databaseConnection['port'],
151+
$databaseConnection['database'],
152+
$file
153+
);
154+
155+
if ($excludeCache) {
156+
$ignoreTable = '';
157+
foreach ($excludeTables as $table) {
158+
$ignoreTable .= "--ignore-table=\"{$table}\" ";
159+
}
160+
161+
$command = sprintf(
162+
"mysqldump --user='%s' --password='%s' --host='%s' --port='%s' %s '%s'> '%s'",
163+
$databaseConnection['username'],
164+
$databaseConnection['password'],
165+
$databaseConnection['host'],
166+
$databaseConnection['port'],
167+
$ignoreTable,
168+
$databaseConnection['database'],
169+
$file
170+
);
171+
172+
}
172173
} elseif ($databaseConnection['driver'] == 'pgsql') {
173-
$command = sprintf(
174-
'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
175-
$databaseConnection['password'],
176-
$databaseConnection['username'],
177-
$databaseConnection['host'],
178-
$databaseConnection['port'],
179-
$databaseConnection['database'],
180-
$file
181-
);
182-
183-
if ($excludeCache) {
184-
$ignoreTable = '';
185-
foreach ($excludeTables as $table) {
186-
$ignoreTable .= "-T \"{$table}\" ";
187-
}
188-
189-
$command = sprintf(
190-
'PGPASSWORD="%s" pg_dump -w -U "%s" -h "%s" -p "%s" -f "%s" %s-d "%s"',
191-
$databaseConnection['password'],
192-
$databaseConnection['username'],
193-
$databaseConnection['host'],
194-
$databaseConnection['port'],
195-
$file,
196-
$ignoreTable,
197-
$databaseConnection['database']
198-
);
199-
}
174+
$command = sprintf(
175+
"PGPASSWORD='%s' pg_dumpall -w -U '%s' -h '%s' -p '%s' -l '%s' -f '%s'",
176+
$databaseConnection['password'],
177+
$databaseConnection['username'],
178+
$databaseConnection['host'],
179+
$databaseConnection['port'],
180+
$databaseConnection['database'],
181+
$file
182+
);
183+
184+
if ($excludeCache) {
185+
$ignoreTable = '';
186+
foreach ($excludeTables as $table) {
187+
$ignoreTable .= "-T \"{$table}\" ";
188+
}
189+
190+
$command = sprintf(
191+
"PGPASSWORD='%s' pg_dump -w -U '%s' -h '%s' -p '%s' -f '%s' %s-d '%s'",
192+
$databaseConnection['password'],
193+
$databaseConnection['username'],
194+
$databaseConnection['host'],
195+
$databaseConnection['port'],
196+
$file,
197+
$ignoreTable,
198+
$databaseConnection['database']
199+
);
200+
}
200201
}
201202

202203
if ($learning) {
203204
$this->getIo()->commentBlock($command);
204205
}
205206

206-
if ($this->shellProcess->exec($command, $this->appRoot)) {
207-
$resultFile = $file;
208-
if ($gz) {
209-
if (substr($file, -3) != '.gz') {
210-
$resultFile = $file . '.gz';
211-
}
212-
file_put_contents(
213-
$resultFile,
214-
gzencode(
215-
file_get_contents(
216-
$file
207+
try {
208+
$process = new Process($command);
209+
$process->setTimeout(null);
210+
$process->setWorkingDirectory($this->appRoot);
211+
$process->run();
212+
213+
if($process->isSuccessful()) {
214+
$resultFile = $file;
215+
if ($gz) {
216+
if (substr($file, -3) != '.gz') {
217+
$resultFile = $file . '.gz';
218+
}
219+
file_put_contents(
220+
$resultFile,
221+
gzencode(
222+
file_get_contents(
223+
$file
224+
)
217225
)
226+
);
227+
if ($resultFile != $file) {
228+
unlink($file);
229+
}
230+
}
231+
232+
$this->getIo()->success(
233+
sprintf(
234+
'%s %s',
235+
$this->trans('commands.database.dump.messages.success'),
236+
$resultFile
218237
)
219238
);
220-
if ($resultFile != $file) {
221-
unlink($file);
222-
}
223239
}
224240

225-
$this->getIo()->success(
226-
sprintf(
227-
'%s %s',
228-
$this->trans('commands.database.dump.messages.success'),
229-
$resultFile
230-
)
231-
);
241+
return 0;
242+
} catch (\Exception $e) {
243+
return 1;
232244
}
233-
234-
return 0;
235245
}
236246
}

src/Command/Database/RestoreCommand.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Symfony\Component\Console\Input\InputOption;
1212
use Symfony\Component\Console\Input\InputInterface;
1313
use Symfony\Component\Console\Output\OutputInterface;
14-
use Symfony\Component\Process\ProcessBuilder;
14+
use Symfony\Component\Process\Process;
1515
use Drupal\Console\Core\Command\Command;
1616
use Drupal\Console\Command\Shared\ConnectTrait;
1717

@@ -94,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9494
if ($databaseConnection['driver'] == 'mysql') {
9595
// Drop database first.
9696
$commands[] = sprintf(
97-
'mysql --user=%s --password=%s --host=%s --port=%s -e"DROP DATABASE IF EXISTS %s"',
97+
"mysql --user='%s' --password='%s' --host='%s' --port='%s' -e'DROP DATABASE IF EXISTS %s'",
9898
$databaseConnection['username'],
9999
$databaseConnection['password'],
100100
$databaseConnection['host'],
@@ -104,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
104104

105105
// Recreate database.
106106
$commands[] = sprintf(
107-
'mysql --user=%s --password=%s --host=%s --port=%s -e"CREATE DATABASE %s"',
107+
"mysql --user='%s' --password='%s' --host='%s' --port='%s' -e'CREATE DATABASE %s'",
108108
$databaseConnection['username'],
109109
$databaseConnection['password'],
110110
$databaseConnection['host'],
@@ -114,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
114114

115115
// Import dump.
116116
$commands[] = sprintf(
117-
$catCommand . 'mysql --user=%s --password=%s --host=%s --port=%s %s',
117+
$catCommand . "mysql --user='%s' --password='%s' --host='%s' --port='%s' %s",
118118
$file,
119119
$databaseConnection['username'],
120120
$databaseConnection['password'],
@@ -124,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
124124
);
125125
} elseif ($databaseConnection['driver'] == 'pgsql') {
126126
$commands[] = sprintf(
127-
'PGPASSWORD="%s" ' . $catCommand . 'psql -w -U %s -h %s -p %s -d %s',
127+
"PGPASSWORD='%s' " . $catCommand . "psql -w -U '%s' -h '%s' -p '%s' -d '%s'",
128128
$file,
129129
$databaseConnection['password'],
130130
$databaseConnection['username'],
@@ -139,11 +139,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
139139
$this->getIo()->commentBlock($command);
140140
}
141141

142-
$processBuilder = new ProcessBuilder(['-v']);
143-
$process = $processBuilder->getProcess();
142+
$process = new Process($command);
143+
$process->setTimeout(null);
144144
$process->setWorkingDirectory($this->appRoot);
145145
$process->setTty($input->isInteractive());
146-
$process->setCommandLine($command);
147146
$process->run();
148147

149148
if (!$process->isSuccessful()) {

src/Command/Shared/ConnectTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function escapeConnection($databaseConnection) {
7575
];
7676

7777
foreach ($settings as $setting) {
78-
$databaseConnection[$setting] = escapeshellcmd($databaseConnection[$setting]);
78+
$databaseConnection[$setting] = $databaseConnection[$setting];
7979
}
8080

8181
return $databaseConnection;

0 commit comments

Comments
 (0)