Skip to content

Commit

Permalink
šŸ› mariadb added sandbox mode to the mysql-dump command https://mariadā€¦
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Jun 5, 2024
1 parent 0cf28c1 commit 4e86ff2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
23 changes: 19 additions & 4 deletions src/Command/DatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public function generate(ShellBuilder $shellBuilder): ShellBuilder
->addToBuilder()
->and('cat')
);
$removeSandboxModeCommand = $this->getRemoveSandboxModeCommand();
$removeDefinerCommand = $this->getRemoveDefinerCommand();
$compressCmd = $this->compression->getCompressCommand();
$unCompressCmd = $this->compression->getUnCompressCommand();
Expand All @@ -334,10 +335,15 @@ public function generate(ShellBuilder $shellBuilder): ShellBuilder
$to,
true
);
$dumpBuilder->if(
$this->fromDatabase->shouldDefinerBeRemoved(),
static fn(ShellBuilder $builder): ShellBuilder => $builder->pipe($removeDefinerCommand)
)
$dumpBuilder
->if(
!$this->fromDatabase->shouldAllowSandboxMode(),
static fn(ShellBuilder $builder): ShellBuilder => $builder->pipe($removeSandboxModeCommand)
)
->if(
$this->fromDatabase->shouldDefinerBeRemoved(),
static fn(ShellBuilder $builder): ShellBuilder => $builder->pipe($removeDefinerCommand)
)
->if($compressCmd !== '' && $compressCmd !== '0', static fn(ShellBuilder $builder): ShellBuilder => $builder->pipe($compressCmd));
if ($hostsDifferentiate) {
if ($this->fromHost !== '') {
Expand Down Expand Up @@ -393,6 +399,15 @@ private function getRemoveDefinerCommand(): ShellInterface
);
}

private function getRemoveSandboxModeCommand(): ShellInterface
{
return ShellBuilder::command('grep')
->addShortOption(
'v',
'enable the sandbox mode'
);
}

