Skip to content

Commit

Permalink
Add ability to specify all databases as MySQL option (#93)
Browse files Browse the repository at this point in the history
* Add ability to specify all databases as MySQL option

* StyleCI

* Add documentation for feature
  • Loading branch information
Aaron Huisinga authored and freekmurze committed Mar 1, 2019
1 parent 4593c47 commit 3f7d4a0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class MySql extends DbDumper
/** @var bool */
protected $dbNameWasSetAsExtraOption = false;

/** @var bool */
protected $allDatabasesWasSetAsExtraOption = false;

/** @var string */
protected $setGtidPurged = 'AUTO';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 3f7d4a0

Please sign in to comment.