Fix: sort foreign keys in SchemaNormalizer to stabilize bean generation#305
Open
nguyenk wants to merge 1 commit into
Open
Fix: sort foreign keys in SchemaNormalizer to stabilize bean generation#305nguyenk wants to merge 1 commit into
nguyenk wants to merge 1 commit into
Conversation
When a table has two foreign keys pointing to the same target table, the generated Base bean methods for the reverse relation (one-to-many) were reordered on every `tdbm:generate` run. Root cause: SchemaNormalizer::normalizeTable() wrote foreign_keys to the lock file in the order returned by DB introspection (AbstractSchemaManager::introspectSchema()). MySQL/MariaDB does not guarantee a stable ordering of FK constraints, so the lock file FK order could differ between environments or after schema changes. Since tdbm:generate writes the lock file then immediately reads it to generate beans, the bean method order inherited this non-determinism. The post-processing normalize-tdbm-yaml.php script sorted the lock file after the fact, but beans were already generated by then. Fix: apply ksort() on foreign_keys after collection in normalizeTable(), consistent with the existing ksort() on tables in normalize(). Guard with isset() for tables that have no foreign keys. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes non-deterministic ordering of generated bean reverse-relation methods by ensuring foreign keys are serialized in a stable order during schema normalization (rather than relying on DB introspection ordering, which can vary on MySQL/MariaDB).
Changes:
- Sort
foreign_keysby constraint name (ksort) insideSchemaNormalizer::normalizeTable()(guarded for tables with no FKs). - Add a PHPUnit test that verifies foreign keys are normalized in alphabetical order regardless of insertion order.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/SchemaVersionControl/SchemaNormalizer.php |
Stabilizes schema lock output by sorting foreign key entries per table. |
tests/SchemaVersionControl/SchemaNormalizerTest.php |
Adds regression coverage to ensure FK ordering is deterministic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
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.
When a table has two foreign keys pointing to the same target table, the generated Base bean methods for the reverse relation (one-to-many) were reordered on every
tdbm:generaterun.Root cause: SchemaNormalizer::normalizeTable() wrote foreign_keys to the lock file in the order returned by DB introspection (AbstractSchemaManager::introspectSchema()). MySQL/MariaDB does not guarantee a stable ordering of FK constraints, so the lock file FK order could differ between environments or after schema changes. Since tdbm:generate writes the lock file then immediately reads it to generate beans, the bean method order inherited this non-determinism.
The post-processing normalize-tdbm-yaml.php script sorted the lock file after the fact, but beans were already generated by then.
Fix: apply ksort() on foreign_keys after collection in normalizeTable(), consistent with the existing ksort() on tables in normalize(). Guard with isset() for tables that have no foreign keys.