-
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
base: 5.x
Are you sure you want to change the base?
Conversation
Implements support for MySQL's ALGORITHM and LOCK clauses in ALTER TABLE
operations, enabling zero-downtime schema migrations for compatible operations.
Key additions:
- Class constants for ALGORITHM (DEFAULT, INSTANT, INPLACE, COPY) and LOCK
(DEFAULT, NONE, SHARED, EXCLUSIVE) options to avoid magic strings
- Column class now supports algorithm and lock options via setAlgorithm()/setLock()
- MysqlAdapter validates and applies algorithm/lock clauses to ALTER operations
- Batched operations detect conflicts and throw clear error messages
- Comprehensive test coverage (11 new test cases)
Benefits:
- Near-zero downtime migrations on large tables with ALGORITHM=INSTANT
- Production-friendly migrations with explicit locking control
- Improved performance for compatible schema changes on MySQL 8.0+/MariaDB 10.3+
Usage:
```php
use Migrations\Db\Adapter\MysqlAdapter;
$table->addColumn('status', 'string', [
'null' => true,
'algorithm' => MysqlAdapter::ALGORITHM_INSTANT,
'lock' => MysqlAdapter::LOCK_NONE,
])->update();
```
Closes #2323
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
- Remove duplicate algorithm/lock clauses from individual column instructions - Add MySQL restriction validation: ALGORITHM=INSTANT cannot be combined with explicit LOCK modes (LOCK=NONE, LOCK=SHARED, LOCK=EXCLUSIVE) - Update tests to use ALGORITHM=INPLACE with explicit LOCK values instead - Add test for MySQL restriction validation - Remove unused algorithmClause() method - Update documentation to clarify MySQL restrictions Fixes duplicate clause issue where algorithm/lock was being added twice: once in getAddColumnInstructions() and once in executeActionsWithAlgorithmAndLock(). The algorithm/lock clauses should only be applied at the ALTER TABLE statement level, not at individual column instruction level. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
| * @throws \InvalidArgumentException | ||
| * @return void | ||
| */ | ||
| protected function executeActionsWithAlgorithmAndLock( |
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 the actions and instructions. That would allow the MySqlAdapter to append the lock/algorithm clauses without having to create all this duplication.
| $alterTemplate = sprintf('ALTER TABLE %s %%s', $this->quoteTableName($table->getName())); | ||
|
|
||
| // Add algorithm and lock clauses | ||
| $algorithmLockClause = ''; |
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.
Won't we be missing lock/algorithm when $adapter->addColumn() and changeColumn() are used because of where this logic is?
Summary
Adds support for MySQL's
ALGORITHMandLOCKclauses inALTER TABLEoperations, enabling zero-downtime schema migrations for compatible operations on MySQL 8.0+ and MariaDB 10.3+.Why This Is Useful
Production Impact:
ALGORITHM=INSTANTallows adding nullable columns, dropping columns, and other compatible operations without copying the entire tableLOCK=NONEto ensure migrations don't block concurrent reads/writesReal-world scenario:
Implementation
Class Constants (No Magic Strings)
Key Features
Important MySQL Restriction
LOCK=NONE,LOCK=SHARED,LOCK=EXCLUSIVE) due to MySQL limitations. Use either:ALGORITHM=INSTANTalone (recommended)ALGORITHM=INSTANTwithLOCK=DEFAULTALGORITHM=INPLACEwith any LOCK optionUsage Examples
Single operation:
Batched operations:
Changes
algorithmandlockproperties with getters/settersCompatibility