Skip to content

Commit

Permalink
Merge pull request #190 from phpsu/bugfix/mysql-sync-wrong-exit-code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Jul 17, 2024
2 parents a370c39 + 662fd27 commit b1828af
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 131 deletions.
30 changes: 21 additions & 9 deletions src/Command/DatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public function generate(ShellBuilder $shellBuilder): ShellBuilder
$sshCommand->setSshConfig($this->sshConfig);
$sshCommand->setInto($this->fromHost);
$sshCommand->setVerbosity($this->verbosity);
$sshCommand->setCommand($dumpBuilder);
$sshCommand->setCommand($this->prependPipefail($dumpBuilder));
$sshCommand->generate($shellBuilder);
}

Expand All @@ -363,10 +363,12 @@ public function generate(ShellBuilder $shellBuilder): ShellBuilder
->setInto($this->toHost)
->setVerbosity($this->verbosity)
->setCommand(
$importBuilder->if(
$unCompressCmd !== '' && $unCompressCmd !== '0',
static fn(ShellBuilder $builder): ShellBuilder => $builder->add($unCompressCmd)->pipe($importCommand),
static fn(ShellBuilder $builder): ShellBuilder => $builder->add($importCommand)
$this->prependPipefail(
$importBuilder->if(
$unCompressCmd !== '' && $unCompressCmd !== '0',
static fn(ShellBuilder $builder): ShellBuilder => $builder->add($unCompressCmd)->pipe($importCommand),
static fn(ShellBuilder $builder): ShellBuilder => $builder->add($importCommand)
)
)
)
->generate(ShellBuilder::new());
Expand All @@ -376,18 +378,28 @@ public function generate(ShellBuilder $shellBuilder): ShellBuilder
$importBuilder->add($importCommand);
}

return $shellBuilder->if(
$dumpBuilderPackagedIfNeeded = $shellBuilder->if(
$shellBuilder->__toArray() === [],
static fn(ShellBuilder $builder): ShellBuilder => $builder->add($dumpBuilder)
)->pipe($importBuilder);
);
return $this->prependPipefail($dumpBuilderPackagedIfNeeded->pipe($importBuilder));
}

if (!$this->fromHost) {
return $this->prependPipefail($dumpBuilder->pipe($importCommand));
}

$sshCommand = new SshCommand();
$sshCommand->setSshConfig($this->sshConfig);
$sshCommand->setInto($this->fromHost);
$sshCommand->setVerbosity($this->verbosity);
$sshCommand->setCommand($dumpBuilder->pipe($importCommand));
return $sshCommand->generate($shellBuilder);
$sshCommand->setCommand($this->prependPipefail($dumpBuilder->pipe($importCommand)));
return $this->prependPipefail($sshCommand->generate($shellBuilder));
}

public function prependPipefail(ShellInterface $shell): ShellBuilder
{
return ShellBuilder::command('set')->addShortOption('o', 'pipefail', false)->addToBuilder()->and($shell);
}

