diff --git a/README.md b/README.md index 09ed77b..034efca 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,17 @@ $dumpCommand = MySql::create() ->getDumpCommand('dump.sql', 'credentials.txt'); ``` -Please note that using the `->addExtraOption('--databases dbname')` will override the database name set on a previous `->setDbName()` call. +With MySql, you also have the option to use the `--all-databases` extra option. This is useful when you want to run a full backup of all the databases in the specified MySQL connection. + +```php +$dumpCommand = MySql::create() + ->setUserName('username') + ->setPassword('password') + ->addExtraOption('--all-databases') + ->getDumpCommand('dump.sql', 'credentials.txt'); +``` + +Please note that using the `->addExtraOption('--databases dbname')` or `->addExtraOption('--all-databases')` will override the database name set on a previous `->setDbName()` call. ### Using compression If you want to compress the outputted file, you can use one of the compressors and the resulted dump file will be compressed. diff --git a/src/Databases/MySql.php b/src/Databases/MySql.php index cfde996..6395e9d 100644 --- a/src/Databases/MySql.php +++ b/src/Databases/MySql.php @@ -23,6 +23,9 @@ class MySql extends DbDumper /** @var bool */ protected $dbNameWasSetAsExtraOption = false; + /** @var bool */ + protected $allDatabasesWasSetAsExtraOption = false; + /** @var string */ protected $setGtidPurged = 'AUTO'; @@ -147,6 +150,11 @@ public function dumpToFile(string $dumpFile) public function addExtraOption(string $extraOption) { + if (strpos($extraOption, '--all-databases') !== false) { + $this->dbNameWasSetAsExtraOption = true; + $this->allDatabasesWasSetAsExtraOption = true; + } + if (preg_match('/^--databases (\S+)/', $extraOption, $matches) === 1) { $this->setDbName($matches[1]); $this->dbNameWasSetAsExtraOption = true; @@ -243,11 +251,15 @@ public function getContentsOfCredentialsFile(): string protected function guardAgainstIncompleteCredentials() { - foreach (['userName', 'dbName', 'host'] as $requiredProperty) { + foreach (['userName', 'host'] as $requiredProperty) { if (strlen($this->$requiredProperty) === 0) { throw CannotStartDump::emptyParameter($requiredProperty); } } + + if (strlen('dbName') === 0 && ! $this->allDatabasesWasSetAsExtraOption) { + throw CannotStartDump::emptyParameter($requiredProperty); + } } protected function determineQuote(): string diff --git a/tests/MySqlTest.php b/tests/MySqlTest.php index 10d6923..32644a2 100644 --- a/tests/MySqlTest.php +++ b/tests/MySqlTest.php @@ -316,6 +316,18 @@ public function it_can_get_the_name_of_the_db_when_dbname_was_overriden_as_an_ex $this->assertEquals($overridenDbName, $dbDumper->getDbName()); } + /** @test */ + public function it_can_get_the_name_of_the_db_when_all_databases_was_set_as_an_extra_option() + { + $dumpCommand = MySql::create() + ->setUserName('username') + ->setPassword('password') + ->addExtraOption('--all-databases') + ->getDumpCommand('dump.sql', 'credentials.txt'); + + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --all-databases > "dump.sql"', $dumpCommand); + } + /** @test */ public function it_can_generate_a_dump_command_excluding_tables_as_array_when_dbname_was_set_as_an_extra_option() {