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
Changes from 1 commit
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
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