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

Conversation

jhonymiler
Copy link
Contributor

This pull request introduces a new feature to the MySql dumper class, enabling users to exclude AUTO_INCREMENT values from their MySQL dumps. By adding a skipAutoIncrement boolean property along with two methods, skipAutoIncrement() and dontSkipAutoIncrement(), users can now toggle whether AUTO_INCREMENT values are included in the dump files. This flexibility is useful in scenarios where preserving AUTO_INCREMENT values is not necessary, such as when managing schema migrations or version controlling database structures.

Implementation Details:

A protected boolean property $skipAutoIncrement has been added to control whether AUTO_INCREMENT values are included in the dump.
The skipAutoIncrement() method sets $skipAutoIncrement to true, instructing the dumper to exclude AUTO_INCREMENT values from the output.
Conversely, the dontSkipAutoIncrement() method sets $skipAutoIncrement to false, including AUTO_INCREMENT values in the dump.
Within the getDumpCommand method, a conditional check on $skipAutoIncrement appends a sed command (sed 's/ AUTO_INCREMENT=[0-9]*\b//') to the mysqldump command. This sed command effectively removes any AUTO_INCREMENT declarations from the dump output.

Usage Example:

config/database.php

'dump' => [
     'skipAutoIncrement' => true,
     // or
     'dontSkipAutoIncrement' => true,
],

Copy link
Member

@freekmurze freekmurze left a comment

Choose a reason for hiding this comment

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

Nice!

Could you also add tests for this and update the readme.

Does this work for all supported DBs?

@@ -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.

@freekmurze freekmurze merged commit d6519cd into spatie:main Apr 8, 2024
7 checks passed
@freekmurze
Copy link
Member

Thanks for this quality PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants