From 539ee99227619f25a5dd4266ec995a25c391e2f8 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Tue, 30 May 2017 18:38:30 +0200 Subject: [PATCH 1/6] Apply fixes from StyleCI (#53) --- tests/SqlLiteTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SqlLiteTest.php b/tests/SqlLiteTest.php index c12247f..8e8ae05 100644 --- a/tests/SqlLiteTest.php +++ b/tests/SqlLiteTest.php @@ -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) From 73b0d217fe7a4c55b89ed3aa1a824aa65b8282f8 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Fri, 18 Aug 2017 08:46:47 -0300 Subject: [PATCH 2/6] add --databases option to mysqldump (#56) --- src/Databases/MySql.php | 5 +++-- tests/MySqlTest.php | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Databases/MySql.php b/src/Databases/MySql.php index 0ddffa9..7eec9df 100644 --- a/src/Databases/MySql.php +++ b/src/Databases/MySql.php @@ -171,10 +171,11 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil $command[] = "--result-file=\"{$dumpFile}\""; - $command[] = "{$this->dbName}"; + $command[] = "--databases {$this->dbName}"; if (! empty($this->includeTables)) { - $command[] = implode(' ', $this->includeTables); + $includeTables = implode(' ', $this->includeTables); + $command[] = "--tables {$includeTables}"; } return implode(' ', $command); diff --git a/tests/MySqlTest.php b/tests/MySqlTest.php index 0783595..3aec23c 100644 --- a/tests/MySqlTest.php +++ b/tests/MySqlTest.php @@ -32,7 +32,7 @@ public function it_can_generate_a_dump_command() ->setPassword('password') ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -45,7 +45,7 @@ public function it_can_generate_a_dump_command_without_using_comments() ->dontSkipComments() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --extended-insert --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -58,7 +58,7 @@ public function it_can_generate_a_dump_command_without_using_extended_insterts() ->dontUseExtendedInserts() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -71,7 +71,7 @@ public function it_can_generate_a_dump_command_with_custom_binary_path() ->setDumpBinaryPath('/custom/directory') ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'/custom/directory/mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'/custom/directory/mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -84,7 +84,7 @@ public function it_can_generate_a_dump_command_without_using_extending_inserts() ->dontUseExtendedInserts() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -97,7 +97,7 @@ public function it_can_generate_a_dump_command_using_single_transaction() ->useSingleTransaction() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --single-transaction --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --single-transaction --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -110,7 +110,7 @@ public function it_can_generate_a_dump_command_with_a_custom_socket() ->setSocket(1234) ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --socket=1234 --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --socket=1234 --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ @@ -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" --databases dbname --tables tb1 tb2 tb3', $dumpCommand); } /** @test */ @@ -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" --databases dbname --tables tb1 tb2 tb3', $dumpCommand); } /** @test */ @@ -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" --databases dbname', $dumpCommand); } /** @test */ @@ -177,7 +177,7 @@ 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" --databases dbname', $dumpCommand); } /** @test */ @@ -230,7 +230,7 @@ public function it_can_add_extra_options() ->addExtraOption('--another-extra-option="value"') ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --result-file="dump.sql" dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --result-file="dump.sql" --databases dbname', $dumpCommand); } /** @test */ From e3fdf7a5f545c2d6fe3c349a4c58394d03ba1c9b Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Fri, 18 Aug 2017 13:48:03 +0200 Subject: [PATCH 3/6] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaf3baf..b35e76a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `db-dumper` will be documented in this file +## 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 From 818a78cf6e2ea8380f29ea5ab708962123d48fe0 Mon Sep 17 00:00:00 2001 From: Matt Trask Date: Wed, 6 Sep 2017 12:22:55 -0500 Subject: [PATCH 4/6] Update .travis.yml added 7.2 to travis test suite --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index da7f7cd..b95d9ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 7.0 - 7.1 + - 7.2 env: matrix: From c3eaef45ff40495ba24a23b5a4e092adb7f37b06 Mon Sep 17 00:00:00 2001 From: Harish Toshniwal Date: Thu, 7 Sep 2017 04:29:58 +0530 Subject: [PATCH 5/6] Added PHP 7.2 (#59) From 22082be445e627285fbb7c7b0d3e75288f49e1f9 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Thu, 7 Sep 2017 09:11:04 -0300 Subject: [PATCH 6/6] make --databases optional as an extra option (#58) --- README.md | 17 +++++++- src/Databases/MySql.php | 17 +++++++- tests/MySqlTest.php | 89 +++++++++++++++++++++++++++++++++++------ 3 files changed, 108 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7b9e874..05210c4 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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. diff --git a/src/Databases/MySql.php b/src/Databases/MySql.php index 7eec9df..799f7b7 100644 --- a/src/Databases/MySql.php +++ b/src/Databases/MySql.php @@ -20,6 +20,9 @@ class MySql extends DbDumper /** @var string */ protected $defaultCharacterSet = ''; + /** @var bool */ + protected $dbNameWasSetAsExtraOption = false; + public function __construct() { $this->port = 3306; @@ -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. * @@ -171,7 +184,9 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil $command[] = "--result-file=\"{$dumpFile}\""; - $command[] = "--databases {$this->dbName}"; + if (! $this->dbNameWasSetAsExtraOption) { + $command[] = $this->dbName; + } if (! empty($this->includeTables)) { $includeTables = implode(' ', $this->includeTables); diff --git a/tests/MySqlTest.php b/tests/MySqlTest.php index 3aec23c..6d2894d 100644 --- a/tests/MySqlTest.php +++ b/tests/MySqlTest.php @@ -32,7 +32,7 @@ public function it_can_generate_a_dump_command() ->setPassword('password') ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -45,7 +45,7 @@ public function it_can_generate_a_dump_command_without_using_comments() ->dontSkipComments() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --extended-insert --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -58,7 +58,7 @@ public function it_can_generate_a_dump_command_without_using_extended_insterts() ->dontUseExtendedInserts() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -71,7 +71,7 @@ public function it_can_generate_a_dump_command_with_custom_binary_path() ->setDumpBinaryPath('/custom/directory') ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'/custom/directory/mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'/custom/directory/mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -84,7 +84,7 @@ public function it_can_generate_a_dump_command_without_using_extending_inserts() ->dontUseExtendedInserts() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -97,7 +97,7 @@ public function it_can_generate_a_dump_command_using_single_transaction() ->useSingleTransaction() ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --single-transaction --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --single-transaction --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -110,7 +110,7 @@ public function it_can_generate_a_dump_command_with_a_custom_socket() ->setSocket(1234) ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --socket=1234 --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --socket=1234 --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -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" --databases dbname --tables 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 */ @@ -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" --databases dbname --tables 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 */ @@ -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" --databases dbname', $dumpCommand); + '--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -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" --databases 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); @@ -230,7 +230,7 @@ public function it_can_add_extra_options() ->addExtraOption('--another-extra-option="value"') ->getDumpCommand('dump.sql', 'credentials.txt'); - $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --result-file="dump.sql" --databases dbname', $dumpCommand); + $this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --result-file="dump.sql" dbname', $dumpCommand); } /** @test */ @@ -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); + } }