Skip to content

Commit

Permalink
feat: 🎉 Add Feature to Skip AUTO_INCREMENT Values in MySQL Dumps (#207)
Browse files Browse the repository at this point in the history
* feat: 🎉 Add Feature to Skip AUTO_INCREMENT Values in MySQL Dumps

* Creating tests for MySql Skip Autoincrement value

* Creating documentation for Skipping AUTO_INCREMENT Values
  • Loading branch information
jhonymiler committed Apr 8, 2024
1 parent 01760c4 commit d6519cd
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
47 changes: 41 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ For compressing dump files, `gzip` and/or `bzip2` should be installed.
## Installation

You can install the package via composer:
``` bash

```bash
composer require spatie/db-dumper
```

Expand Down Expand Up @@ -120,6 +121,36 @@ Spatie\DbDumper\Databases\MySql::create()
->dumpToFile('dump.sql');
```

### Handling AUTO_INCREMENT Values in Dumps

When creating a database dump, you might need to control the inclusion of AUTO_INCREMENT values. This can be crucial for avoiding primary key conflicts or for maintaining ID consistency when transferring data across environments.

#### Skipping AUTO_INCREMENT Values

To omit the AUTO_INCREMENT values from the tables in your dump, use the skipAutoIncrement method. This is particularly useful to prevent conflicts when importing the dump into another database where those specific AUTO_INCREMENT values might already exist, or when the exact values are not relevant.

```php
Spatie\DbDumper\Databases\MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->skipAutoIncrement()
->dumpToFile('dump.sql');
```

### Including AUTO_INCREMENT values in the dump

By default, the AUTO_INCREMENT values are included in the dump. However, if you previously used the skipAutoIncrement method and wish to ensure that the AUTO_INCREMENT values are included in a subsequent dump, use the dontSkipAutoIncrement method to explicitly include them.

```php
Spatie\DbDumper\Databases\MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->dontSkipAutoIncrement()
->dumpToFile('dump.sql');
```

### Use a Database URL

In some applications or environments, database credentials are provided as URLs instead of individual components. In this case, you can use the `setDatabaseUrl` method instead of the individual methods.
Expand All @@ -146,6 +177,7 @@ Spatie\DbDumper\Databases\MySql::create()
->includeTables(['table1', 'table2', 'table3'])
->dumpToFile('dump.sql');
```

Using a string:

```php
Expand Down Expand Up @@ -185,6 +217,7 @@ Spatie\DbDumper\Databases\MySql::create()
->excludeTables(['table1', 'table2', 'table3'])
->dumpToFile('dump.sql');
```

Using a string:

```php
Expand All @@ -197,6 +230,7 @@ Spatie\DbDumper\Databases\MySql::create()
```

### Do not write CREATE TABLE statements that create each dumped table.

```php
$dumpCommand = MySql::create()
->setDbName('dbname')
Expand All @@ -207,6 +241,7 @@ $dumpCommand = MySql::create()
```

### Adding extra options

If you want to add an arbitrary option to the dump command you can use `addExtraOption`

```php
Expand Down Expand Up @@ -247,8 +282,8 @@ If you want the output file to be compressed, you can use a compressor class.

There are two compressors that come out of the box:

- `GzipCompressor` - This will compress your db dump with `gzip`. Make sure `gzip` is installed on your system before using this.
- `Bzip2Compressor` - This will compress your db dump with `bzip2`. Make sure `bzip2` is installed on your system before using this.
- `GzipCompressor` - This will compress your db dump with `gzip`. Make sure `gzip` is installed on your system before using this.
- `Bzip2Compressor` - This will compress your db dump with `bzip2`. Make sure `bzip2` is installed on your system before using this.

```php
$dumpCommand = MySql::create()
Expand Down Expand Up @@ -295,7 +330,7 @@ class GzipCompressor implements Compressor

## Testing

``` bash
```bash
$ composer test
```

Expand All @@ -313,8 +348,8 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

Initial PostgreSQL support was contributed by [Adriano Machado](https://github.com/ammachado). SQlite support was contributed by [Peter Matseykanets](https://twitter.com/pmatseykanets).

Expand Down
24 changes: 23 additions & 1 deletion src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class MySql extends DbDumper

protected string $setGtidPurged = 'AUTO';

protected bool $skipAutoIncrement = false;

protected bool $createTables = true;

/** @var false|resource */
Expand Down Expand Up @@ -129,6 +131,18 @@ public function setGtidPurged(string $setGtidPurged): self
return $this;
}

public function skipAutoIncrement(): self
{
$this->skipAutoIncrement = true;
return $this;
}

public function dontSkipAutoIncrement(): self
{
$this->skipAutoIncrement = false;
return $this;
}

public function dumpToFile(string $dumpFile): void
{
$this->guardAgainstIncompleteCredentials();
Expand Down Expand Up @@ -233,7 +247,15 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil
$command[] = $extraOptionAfterDbName;
}

return $this->echoToFile(implode(' ', $command), $dumpFile);
$finalDumpCommand = implode(' ', $command);

if ($this->skipAutoIncrement) {
$sedCommand = "sed 's/ AUTO_INCREMENT=[0-9]*\b//'";
$finalDumpCommand .= " | {$sedCommand}";
}

return $this->echoToFile($finalDumpCommand, $dumpFile);

}

public function getContentsOfCredentialsFile(): string
Expand Down
22 changes: 22 additions & 0 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@
);
});

it('can generate a dump command skipping auto increment values', function () {
$dumpCommand = MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->skipAutoIncrement()
->getDumpCommand('dump.sql', 'credentials.txt');

expect($dumpCommand)->toContain("sed 's/ AUTO_INCREMENT=[0-9]*\\b//'");
});

it('can generate a dump command not skipping auto increment values', function () {
$dumpCommand = MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->dontSkipAutoIncrement()
->getDumpCommand('dump.sql', 'credentials.txt');

expect($dumpCommand)->not->toContain("sed 's/ AUTO_INCREMENT=[0-9]*\\b//'");
});

it('can generate a dump command for specific tables as string', function () {
$dumpCommand = MySql::create()
->setDbName('dbname')
Expand Down

0 comments on commit d6519cd

Please sign in to comment.