Skip to content

Commit

Permalink
Allow for adding options after the db name (#129)
Browse files Browse the repository at this point in the history
* Allow for adding options after the db name

This is useful for being able to use commands such as SED to edit the stream prior to being written to the dump file ie remove auto-increment values

* silly lint fail

* Slight naming change as requested
  • Loading branch information
designvoid committed Apr 16, 2020
1 parent e167b8b commit e7a4d3c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil
$command[] = "--tables {$includeTables}";
}

foreach ($this->extraOptionsAfterDbName as $extraOptionAfterDbName) {
$command[] = $extraOptionAfterDbName;
}

return $this->echoToFile(implode(' ', $command), $dumpFile);
}

Expand Down
17 changes: 17 additions & 0 deletions src/DbDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ abstract class DbDumper
/** @var array */
protected $extraOptions = [];

/** @var array */
protected $extraOptionsAfterDbName = [];

/** @var object */
protected $compressor = null;

Expand Down Expand Up @@ -238,6 +241,20 @@ public function addExtraOption(string $extraOption)
return $this;
}

/**
* @param string $extraOptionAtEnd
*
* @return $this
*/
public function addExtraOptionAfterDbName(string $extraOptionAfterDbName)
{
if (! empty($extraOptionAfterDbName)) {
$this->extraOptionsAfterDbName[] = $extraOptionAfterDbName;
}

return $this;
}

abstract public function dumpToFile(string $dumpFile);

protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
Expand Down
16 changes: 15 additions & 1 deletion tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function it_can_generate_a_dump_command_without_using_comments()
}

/** @test */
public function it_can_generate_a_dump_command_without_using_extended_insterts()
public function it_can_generate_a_dump_command_without_using_extended_inserts()
{
$dumpCommand = MySql::create()
->setDbName('dbname')
Expand Down Expand Up @@ -302,6 +302,20 @@ public function it_can_add_extra_options()
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" dbname > "dump.sql"', $dumpCommand);
}

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

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

/** @test */
public function it_can_get_the_host()
{
Expand Down

0 comments on commit e7a4d3c

Please sign in to comment.