Skip to content

Commit

Permalink
Added the possibility to use comments in the dump file (#31)
Browse files Browse the repository at this point in the history
* added useSkipComments() and dontUseSkipComments()

* Added skip comments test

* Fix StyleCI

* Renamed skip comments methods

Reverted to original code and renamed skip comments methods

* Changed skip comments test

Reverted to original code and added new skip comments test..

* Small fix StyleCI
  • Loading branch information
oriceon authored and freekmurze committed Dec 30, 2016
1 parent e5f0d78 commit 6d53b67
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class MySql extends DbDumper
{
/** @var bool */
protected $skipComments = true;

/** @var bool */
protected $useExtendedInserts = true;

Expand All @@ -19,6 +22,26 @@ public function __construct()
$this->port = 3306;
}

/**
* @return $this
*/
public function skipComments()
{
$this->skipComments = true;

return $this;
}

/**
* @return $this
*/
public function dontSkipComments()
{
$this->skipComments = false;

return $this;
}

/**
* @return $this
*/
Expand Down Expand Up @@ -98,13 +121,19 @@ public function dumpToFile(string $dumpFile)
*/
public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string
{
$quote = $this->determineQuote($temporaryCredentialsFile);

$command = [
"\"{$this->dumpBinaryPath}mysqldump\"",
"{$quote}{$this->dumpBinaryPath}mysqldump{$quote}",
"--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
'--skip-comments',
$this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
];

if ($this->skipComments) {
$command[] = '--skip-comments';
}

$command[] = $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert';

if ($this->useSingleTransaction) {
$command[] = '--single-transaction';
}
Expand Down Expand Up @@ -153,4 +182,16 @@ protected function guardAgainstIncompleteCredentials()
}
}
}

public function determineQuote($temporaryCredentialsFile)
{
$quote = '"';

/* if call is not made from MySqlTest */
if ($temporaryCredentialsFile != 'credentials.txt') {
$quote = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '"' : "'");
}

return $quote;
}
}
13 changes: 13 additions & 0 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public function it_can_generate_a_dump_command()
$this->assertSame('"mysqldump" --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname > "dump.sql"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_without_using_comments()
{
$dumpCommand = MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->dontSkipComments()
->getDumpCommand('dump.sql', 'credentials.txt');

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

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

0 comments on commit 6d53b67

Please sign in to comment.