Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap dump command in an if when using compression, fixes #89 #100

Merged
merged 5 commits into from May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"php" : "^7.1",
"php" : "^7.3",
"symfony/process": "^4.2"
},
"require-dev": {
Expand Down
20 changes: 15 additions & 5 deletions src/DbDumper.php
Expand Up @@ -257,12 +257,22 @@ protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile

protected function echoToFile(string $command, string $dumpFile): string
{
$compressor = $this->compressor
? ' | '.$this->compressor->useCommand()
: '';

$dumpFile = '"'.addcslashes($dumpFile, '\\"').'"';

return $command.$compressor.' > '.$dumpFile;
if ($this->compressor) {
$compressCommand = $this->compressor->useCommand();

return <<<BASH
if output=\$({$command});
then
echo "\$output" | $compressCommand > $dumpFile
else
echo "Dump was not succesful." >&2
exit 1
fi
BASH;
}

return $command.' > '.$dumpFile;
}
}
22 changes: 15 additions & 7 deletions tests/MongoDbTest.php
Expand Up @@ -42,8 +42,13 @@ public function it_can_generate_a_dump_command_with_compression_enabled()
->enableCompression()
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname'
.' --archive --host localhost --port 27017 | gzip > "dbname.gz"', $dumpCommand);
$this->assertSame('if output=$(\'mongodump\' --db dbname --archive --host localhost --port 27017);
riasvdv marked this conversation as resolved.
Show resolved Hide resolved
then
echo "$output" | gzip > "dbname.gz"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand All @@ -54,20 +59,23 @@ public function it_can_generate_a_dump_command_with_gzip_compressor_enabled()
->useCompressor(new GzipCompressor)
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname'
.' --archive --host localhost --port 27017 | gzip > "dbname.gz"', $dumpCommand);
$this->assertSame('if output=$(\'mongodump\' --db dbname --archive --host localhost --port 27017);
then
echo "$output" | gzip > "dbname.gz"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_absolute_path_having_space_and_brackets()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->useCompressor(new GzipCompressor)
->getDumpCommand('/save/to/new (directory)/dbname.gz');

$this->assertSame('\'mongodump\' --db dbname'
.' --archive --host localhost --port 27017 | gzip > "/save/to/new (directory)/dbname.gz"', $dumpCommand);
$this->assertSame('\'mongodump\' --db dbname --archive --host localhost --port 27017 > "/save/to/new (directory)/dbname.gz"', $dumpCommand);
}

/** @test */
Expand Down
19 changes: 15 additions & 4 deletions tests/MySqlTest.php
Expand Up @@ -46,7 +46,13 @@ public function it_can_generate_a_dump_command_with_compression_enabled()
->enableCompression()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname | gzip > "dump.sql"', $dumpCommand);
$this->assertSame('if output=$(\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname);
then
echo "$output" | gzip > "dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand All @@ -59,7 +65,13 @@ public function it_can_generate_a_dump_command_with_gzip_compressor_enabled()
->useCompressor(new GzipCompressor)
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname | gzip > "dump.sql"', $dumpCommand);
$this->assertSame('if output=$(\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname);
then
echo "$output" | gzip > "dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand All @@ -69,10 +81,9 @@ public function it_can_generate_a_dump_command_with_absolute_path_having_space_a
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->useCompressor(new GzipCompressor())
->getDumpCommand('/save/to/new (directory)/dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname | gzip > "/save/to/new (directory)/dump.sql"', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname > "/save/to/new (directory)/dump.sql"', $dumpCommand);
}

/** @test */
Expand Down
19 changes: 15 additions & 4 deletions tests/PostgreSqlTest.php
Expand Up @@ -46,7 +46,13 @@ public function it_can_generate_a_dump_command_with_compression_enabled()
->enableCompression()
->getDumpCommand('dump.sql');

$this->assertSame('\'pg_dump\' -U username -h localhost -p 5432 | gzip > "dump.sql"', $dumpCommand);
$this->assertSame('if output=$(\'pg_dump\' -U username -h localhost -p 5432);
then
echo "$output" | gzip > "dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand All @@ -59,7 +65,13 @@ public function it_can_generate_a_dump_command_with_gzip_compressor_enabled()
->useCompressor(new GzipCompressor)
->getDumpCommand('dump.sql');

$this->assertSame('\'pg_dump\' -U username -h localhost -p 5432 | gzip > "dump.sql"', $dumpCommand);
$this->assertSame('if output=$(\'pg_dump\' -U username -h localhost -p 5432);
then
echo "$output" | gzip > "dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand All @@ -69,10 +81,9 @@ public function it_can_generate_a_dump_command_with_absolute_path_having_space_a
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->useCompressor(new GzipCompressor)
->getDumpCommand('/save/to/new (directory)/dump.sql');

$this->assertSame('\'pg_dump\' -U username -h localhost -p 5432 | gzip > "/save/to/new (directory)/dump.sql"', $dumpCommand);
$this->assertSame('\'pg_dump\' -U username -h localhost -p 5432 > "/save/to/new (directory)/dump.sql"', $dumpCommand);
}

/** @test */
Expand Down
18 changes: 16 additions & 2 deletions tests/SqliteTest.php
Expand Up @@ -34,7 +34,14 @@ public function it_can_generate_a_dump_command_with_compression_enabled()
->enableCompression()
->getDumpCommand('dump.sql');

$expected = "echo 'BEGIN IMMEDIATE;\n.dump' | 'sqlite3' --bail 'dbname.sqlite' | gzip > \"dump.sql\"";
$expected = 'if output=$(echo \'BEGIN IMMEDIATE;
.dump\' | \'sqlite3\' --bail \'dbname.sqlite\');
then
echo "$output" | gzip > "dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi';

$this->assertEquals($expected, $dumpCommand);
}
Expand All @@ -47,7 +54,14 @@ public function it_can_generate_a_dump_command_with_gzip_compressor_enabled()
->useCompressor(new GzipCompressor)
->getDumpCommand('dump.sql');

$expected = "echo 'BEGIN IMMEDIATE;\n.dump' | 'sqlite3' --bail 'dbname.sqlite' | gzip > \"dump.sql\"";
$expected = 'if output=$(echo \'BEGIN IMMEDIATE;
.dump\' | \'sqlite3\' --bail \'dbname.sqlite\');
then
echo "$output" | gzip > "dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi';

$this->assertEquals($expected, $dumpCommand);
}
Expand Down