diff --git a/README.md b/README.md index af4e9c5..d091043 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ Spatie\DbDumper\Databases\MySql::create() ### Excluding tables from the dump -Using an array: +You can exclude tables from the dump by using an array: ```php Spatie\DbDumper\Databases\MySql::create() @@ -218,7 +218,7 @@ Spatie\DbDumper\Databases\MySql::create() ->dumpToFile('dump.sql'); ``` -Using a string: +Or by using a string: ```php Spatie\DbDumper\Databases\MySql::create() @@ -229,7 +229,9 @@ Spatie\DbDumper\Databases\MySql::create() ->dumpToFile('dump.sql'); ``` -### Do not write CREATE TABLE statements that create each dumped table. +### Do not write CREATE TABLE statements that create each dumped table + +You can use `doNotCreateTables` to prevent writing create statements. ```php $dumpCommand = MySql::create() @@ -240,6 +242,20 @@ $dumpCommand = MySql::create() ->getDumpCommand('dump.sql', 'credentials.txt'); ``` +### Do not write row data + +You can use `doNotDumpData` to prevent writing row data. + + +```php +$dumpCommand = MySql::create() + ->setDbName('dbname') + ->setUserName('username') + ->setPassword('password') + ->doNotDumpData() + ->getDumpCommand('dump.sql', 'credentials.txt'); +``` + ### Adding extra options If you want to add an arbitrary option to the dump command you can use `addExtraOption` diff --git a/src/Databases/MySql.php b/src/Databases/MySql.php index 4f2d56e..b60bc11 100644 --- a/src/Databases/MySql.php +++ b/src/Databases/MySql.php @@ -32,6 +32,8 @@ class MySql extends DbDumper protected bool $createTables = true; + protected bool $includeData = true; + /** @var false|resource */ private $tempFileHandle; @@ -181,6 +183,13 @@ public function doNotCreateTables(): self return $this; } + public function doNotDumpData(): self + { + $this->includeData = false; + + return $this; + } + public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string { $quote = $this->determineQuote(); @@ -194,6 +203,10 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil $command[] = '--no-create-info'; } + if (!$this->includeData) { + $command[] = '--no-data'; + } + if ($this->skipComments) { $command[] = '--skip-comments'; } diff --git a/src/Databases/PostgreSql.php b/src/Databases/PostgreSql.php index 2f6f857..d00218a 100644 --- a/src/Databases/PostgreSql.php +++ b/src/Databases/PostgreSql.php @@ -12,6 +12,8 @@ class PostgreSql extends DbDumper protected bool $createTables = true; + protected bool $includeData = true; + /** @var false|resource */ private $tempFileHandle; @@ -60,6 +62,10 @@ public function getDumpCommand(string $dumpFile): string $command[] = '--data-only'; } + if (!$this->includeData) { + $command[] = '--schema-only'; + } + foreach ($this->extraOptions as $extraOption) { $command[] = $extraOption; } @@ -120,6 +126,13 @@ public function doNotCreateTables(): self return $this; } + public function doNotDumpData(): self + { + $this->includeData = false; + + return $this; + } + public function getProcess(string $dumpFile): Process { $command = $this->getDumpCommand($dumpFile); diff --git a/tests/MySqlTest.php b/tests/MySqlTest.php index a333047..2d1db94 100644 --- a/tests/MySqlTest.php +++ b/tests/MySqlTest.php @@ -448,3 +448,17 @@ '\'mysqldump\' --defaults-extra-file="credentials.txt" --no-create-info --skip-comments --extended-insert dbname > "dump.sql"' ); }); + + +it('can generate a dump command with no data', function () { + $dumpCommand = MySQL::create() + ->setDbName('dbname') + ->setUserName('username') + ->setPassword('password') + ->doNotDumpData() + ->getDumpCommand('dump.sql', 'credentials.txt'); + + expect($dumpCommand)->toEqual( + '\'mysqldump\' --defaults-extra-file="credentials.txt" --no-data --skip-comments --extended-insert dbname > "dump.sql"' + ); +}); diff --git a/tests/PostgreSqlTest.php b/tests/PostgreSqlTest.php index e755b2e..2496fb8 100644 --- a/tests/PostgreSqlTest.php +++ b/tests/PostgreSqlTest.php @@ -231,3 +231,14 @@ expect($dumpCommand)->toEqual('\'pg_dump\' -U "username" -h localhost -p 5432 --data-only > "dump.sql"'); }); + +it('can generate a dump command with no data', function () { + $dumpCommand = PostgreSql::create() + ->setDbName('dbname') + ->setUserName('username') + ->setPassword('password') + ->doNotDumpData() + ->getDumpCommand('dump.sql'); + + expect($dumpCommand)->toEqual('\'pg_dump\' -U "username" -h localhost -p 5432 --schema-only > "dump.sql"'); +});