Skip to content

Commit

Permalink
add doNotDumpData() for MySQL and Postgres (#208)
Browse files Browse the repository at this point in the history
* add doNotDumpData() for MySQL and Postgres

* Update README.md

---------

Co-authored-by: Freek Van der Herten <[email protected]>
  • Loading branch information
jbraband and freekmurze authored Apr 24, 2024
1 parent 6de6dcd commit 519b558
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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`
Expand Down
13 changes: 13 additions & 0 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class MySql extends DbDumper

protected bool $createTables = true;

protected bool $includeData = true;

/** @var false|resource */
private $tempFileHandle;

Expand Down Expand Up @@ -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();
Expand All @@ -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';
}
Expand Down
13 changes: 13 additions & 0 deletions src/Databases/PostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class PostgreSql extends DbDumper

protected bool $createTables = true;

protected bool $includeData = true;

/** @var false|resource */
private $tempFileHandle;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
);
});
11 changes: 11 additions & 0 deletions tests/PostgreSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
});

0 comments on commit 519b558

Please sign in to comment.