-
Notifications
You must be signed in to change notification settings - Fork 120
Add support for partitionBy() + update() on existing tables #989
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
jamisonbryant
wants to merge
8
commits into
5.x
Choose a base branch
from
fix-migrations-set-partitioning
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.
+743
−60
Conversation
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
When adding multiple partitions to an existing table, MySQL requires: ALTER TABLE foo ADD PARTITION (PARTITION p1 ..., PARTITION p2 ...) Previously, each AddPartition action generated its own ADD PARTITION clause, which when joined with commas resulted in invalid SQL: ALTER TABLE foo ADD PARTITION (...), ADD PARTITION (...) This fix: - Batches AddPartition actions together in executeActions() - Adds new getAddPartitionsInstructions() method to AbstractAdapter with a default implementation that calls the single partition method - Overrides getAddPartitionsInstructions() in MysqlAdapter to generate correct batched SQL: ADD PARTITION (PARTITION p1, PARTITION p2) - Similarly batches DropPartition actions for efficiency - Adds gatherPartitions() to Plan.php to properly gather partition actions - Includes extensive tests for single/multiple partition add/drop scenarios Refs #986
Enables adding partitioning to existing non-partitioned tables using:
$table->partitionBy(Partition::TYPE_RANGE_COLUMNS, 'created')
->addPartition('p2023', '2024-01-01')
->update();
Previously this generated no SQL. Now it properly generates:
ALTER TABLE `table` PARTITION BY RANGE COLUMNS (created) (...)
Changes:
- Add SetPartitioning action class
- Update Plan to handle SetPartitioning in gatherPartitions()
- Add getSetPartitioningInstructions() to AbstractAdapter/MysqlAdapter
- Create SetPartitioning action in Table::executeActions() when updating
Member
|
One minor gap: There's no explicit test for composite partition keys with multiple columns like ['year', 'month'], but the code supports it. ->partitionBy(Partition::TYPE_RANGE_COLUMNS, ['year', 'month'])
->addPartition('p202401', [2024, 2])You could add 3425 + public function testCreateTableWithCompositePartitionKey(): void
3426 + {
3427 + // Test composite partition keys - partitioning by multiple columns
3428 + // MySQL RANGE COLUMNS supports multiple columns
3429 + $table = new Table('composite_partitioned', ['id' => false, 'primary_key' => ['id', 'year', 'month']], $this->adapter);
3430 + $table->addColumn('id', 'integer')
3431 + ->addColumn('year', 'integer')
3432 + ->addColumn('month', 'integer')
3433 + ->addColumn('data', 'string', ['limit' => 100])
3434 + ->partitionBy(Partition::TYPE_RANGE_COLUMNS, ['year', 'month'])
3435 + ->addPartition('p202401', [2024, 2])
3436 + ->addPartition('p202402', [2024, 3])
3437 + ->addPartition('p202403', [2024, 4])
3438 + ->create();
3439 +
3440 + $this->assertTrue($this->adapter->hasTable('composite_partitioned'));
3441 +
3442 + // Verify partitioning works by inserting data into different partitions
3443 + $this->adapter->execute(
3444 + "INSERT INTO composite_partitioned (id, year, month, data) VALUES (1, 2024, 1, 'January')",
3445 + );
3446 + $this->adapter->execute(
3447 + "INSERT INTO composite_partitioned (id, year, month, data) VALUES (2, 2024, 2, 'February')",
3448 + );
3449 + $this->adapter->execute(
3450 + "INSERT INTO composite_partitioned (id, year, month, data) VALUES (3, 2024, 3, 'March')",
3451 + );
3452 +
3453 + $rows = $this->adapter->fetchAll('SELECT * FROM composite_partitioned ORDER BY month');
3454 + $this->assertCount(3, $rows);
3455 + $this->assertEquals('January', $rows[0]['data']);
3456 + $this->assertEquals('February', $rows[1]['data']);
3457 + $this->assertEquals('March', $rows[2]['data']);
3458 + } |
This was
linked to
issues
Jan 6, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Continuation of #988
Summary
The plugin currently supports table partitioning in two scenarios:
partitionBy()+create()works correctlyaddPartitionToExisting()+save()works correctlyHowever, there's a third real-world scenario that wasn't supported: converting an existing non-partitioned table to a partitioned one. This is common when:
Root cause:
partitionBy()andaddPartition()store configuration on the TableMetadata object but don't create an Action. Thecreate()path reads this config when buildingCREATE TABLE, but theupdate()path had no corresponding action to triggerALTER TABLE ... PARTITION BY ....Fix
This PR adds a new
SetPartitioningaction that bridges this gap, enabling:To generate: