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 3 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
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;
riasvdv marked this conversation as resolved.
Show resolved Hide resolved
}

return $command.' > '.$dumpFile;
}
}
27 changes: 21 additions & 6 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,8 +59,13 @@ 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 */
Expand All @@ -66,8 +76,13 @@ public function it_can_generate_a_dump_command_with_absolute_path_having_space_a
->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('if output=$(\'mongodump\' --db dbname --archive --host localhost --port 27017);
then
echo "$output" | gzip > "/save/to/new (directory)/dbname.gz"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand Down
24 changes: 21 additions & 3 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 @@ -72,7 +84,13 @@ public function it_can_generate_a_dump_command_with_absolute_path_having_space_a
->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('if output=$(\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname);
then
echo "$output" | gzip > "/save/to/new (directory)/dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi', $dumpCommand);
}

/** @test */
Expand Down
24 changes: 21 additions & 3 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 @@ -72,7 +84,13 @@ public function it_can_generate_a_dump_command_with_absolute_path_having_space_a
->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('if output=$(\'pg_dump\' -U username -h localhost -p 5432);
then
echo "$output" | gzip > "/save/to/new (directory)/dump.sql"
else
echo "Dump was not succesful." >&2
exit 1
fi', $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