Skip to content

Commit

Permalink
edit changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Sep 7, 2017
2 parents ff82ea7 + 22082be commit 669045f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 7.0
- 7.1
- 7.2

env:
matrix:
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

All notable changes to `db-dumper` will be documented in this file

## 2.8.0 - 2017-09-07
## 2.7.2 - 2017-09-07

- make `--databases` optional

## 2.7.1 - 2017-08-18

- made option passing more flexible by adding `--databases` option to the MySQL dumper

## 2.7.0 - 2017-04-13

- `MongoDb` dumps won't be compressed by default anymore
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Spatie\DbDumper\Databases\MySql::create()


### Adding extra options
If you want to add an arbitrary option to the dump command you can use `addOption`
If you want to add an arbitrary option to the dump command you can use `addExtraOption`

```php
$dumpCommand = MySql::create()
Expand All @@ -170,6 +170,21 @@ $dumpCommand = MySql::create()
->getDumpCommand('dump.sql', 'credentials.txt');
```

If you're working with MySql you can set the database name using `--databases` as an extra option. This is particularly useful when used in conjunction with the `--add-drop-database` `mysqldump` option (see the [mysqldump docs](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_add-drop-database)).

```php
$dumpCommand = MySql::create()
->setUserName('username')
->setPassword('password')
->addExtraOption('--databases dbname')
->addExtraOption('--add-drop-database')
->getDumpCommand('dump.sql', 'credentials.txt');
```

Please note that using the `->addExtraOption('--databases dbname')` will override the database name set on a previous `->setDbName()` call.



## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
20 changes: 18 additions & 2 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MySql extends DbDumper
/** @var string */
protected $defaultCharacterSet = '';

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

public function __construct()
{
$this->port = 3306;
Expand Down Expand Up @@ -126,6 +129,16 @@ public function dumpToFile(string $dumpFile)
$this->checkIfDumpWasSuccessFul($process, $dumpFile);
}

public function addExtraOption(string $extraOption)
{
if (preg_match('/^--databases (\S+)/', $extraOption, $matches) === 1) {
$this->setDbName($matches[1]);
$this->dbNameWasSetAsExtraOption = true;
}

return parent::addExtraOption($extraOption);
}

/**
* Get the command that should be performed to dump the database.
*
Expand Down Expand Up @@ -171,10 +184,13 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil

$command[] = "--result-file=\"{$dumpFile}\"";

$command[] = "{$this->dbName}";
if (! $this->dbNameWasSetAsExtraOption) {
$command[] = $this->dbName;
}

if (! empty($this->includeTables)) {
$command[] = implode(' ', $this->includeTables);
$includeTables = implode(' ', $this->includeTables);
$command[] = "--tables {$includeTables}";
}

return implode(' ', $command);
Expand Down
73 changes: 68 additions & 5 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function it_can_generate_a_dump_command_for_specific_tables_as_array()
->includeTables(['tb1', 'tb2', 'tb3'])
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname tb1 tb2 tb3', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname --tables tb1 tb2 tb3', $dumpCommand);
}

/** @test */
Expand All @@ -136,7 +136,7 @@ public function it_can_generate_a_dump_command_for_specific_tables_as_string()
->includeTables('tb1 tb2 tb3')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname tb1 tb2 tb3', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname --tables tb1 tb2 tb3', $dumpCommand);
}

/** @test */
Expand All @@ -163,7 +163,7 @@ public function it_can_generate_a_dump_command_excluding_tables_as_array()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand);
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand);
}

/** @test */
Expand All @@ -177,11 +177,11 @@ public function it_can_generate_a_dump_command_excluding_tables_as_string()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand);
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand);
}

/** @test */
public function it_will_throw_an_exception_when_setting_tables_after_setting_esclude_tables()
public function it_will_throw_an_exception_when_setting_tables_after_setting_exclude_tables()
{
$this->expectException(CannotSetParameter::class);

Expand Down Expand Up @@ -240,4 +240,67 @@ public function it_can_get_the_host()

$this->assertEquals('myHost', $dumper->getHost());
}

/** @test */
public function it_can_set_db_name_as_an_extra_options()
{
$dumpCommand = MySql::create()
->setUserName('username')
->setPassword('password')
->addExtraOption('--extra-option')
->addExtraOption('--another-extra-option="value"')
->addExtraOption('--databases dbname')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --databases dbname --result-file="dump.sql"', $dumpCommand);
}

/** @test */
public function it_can_get_the_name_of_the_db_when_dbname_was_set_as_an_extra_option()
{
$dbName = 'testName';

$dbDumper = MySql::create()->addExtraOption("--databases {$dbName}");

$this->assertEquals($dbName, $dbDumper->getDbName());
}

/** @test */
public function it_can_get_the_name_of_the_db_when_dbname_was_overriden_as_an_extra_option()
{
$dbName = 'testName';
$overridenDbName = 'otherName';

$dbDumper = MySql::create()->setDbName($dbName)->addExtraOption("--databases {$overridenDbName}");

$this->assertEquals($overridenDbName, $dbDumper->getDbName());
}

/** @test */
public function it_can_generate_a_dump_command_excluding_tables_as_array_when_dbname_was_set_as_an_extra_option()
{
$dumpCommand = MySql::create()
->setUserName('username')
->setPassword('password')
->addExtraOption('--databases dbname')
->excludeTables(['tb1', 'tb2', 'tb3'])
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --databases dbname --result-file="dump.sql"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_excluding_tables_as_string_when_dbname_was_set_as_an_extra_option()
{
$dumpCommand = MySql::create()
->setUserName('username')
->setPassword('password')
->addExtraOption('--databases dbname')
->excludeTables('tb1, tb2, tb3')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --databases dbname --result-file="dump.sql"', $dumpCommand);
}
}
4 changes: 2 additions & 2 deletions tests/SqlLiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function it_can_generate_a_dump_command_with_absolute_paths()
/** @test */
public function it_successfully_creates_a_backup()
{
$dbPath = __DIR__ . '/stubs/database.sqlite';
$dbBackupPath = __DIR__ . '/temp/backup.sql';
$dbPath = __DIR__.'/stubs/database.sqlite';
$dbBackupPath = __DIR__.'/temp/backup.sql';

Sqlite::create()
->setDbName($dbPath)
Expand Down

0 comments on commit 669045f

Please sign in to comment.