-
Notifications
You must be signed in to change notification settings - Fork 121
Add ALGORITHM and LOCK support for MySQL ALTER TABLE operations #955
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
Open
dereuromark
wants to merge
2
commits into
5.x
Choose a base branch
from
5.x-alg
base: 5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,18 @@ | |
| use Cake\Database\Schema\SchemaDialect; | ||
| use Cake\Database\Schema\TableSchema; | ||
| use InvalidArgumentException; | ||
| use Migrations\Db\Action\AddColumn; | ||
| use Migrations\Db\Action\AddForeignKey; | ||
| use Migrations\Db\Action\AddIndex; | ||
| use Migrations\Db\Action\ChangeColumn; | ||
| use Migrations\Db\Action\ChangeComment; | ||
| use Migrations\Db\Action\ChangePrimaryKey; | ||
| use Migrations\Db\Action\DropForeignKey; | ||
| use Migrations\Db\Action\DropIndex; | ||
| use Migrations\Db\Action\DropTable; | ||
| use Migrations\Db\Action\RemoveColumn; | ||
| use Migrations\Db\Action\RenameColumn; | ||
| use Migrations\Db\Action\RenameTable; | ||
| use Migrations\Db\AlterInstructions; | ||
| use Migrations\Db\Table\CheckConstraint; | ||
| use Migrations\Db\Table\Column; | ||
|
|
@@ -107,6 +119,77 @@ class MysqlAdapter extends AbstractAdapter | |
|
|
||
| public const FIRST = 'FIRST'; | ||
|
|
||
| /** | ||
| * MySQL ALTER TABLE ALGORITHM options | ||
| * | ||
| * These constants control how MySQL performs ALTER TABLE operations: | ||
| * - ALGORITHM_DEFAULT: Let MySQL choose the best algorithm | ||
| * - ALGORITHM_INSTANT: Instant operation (no table copy, MySQL 8.0+ / MariaDB 10.3+) | ||
| * - ALGORITHM_INPLACE: In-place operation (no full table copy) | ||
| * - ALGORITHM_COPY: Traditional table copy algorithm | ||
| * | ||
| * Usage: | ||
| * ```php | ||
| * use Migrations\Db\Adapter\MysqlAdapter; | ||
| * | ||
| * // ALGORITHM=INSTANT alone (recommended) | ||
| * $table->addColumn('status', 'string', [ | ||
| * 'null' => true, | ||
| * 'algorithm' => MysqlAdapter::ALGORITHM_INSTANT, | ||
| * ]); | ||
| * | ||
| * // Or with ALGORITHM=INPLACE and explicit LOCK | ||
| * $table->addColumn('status', 'string', [ | ||
| * 'algorithm' => MysqlAdapter::ALGORITHM_INPLACE, | ||
| * 'lock' => MysqlAdapter::LOCK_NONE, | ||
| * ]); | ||
| * ``` | ||
| * | ||
| * Important: ALGORITHM=INSTANT cannot be combined with LOCK=NONE, LOCK=SHARED, | ||
| * or LOCK=EXCLUSIVE (MySQL restriction). Use ALGORITHM=INSTANT alone or with | ||
| * LOCK=DEFAULT only. | ||
| * | ||
| * Note: ALGORITHM_INSTANT requires MySQL 8.0+ or MariaDB 10.3+ and only works for | ||
| * compatible operations (adding nullable columns, dropping columns, etc.). | ||
| * If the operation cannot be performed instantly, MySQL will return an error. | ||
| * | ||
| * @see https://dev.mysql.com/doc/refman/8.0/en/alter-table.html | ||
| * @see https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html | ||
| * @see https://mariadb.com/kb/en/alter-table/#algorithm | ||
| */ | ||
| public const ALGORITHM_DEFAULT = 'DEFAULT'; | ||
| public const ALGORITHM_INSTANT = 'INSTANT'; | ||
| public const ALGORITHM_INPLACE = 'INPLACE'; | ||
| public const ALGORITHM_COPY = 'COPY'; | ||
|
|
||
| /** | ||
| * MySQL ALTER TABLE LOCK options | ||
| * | ||
| * These constants control the locking behavior during ALTER TABLE operations: | ||
| * - LOCK_DEFAULT: Let MySQL choose the appropriate lock level | ||
| * - LOCK_NONE: Allow concurrent reads and writes (least restrictive) | ||
| * - LOCK_SHARED: Allow concurrent reads, block writes | ||
| * - LOCK_EXCLUSIVE: Block all concurrent access (most restrictive) | ||
| * | ||
| * Usage: | ||
| * ```php | ||
| * use Migrations\Db\Adapter\MysqlAdapter; | ||
| * | ||
| * $table->changeColumn('name', 'string', [ | ||
| * 'limit' => 500, | ||
| * 'algorithm' => MysqlAdapter::ALGORITHM_INPLACE, | ||
| * 'lock' => MysqlAdapter::LOCK_NONE, | ||
| * ]); | ||
| * ``` | ||
| * | ||
| * @see https://dev.mysql.com/doc/refman/8.0/en/alter-table.html | ||
| * @see https://mariadb.com/kb/en/alter-table/#lock | ||
| */ | ||
| public const LOCK_DEFAULT = 'DEFAULT'; | ||
| public const LOCK_NONE = 'NONE'; | ||
| public const LOCK_SHARED = 'SHARED'; | ||
| public const LOCK_EXCLUSIVE = 'EXCLUSIVE'; | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
|
|
@@ -1164,4 +1247,254 @@ protected function isMariaDb(): bool | |
|
|
||
| return stripos($version, 'mariadb') !== false; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * Overridden to support ALGORITHM and LOCK clauses for MySQL ALTER TABLE operations. | ||
| * | ||
| * @throws \InvalidArgumentException | ||
| * @return void | ||
| */ | ||
| public function executeActions(TableMetadata $table, array $actions): void | ||
| { | ||
| // Extract algorithm and lock specifications from all actions | ||
| $algorithm = null; | ||
| $lock = null; | ||
|
|
||
| foreach ($actions as $action) { | ||
| if (!method_exists($action, 'getColumn')) { | ||
| continue; | ||
| } | ||
|
|
||
| $column = $action->getColumn(); | ||
| if (!($column instanceof Column)) { | ||
| continue; | ||
| } | ||
|
|
||
| $colAlgorithm = $column->getAlgorithm(); | ||
| $colLock = $column->getLock(); | ||
|
|
||
| if ($colAlgorithm !== null) { | ||
| if ($algorithm !== null && $algorithm !== $colAlgorithm) { | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Conflicting algorithm specifications in batched operations: "%s" and "%s". ' . | ||
| 'All operations in a batch must use the same algorithm, or specify it on only one operation.', | ||
| $algorithm, | ||
| $colAlgorithm, | ||
| )); | ||
| } | ||
| $algorithm = $colAlgorithm; | ||
| } | ||
|
|
||
| if ($colLock !== null) { | ||
| if ($lock !== null && $lock !== $colLock) { | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Conflicting lock specifications in batched operations: "%s" and "%s". ' . | ||
| 'All operations in a batch must use the same lock mode, or specify it on only one operation.', | ||
| $lock, | ||
| $colLock, | ||
| )); | ||
| } | ||
| $lock = $colLock; | ||
| } | ||
| } | ||
|
|
||
| // If no algorithm/lock specified, use parent implementation | ||
| if ($algorithm === null && $lock === null) { | ||
| parent::executeActions($table, $actions); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Otherwise, execute with custom algorithm/lock support | ||
| $this->executeActionsWithAlgorithmAndLock($table, $actions, $algorithm, $lock); | ||
| } | ||
|
|
||
| /** | ||
| * Executes actions with ALGORITHM and LOCK clauses. | ||
| * | ||
| * @param \Migrations\Db\Table\TableMetadata $table The table metadata | ||
| * @param array $actions The actions to execute | ||
| * @param string|null $algorithm The algorithm to use | ||
| * @param string|null $lock The lock mode to use | ||
| * @throws \InvalidArgumentException | ||
| * @return void | ||
| */ | ||
| protected function executeActionsWithAlgorithmAndLock( | ||
| TableMetadata $table, | ||
| array $actions, | ||
| ?string $algorithm, | ||
| ?string $lock, | ||
| ): void { | ||
| $instructions = new AlterInstructions(); | ||
|
|
||
| // Build instructions (copied from AbstractAdapter::executeActions) | ||
| foreach ($actions as $action) { | ||
| switch (true) { | ||
| case $action instanceof AddColumn: | ||
| $instructions->merge($this->getAddColumnInstructions($table, $action->getColumn())); | ||
| break; | ||
|
|
||
| case $action instanceof AddIndex: | ||
| $instructions->merge($this->getAddIndexInstructions($table, $action->getIndex())); | ||
| break; | ||
|
|
||
| case $action instanceof AddForeignKey: | ||
| $instructions->merge($this->getAddForeignKeyInstructions($table, $action->getForeignKey())); | ||
| break; | ||
|
|
||
| case $action instanceof ChangeColumn: | ||
| $instructions->merge($this->getChangeColumnInstructions( | ||
| $table->getName(), | ||
| $action->getColumnName(), | ||
| $action->getColumn(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof DropForeignKey && !$action->getForeignKey()->getName(): | ||
| $instructions->merge($this->getDropForeignKeyByColumnsInstructions( | ||
| $table->getName(), | ||
| $action->getForeignKey()->getColumns(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof DropForeignKey && $action->getForeignKey()->getName(): | ||
| $instructions->merge($this->getDropForeignKeyInstructions( | ||
| $table->getName(), | ||
| (string)$action->getForeignKey()->getName(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof DropIndex && $action->getIndex()->getName(): | ||
| $instructions->merge($this->getDropIndexByNameInstructions( | ||
| $table->getName(), | ||
| (string)$action->getIndex()->getName(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof DropIndex && !$action->getIndex()->getName(): | ||
| $instructions->merge($this->getDropIndexByColumnsInstructions( | ||
| $table->getName(), | ||
| (array)$action->getIndex()->getColumns(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof DropTable: | ||
| $instructions->merge($this->getDropTableInstructions($table->getName())); | ||
| break; | ||
|
|
||
| case $action instanceof RemoveColumn: | ||
| $instructions->merge($this->getDropColumnInstructions( | ||
| $table->getName(), | ||
| (string)$action->getColumn()->getName(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof RenameColumn: | ||
| $instructions->merge($this->getRenameColumnInstructions( | ||
| $table->getName(), | ||
| (string)$action->getColumn()->getName(), | ||
| $action->getNewName(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof RenameTable: | ||
| $instructions->merge($this->getRenameTableInstructions( | ||
| $table->getName(), | ||
| $action->getNewName(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof ChangePrimaryKey: | ||
| $instructions->merge($this->getChangePrimaryKeyInstructions( | ||
| $table, | ||
| $action->getNewColumns(), | ||
| )); | ||
| break; | ||
|
|
||
| case $action instanceof ChangeComment: | ||
| $instructions->merge($this->getChangeCommentInstructions( | ||
| $table, | ||
| $action->getNewComment(), | ||
| )); | ||
| break; | ||
|
|
||
| default: | ||
| throw new InvalidArgumentException( | ||
| sprintf("Don't know how to execute action `%s`", get_class($action)), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Build ALTER TABLE template with algorithm and lock | ||
| $alterTemplate = sprintf('ALTER TABLE %s %%s', $this->quoteTableName($table->getName())); | ||
|
|
||
| // Add algorithm and lock clauses | ||
| $algorithmLockClause = ''; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't we be missing lock/algorithm when |
||
| $upperAlgorithm = null; | ||
| $upperLock = null; | ||
|
|
||
| if ($algorithm !== null) { | ||
| $upperAlgorithm = strtoupper($algorithm); | ||
| $validAlgorithms = [ | ||
| self::ALGORITHM_DEFAULT, | ||
| self::ALGORITHM_INSTANT, | ||
| self::ALGORITHM_INPLACE, | ||
| self::ALGORITHM_COPY, | ||
| ]; | ||
| if (!in_array($upperAlgorithm, $validAlgorithms, true)) { | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Invalid algorithm "%s". Valid options: %s', | ||
| $algorithm, | ||
| implode(', ', $validAlgorithms), | ||
| )); | ||
| } | ||
| $algorithmLockClause .= ', ALGORITHM=' . $upperAlgorithm; | ||
| } | ||
|
|
||
| if ($lock !== null) { | ||
| $upperLock = strtoupper($lock); | ||
| $validLocks = [ | ||
| self::LOCK_DEFAULT, | ||
| self::LOCK_NONE, | ||
| self::LOCK_SHARED, | ||
| self::LOCK_EXCLUSIVE, | ||
| ]; | ||
| if (!in_array($upperLock, $validLocks, true)) { | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Invalid lock "%s". Valid options: %s', | ||
| $lock, | ||
| implode(', ', $validLocks), | ||
| )); | ||
| } | ||
| $algorithmLockClause .= ', LOCK=' . $upperLock; | ||
| } | ||
|
|
||
| // MySQL restriction: ALGORITHM=INSTANT cannot be combined with explicit LOCK modes | ||
| // except LOCK=DEFAULT. See: https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html | ||
| if ($upperAlgorithm === self::ALGORITHM_INSTANT && $upperLock !== null && $upperLock !== self::LOCK_DEFAULT) { | ||
| throw new InvalidArgumentException( | ||
| 'ALGORITHM=INSTANT cannot be combined with LOCK=NONE, LOCK=SHARED, or LOCK=EXCLUSIVE. ' . | ||
| 'Either use ALGORITHM=INSTANT alone, or use ALGORITHM=INSTANT with LOCK=DEFAULT.', | ||
| ); | ||
| } | ||
|
|
||
| // Execute with custom template | ||
| if ($instructions->getAlterParts()) { | ||
| $alter = sprintf($alterTemplate, implode(', ', $instructions->getAlterParts()) . $algorithmLockClause); | ||
| $this->execute($alter); | ||
| } | ||
|
|
||
| // Execute post-steps | ||
| $state = []; | ||
| foreach ($instructions->getPostSteps() as $instruction) { | ||
| if (is_callable($instruction)) { | ||
| $state = $instruction($state); | ||
| continue; | ||
| } | ||
|
|
||
| $this->execute($instruction); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not duplicate all of this logic? This feels like a big smell that we're missing a better design solution. Perhaps we could have an adapter hook method at the end of
executeActions()that receives both theactionsandinstructions. That would allow the MySqlAdapter to append the lock/algorithm clauses without having to create all this duplication.