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

feat: 🎉 Add Feature to Skip AUTO_INCREMENT Values in MySQL Dumps #207

Merged
merged 3 commits into from
Apr 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a negative like skip might be confusing. Let's word this more positive: includeAutoIncrements and set it to true by default. Rename all other methods in this manner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered using a simple boolean variable, but I realized that it would deviate from the class's own standard, which implements other configuration methods in the format of setters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding other database types, the behavior for the dump is different. Only in MySQL do we have this issue that impacts the subsequent restoration of the dump. Generating the dump with a specified value for autoincrement causes a key conflict error, making it necessary to generate the dump without specifying autoincrement values.


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