private function getDatabaseCreateStatement(string $targetDatabase): string
{
$escapedTargetDatabase = '`' . str_replace('`', '``', $targetDatabase) . '`';
Expand Down
16 changes: 16 additions & 0 deletions src/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ final class Database implements DockerTraitSupportInterface

private bool $noDefiner = true;

/**
* @var bool https://mariadb.org/mariadb-dump-file-compatibility-change/
*/
private bool $allowSandboxMode = false;

public function getName(): string
{
return $this->name;
Expand Down Expand Up @@ -94,4 +99,15 @@ public function setRemoveDefinerFromDump(bool $removeIt): Database
$this->noDefiner = $removeIt;
return $this;
}

public function shouldAllowSandboxMode(): bool
{
return $this->allowSandboxMode;
}

public function setAllowSandboxMode(bool $allowSandboxMode): Database
{
$this->allowSandboxMode = $allowSandboxMode;
return $this;
}
}
10 changes: 4 additions & 6 deletions tests/Command/CommandGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ private function getGlobalConfig(): GlobalConfig
$globalConfig = new GlobalConfig();
$globalConfig->addFilesystemObject((new FileSystem())->setName('fileadmin')->setPath('fileadmin'));
$globalConfig->addFilesystemObject((new FileSystem())->setName('uploads')->setPath('uploads'));
$globalConfig->addDatabaseObject((new Database())->setName('app')->setUrl('mysql://user:pw@host:3307/database')->setRemoveDefinerFromDump(false));
$globalConfig->addDatabaseObject((new Database())->setName('app')->setUrl('mysql://user:pw@host:3307/database')->setRemoveDefinerFromDump(false)->setAllowSandboxMode(true));
$globalConfig->addSshConnection('serverEu', 'user@server.eu');
$globalConfig->addSshConnectionObject((new SshConnection())->setHost('stagingServer')->setUrl('staging@stagingServer.server.eu'));
$globalConfig->addAppInstanceObject((new AppInstance())->setName('production')->setHost('serverEu')->setPath('/var/www/production')->addFilesystemObject(
(new FileSystem())->setName('fileadmin')->setPath('fileadmin2')
)->addDatabaseObject(
(new Database())->setName('app')->setUrl('mysql://root:root@appHost/appDatabase')->setRemoveDefinerFromDump(false)
));
$globalConfig->addAppInstanceObject((new AppInstance())->setName('production')->setHost('serverEu')->setPath('/var/www/production')
->addFilesystemObject((new FileSystem())->setName('fileadmin')->setPath('fileadmin2'))
->addDatabaseObject((new Database())->setName('app')->setUrl('mysql://root:root@appHost/appDatabase')->setRemoveDefinerFromDump(false)->setAllowSandboxMode(true)));
$globalConfig->addAppInstanceObject((new AppInstance())->setName('staging')->setHost('stagingServer')->setPath('/var/www/staging'));
$globalConfig->addAppInstanceObject((new AppInstance())->setName('testing')->setHost('serverEu')->setPath('/var/www/testing'));
$globalConfig->addAppInstanceObject((new AppInstance())->setName('local')->setHost('')->setPath('./'));
Expand Down
25 changes: 15 additions & 10 deletions tests/Command/DatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testDatabaseCommandGenerate(): void
->setToDatabase($toConnection)
->setToHost('');
$this->assertSame(
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -56,7 +56,7 @@ public function testDatabaseCommandWithoutDefinerInstructionsGenerate(): void
->setToDatabase($toConnection)
->setToHost('');
$this->assertSame(
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat) | sed -e '\''s/DEFINER[ ]*=[ ]*[^*]*\*/\*/; s/DEFINER[ ]*=[ ]*[^*]*PROCEDURE/PROCEDURE/; s/DEFINER[ ]*=[ ]*[^*]*FUNCTION/FUNCTION/'\''' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -68,7 +68,9 @@ public function testDatabaseCommandWithoutDefinerInstructionsLocallyGenerate():

$database = new DatabaseCommand();
$fromConnection = (new Database())->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@database/sequelmovie'));
$fromConnection->setRemoveDefinerFromDump(true);
$fromConnection
->setRemoveDefinerFromDump(true)
->setAllowSandboxMode(true);

$toConnection = (new Database())->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@127.0.0.1:2206/sequelmovie2'));
$database->setSshConfig($sshConfig)
Expand All @@ -91,6 +93,7 @@ public function testDatabaseCommandGenerateWithDocker(): void
$fromConnection = (new Database())
->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@database/sequelmovie'))
->setRemoveDefinerFromDump(false)
->setAllowSandboxMode(true)
->executeInDocker(true);
$toConnection = (new Database())->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@127.0.0.1:2206/sequelmovie2'));
$database->setSshConfig($sshConfig)
Expand Down Expand Up @@ -120,6 +123,7 @@ public function testDatabaseCommandGenerateWithDockerAndSudo(): void
$fromConnection = (new Database())
->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@database/sequelmovie'))
->setRemoveDefinerFromDump(false)
->setAllowSandboxMode(true)
->executeInDocker(true)
->enableSudoForDocker(true);
$toConnection = (new Database())->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@127.0.0.1:2206/sequelmovie2'));
Expand Down Expand Up @@ -154,6 +158,7 @@ public function testDatabaseCommandGenerateWithDockerOnBothSides(): void
$fromConnection = (new Database())
->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@database/sequelmovie'))
->setRemoveDefinerFromDump(false)
->setAllowSandboxMode(true)
->executeInDocker(true);
$toConnection = (new Database())
->setConnectionDetails(DatabaseConnectionDetails::fromUrlString('mysql://root:root@127.0.0.1:2206/sequelmovie2'))
Expand Down Expand Up @@ -255,7 +260,7 @@ public function testDatabaseCommandGzip(): void
->setCompression(new GzipCompression());

$this->assertSame(
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . " | gzip' | gunzip | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . " | gzip' | gunzip | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -276,7 +281,7 @@ public function testDatabaseCommandBzip2(): void
->setCompression(new Bzip2Compression());

$this->assertSame(
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . " | bzip2' | bunzip2 | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . " | bzip2' | bunzip2 | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -297,7 +302,7 @@ public function testDatabaseCommandQuiet(): void
->setVerbosity(OutputInterface::VERBOSITY_QUIET);

$this->assertSame(
"ssh -q -F 'php://temp' 'hostc' 'mysqldump -q " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -q -F 'php://temp' 'hostc' 'mysqldump -q " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -318,7 +323,7 @@ public function testDatabaseCommandVerbose(): void
->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);

$this->assertSame(
"ssh -v -F 'php://temp' 'hostc' 'mysqldump -v " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -v -F 'php://temp' 'hostc' 'mysqldump -v " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -339,7 +344,7 @@ public function testDatabaseCommandVeryVerbose(): void
->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);

$this->assertSame(
"ssh -vv -F 'php://temp' 'hostc' 'mysqldump -vv " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -vv -F 'php://temp' 'hostc' 'mysqldump -vv " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -360,7 +365,7 @@ public function testDatabaseCommandDebug(): void
->setVerbosity(OutputInterface::VERBOSITY_DEBUG);

$this->assertSame(
"ssh -vvv -F 'php://temp' 'hostc' 'mysqldump -vvv " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -vvv -F 'php://temp' 'hostc' 'mysqldump -vvv " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand All @@ -379,7 +384,7 @@ public function testDatabaseCommandPasswordWithSpecialCharacters(): void
->setToDatabase($toConnection)
->setToHost('');
$this->assertSame(
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root#password'\''\'\'''\''\"_!'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::REMOVE_DEFINER_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
"ssh -F 'php://temp' 'hostc' 'mysqldump " . ControllerTest::MYSQLDUMP_OPTIONS . " --host='\''database'\'' --user='\''root'\'' --password='\''root#password'\''\'\'''\''\"_!'\'' '\''sequelmovie'\'' | (echo '\''CREATE DATABASE IF NOT EXISTS `sequelmovie2`;USE `sequelmovie2`;'\'' && cat)" . ControllerTest::MYSQL_DUMP_MODIFICATION_PART . "' | mysql --host='127.0.0.1' --port=2206 --user='root' --password='root'",
(string)$database->generate(ShellBuilder::new())
);
}
Expand Down
Loading

0 comments on commit 4e86ff2

Please sign in to comment.