private function getRemoveDefinerCommand(): ShellInterface
Expand Down
14 changes: 7 additions & 7 deletions tests/Command/CommandGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function testProductionToLocalFromAnyThere(): void
$this->assertSame([
'filesystem:fileadmin' => "rsync -az -e 'ssh -F '\''php://temp'\''' 'serverEu:/var/www/production/fileadmin2/' './fileadmin/'",
'filesystem:uploads' => "rsync -az -e 'ssh -F '\''php://temp'\''' 'serverEu:/var/www/production/uploads/' './uploads/'",
'database:app' => "ssh -F 'php://temp' 'serverEu' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat)' | mysql --host='host' --port=3307 --user='user' --password='pw'",
'database:app' => "set -o pipefail && ssh -F 'php://temp' 'serverEu' 'set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat)' | mysql --host='host' --port=3307 --user='user' --password='pw'",
], $result);
$expectedSshConfigString = <<<'SSH_CONFIG'
Host serverEu
Expand Down Expand Up @@ -186,7 +186,7 @@ public function testProductionToTestingFromAnyThere(): void
$this->assertSame([
'filesystem:fileadmin' => "ssh -F 'php://temp' 'serverEu' 'rsync -az '\''/var/www/production/fileadmin2/'\'' '\''/var/www/testing/fileadmin/'\'''",
'filesystem:uploads' => "ssh -F 'php://temp' 'serverEu' 'rsync -az '\''/var/www/production/uploads/'\'' '\''/var/www/testing/uploads/'\'''",
'database:app' => "ssh -F 'php://temp' 'serverEu' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat) | mysql --host='\''host'\'' --port=3307 --user='\''user'\'' --password='\''pw'\'''",
'database:app' => "set -o pipefail && ssh -F 'php://temp' 'serverEu' 'set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat) | mysql --host='\''host'\'' --port=3307 --user='\''user'\'' --password='\''pw'\'''",
], $result);
$expectedSshConfigString = <<<'SSH_CONFIG'
Host serverEu
Expand Down Expand Up @@ -216,7 +216,7 @@ public function testLocalToLocal2FromAnyThere(): void
$this->assertSame([
'filesystem:fileadmin' => "rsync -az './fileadmin/' '../local2/fileadmin/'",
'filesystem:uploads' => "rsync -az './uploads/' '../local2/uploads/'",
'database:app' => "mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='host' --port=3307 --user='user' --password='pw' 'database' | (echo 'CREATE DATABASE IF NOT EXISTS `database`;USE `database`;' && cat) | mysql --host='host' --port=3307 --user='user' --password='pw'",
'database:app' => "set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='host' --port=3307 --user='user' --password='pw' 'database' | (echo 'CREATE DATABASE IF NOT EXISTS `database`;USE `database`;' && cat) | mysql --host='host' --port=3307 --user='user' --password='pw'",
], $result);
$expectedSshConfigString = <<<'SSH_CONFIG'
Host serverEu
Expand Down Expand Up @@ -246,7 +246,7 @@ public function testProductionToStagingFromAnyThere(): void
$this->assertSame([
'filesystem:fileadmin' => "rsync -az -e 'ssh -F '\''php://temp'\''' 'serverEu:/var/www/production/fileadmin2/' 'stagingServer:/var/www/staging/fileadmin/'",
'filesystem:uploads' => "rsync -az -e 'ssh -F '\''php://temp'\''' 'serverEu:/var/www/production/uploads/' 'stagingServer:/var/www/staging/uploads/'",
'database:app' => "ssh -F 'php://temp' 'serverEu' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat)' | ssh -F 'php://temp' 'stagingServer' 'mysql --host='\''host'\'' --port=3307 --user='\''user'\'' --password='\''pw'\'''",
'database:app' => "set -o pipefail && ssh -F 'php://temp' 'serverEu' 'set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat)' | ssh -F 'php://temp' 'stagingServer' 'set -o pipefail && mysql --host='\''host'\'' --port=3307 --user='\''user'\'' --password='\''pw'\'''",
], $result);
$expectedSshConfigString = <<<'SSH_CONFIG'
Host serverEu
Expand Down Expand Up @@ -276,7 +276,7 @@ public function testProductionToStagingFromStaging(): void
$this->assertSame([
'filesystem:fileadmin' => "rsync -az -e 'ssh -F '\''php://temp'\''' 'serverEu:/var/www/production/fileadmin2/' '/var/www/staging/fileadmin/'",
'filesystem:uploads' => "rsync -az -e 'ssh -F '\''php://temp'\''' 'serverEu:/var/www/production/uploads/' '/var/www/staging/uploads/'",
'database:app' => "ssh -F 'php://temp' 'serverEu' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat)' | mysql --host='host' --port=3307 --user='user' --password='pw'",
'database:app' => "set -o pipefail && ssh -F 'php://temp' 'serverEu' 'set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'' '\''appDatabase'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `database`;USE `database`;'\'' && cat)' | mysql --host='host' --port=3307 --user='user' --password='pw'",
], $result);
$expectedSshConfigString = <<<'SSH_CONFIG'
Host serverEu
Expand Down Expand Up @@ -312,7 +312,7 @@ public function testStagingToProductionFromStaging(): void
$this->assertSame([
'filesystem:fileadmin' => "rsync -az -e 'ssh -F '\''php://temp'\''' '/var/www/staging/fileadmin/' 'serverEu:/var/www/production/fileadmin2/'",
'filesystem:uploads' => "rsync -az -e 'ssh -F '\''php://temp'\''' '/var/www/staging/uploads/' 'serverEu:/var/www/production/uploads/'",
'database:app' => "mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='host' --port=3307 --user='user' --password='pw' 'database' | (echo 'CREATE DATABASE IF NOT EXISTS `appDatabase`;USE `appDatabase`;' && cat) | ssh -F 'php://temp' 'serverEu' 'mysql --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'''",
'database:app' => "set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='host' --port=3307 --user='user' --password='pw' 'database' | (echo 'CREATE DATABASE IF NOT EXISTS `appDatabase`;USE `appDatabase`;' && cat) | ssh -F 'php://temp' 'serverEu' 'set -o pipefail && mysql --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'''",
], $result);
$expectedSshConfigString = <<<'SSH_CONFIG'
Host serverEu
Expand Down Expand Up @@ -343,7 +343,7 @@ public function testGzipCompression(): void
$this->assertSame([
'filesystem:fileadmin' => "rsync -az -e 'ssh -F '\''php://temp'\''' '/var/www/staging/fileadmin/' 'serverEu:/var/www/production/fileadmin2/'",
'filesystem:uploads' => "rsync -az -e 'ssh -F '\''php://temp'\''' '/var/www/staging/uploads/' 'serverEu:/var/www/production/uploads/'",
'database:app' => "mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='host' --port=3307 --user='user' --password='pw' 'database' | (echo 'CREATE DATABASE IF NOT EXISTS `appDatabase`;USE `appDatabase`;' && cat) | gzip | ssh -F 'php://temp' 'serverEu' 'gunzip | mysql --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'''",
'database:app' => "set -o pipefail && mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='host' --port=3307 --user='user' --password='pw' 'database' | (echo 'CREATE DATABASE IF NOT EXISTS `appDatabase`;USE `appDatabase`;' && cat) | gzip | ssh -F 'php://temp' 'serverEu' 'set -o pipefail && gunzip | mysql --host='\''appHost'\'' --user='\''root'\'' --password='\''root'\'''",
], $result);
}

Expand Down
Loading

0 comments on commit b1828af

Please sign in to comment.