Skip to content

Commit 5b10053

Browse files
committed
retrieve sql params from restored config/config.php
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 1868597 commit 5b10053

File tree

2 files changed

+101
-24
lines changed

2 files changed

+101
-24
lines changed

lib/Command/PointRestore.php

Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ private function restorePointData(RestoringPoint $point, RestoringData $data): v
352352
* @throws SqlDumpException
353353
*/
354354
private function restorePointSqlDump(RestoringPoint $point, RestoringData $data): void {
355+
$fromConfig = $this->getSqlParamsFromConfig($point);
356+
355357
while (true) {
356358
try {
357-
$sqlParams = $this->requestSqlParams();
359+
$sqlParams = $this->requestSqlParams($fromConfig);
358360
} catch (RestoringDataNotFoundException $e) {
359361
$this->output->writeln(' * ignoring sqldump');
360362

@@ -432,29 +434,58 @@ private function requestDataRoot(RestoringData $data): string {
432434
* @return array
433435
* @throws RestoringDataNotFoundException
434436
*/
435-
private function requestSqlParams(): array {
437+
private function requestSqlParams(array $fromConfig = []): array {
436438
$sqlParams = $this->pointService->getSqlParams();
437439
if ($this->input->getOption('do-not-ask-sql')) {
438440
return $sqlParams;
439441
}
440442

443+
$options = ['cancel', 'yes', 'no'];
444+
if (!empty($fromConfig)) {
445+
array_push($options, 'load');
446+
}
447+
441448
while (true) {
442449
$this->output->writeln(' > will be imported in ' . $this->displaySqlParams($sqlParams, true));
443450

451+
$this->output->writeln('');
452+
$this->output->writeln(' * <comment>Do you want to</comment>:');
453+
$this->output->writeln(' - use this settings and start importing the sqldump (yes)');
454+
$this->output->writeln(' - manually editing the Sql settings (edit)');
455+
if (!empty($fromConfig)) {
456+
$this->output->writeln(
457+
' - load the settings from the config/config.php found in the backup (load)'
458+
);
459+
$displayLoad = 'load/';
460+
} else {
461+
$displayLoad = '';
462+
}
463+
$this->output->writeln(
464+
' - cancel the import of the sqldump, and go on with the restoring process (cancel)'
465+
);
466+
$this->output->writeln('');
467+
444468
$helper = $this->getHelper('question');
445469
$question = new Question(
446-
' - <comment>Do you want to import the dump in the current database, or cancel the import ?</comment> (yes/No/cancel) ',
447-
'no',
470+
' * <comment>Do you want to import the sqldump using those settings?</comment> (yes/Edit/'
471+
. $displayLoad . 'cancel) ',
472+
'edit',
448473
);
449-
$question->setAutocompleterValues(['cancel', 'yes', 'no']);
474+
$question->setAutocompleterValues($options);
450475

451-
switch (strtolower($helper->ask($this->input, $this->output, $question))) {
476+
$ret = strtolower($helper->ask($this->input, $this->output, $question));
477+
switch ($ret) {
452478
case 'yes':
453479
return $sqlParams;
454480
case 'cancel':
455481
throw new RestoringDataNotFoundException();
456482
}
457483

484+
if ($ret === 'load') {
485+
$sqlParams = $fromConfig;
486+
continue;
487+
}
488+
458489
$this->output->writeln(' - current configuration:');
459490
$this->displaySqlParams($sqlParams);
460491

@@ -534,14 +565,36 @@ private function requestSqlParams(): array {
534565
}
535566

536567

568+
/**
569+
* @param RestoringPoint $point
570+
*
571+
* @return array
572+
*/
573+
private function getSqlParamsFromConfig(RestoringPoint $point): array {
574+
$CONFIG = $this->getConfigDirect($point);
575+
if (empty($CONFIG)) {
576+
return [];
577+
}
578+
579+
return [
580+
ISqlDump::DB_TYPE => $this->get(ISqlDump::DB_TYPE, $CONFIG),
581+
ISqlDump::DB_NAME => $this->get(ISqlDump::DB_NAME, $CONFIG),
582+
ISqlDump::DB_HOST => $this->get(ISqlDump::DB_HOST, $CONFIG),
583+
ISqlDump::DB_PORT => $this->get(ISqlDump::DB_PORT, $CONFIG),
584+
ISqlDump::DB_USER => $this->get(ISqlDump::DB_USER, $CONFIG),
585+
ISqlDump::DB_PASS => $this->get(ISqlDump::DB_PASS, $CONFIG)
586+
];
587+
}
588+
589+
537590
/**
538591
* @param RestoringPoint $point
539592
*/
540593
private function updateConfig(RestoringPoint $point): void {
541594
$this->output->writeln('> Refreshing <info>config.php</info>');
542595

543596
$sqlParams = [];
544-
$dataRoot = $configRoot = '';
597+
$dataRoot = '';
545598
foreach ($point->getRestoringData() as $data) {
546599
if ($data->getType() === RestoringData::ROOT_DATA) {
547600
if ($data->getRestoredRoot() === '') {
@@ -550,18 +603,6 @@ private function updateConfig(RestoringPoint $point): void {
550603
$dataRoot = $data->getRestoredRoot();
551604
}
552605

553-
if ($data->getType() === RestoringData::FILE_CONFIG) {
554-
if ($data->getRestoredRoot() === '') {
555-
$this->output->writeln(
556-
' * do not refresh as <info>config/config.php</info> were not restored'
557-
);
558-
$this->configService->maintenanceMode(false);
559-
560-
return;
561-
}
562-
$configRoot = $data->getRestoredRoot();
563-
}
564-
565606
if ($data->getType() === RestoringData::FILE_SQL_DUMP) {
566607
if ($data->getRestoredRoot() === '') {
567608
continue;
@@ -573,9 +614,16 @@ private function updateConfig(RestoringPoint $point): void {
573614
}
574615
}
575616

576-
$CONFIG = [];
577-
$configFile = rtrim($configRoot, '/') . '/config.php';
578-
include $configFile;
617+
$configFile = '';
618+
$CONFIG = $this->getConfigDirect($point, $configFile);
619+
if (empty($CONFIG)) {
620+
$this->output->writeln(
621+
' * do not refresh as <info>config/config.php</info> were not restored'
622+
);
623+
$this->configService->maintenanceMode(false);
624+
625+
return;
626+
}
579627

580628
if ($dataRoot !== '') {
581629
$this->compareConfigDataRoot($CONFIG, $dataRoot);
@@ -597,6 +645,30 @@ private function updateConfig(RestoringPoint $point): void {
597645
}
598646

599647

648+
/**
649+
* @param RestoringPoint $point
650+
* @param string $configFile
651+
*
652+
* @return array
653+
*/
654+
private function getConfigDirect(RestoringPoint $point, string &$configFile = ''): array {
655+
$dataRoot = $configRoot = '';
656+
foreach ($point->getRestoringData() as $data) {
657+
if ($data->getType() === RestoringData::FILE_CONFIG) {
658+
if ($data->getRestoredRoot() === '') {
659+
return [];
660+
}
661+
$configRoot = $data->getRestoredRoot();
662+
}
663+
}
664+
665+
$CONFIG = [];
666+
$configFile = rtrim($configRoot, '/') . '/config.php';
667+
include $configFile;
668+
669+
return $CONFIG;
670+
}
671+
600672
/**
601673
* @param array $CONFIG
602674
* @param string $used
@@ -627,6 +699,9 @@ private function compareConfigDataRoot(array &$CONFIG, string $used): void {
627699
}
628700

629701

702+
703+
// private function getConfigSqlParams();
704+
630705
/**
631706
* @param array $sqlParams
632707
* @param array $CONFIG
@@ -797,12 +872,14 @@ private function restoreUniqueFile(
797872
*/
798873
private function displaySqlParams(array $sql, bool $oneLine = false): string {
799874
if ($oneLine) {
800-
return '<info>' . $this->get(ISqlDump::DB_USER, $sql) . '</info>:****@<info>'
875+
return '<info>' . $this->get(ISqlDump::DB_TYPE, $sql) . '</info>::/<info>'
876+
. $this->get(ISqlDump::DB_USER, $sql) . '</info>:****@<info>'
801877
. $this->get(ISqlDump::DB_HOST, $sql) . ':' . $this->get(ISqlDump::DB_PORT, $sql)
802878
. '</info>/<info>'
803879
. $this->get(ISqlDump::DB_NAME, $sql) . '</info>';
804880
}
805881

882+
$this->output->writeln(' . Type: <info>' . $this->get(ISqlDump::DB_TYPE, $sql) . '</info>');
806883
$this->output->writeln(' . Host: <info>' . $this->get(ISqlDump::DB_HOST, $sql) . '</info>');
807884
$this->output->writeln(' . Port: <info>' . $this->get(ISqlDump::DB_PORT, $sql) . '</info>');
808885
$this->output->writeln(' . Database: <info>' . $this->get(ISqlDump::DB_NAME, $sql) . '</info>');

lib/Service/PointService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ public function getSqlParams(): array {
539539
}
540540

541541
return [
542+
ISqlDump::DB_TYPE => $this->configService->getSystemValue(ISqlDump::DB_TYPE),
542543
ISqlDump::DB_NAME => $this->configService->getSystemValue(ISqlDump::DB_NAME),
543544
ISqlDump::DB_HOST => $host,
544545
ISqlDump::DB_PORT => $port,
@@ -561,7 +562,6 @@ public function getSqlDump(array $params = []): ISqlDump {
561562
} else {
562563
$dbType = $this->get(ISqlDump::DB_TYPE, $params);
563564
}
564-
565565
switch ($dbType) {
566566
case ISqlDump::MYSQL:
567567
$sqlDump = new SqlDumpMySQL();

0 commit comments

Comments
 (0)