fix: ensure deterministic index order in generated models
Sort indexes by name before generating GORM tags to ensure consistent output across multiple code generation runs.
This fixes the issue where index order in generated models was non-deterministic, causing CI diffs on each generation.
Fixes: #1409
- [x] Do only one thing
- [x] Non breaking API changes
- [x] Tested
What did this pull request do?
This PR fixes the non-deterministic index order issue in generated GORM models. Previously, when generating models with multiple indexes on the same column, the order of indexes in the GORM tag could vary between generation runs, causing unnecessary diffs in CI/CD pipelines.
Changes:
- Added sorting of indexes by name in
buildGormTag()method ininternal/model/tbl_column.go - Imported
sortpackage to enable index sorting - Indexes are now sorted alphabetically by name before being added to GORM tags, ensuring consistent output
Example:
Before this fix, a column with indexes idx_payout_tenant_payment_at and idx_payout_tenant_id could generate tags in different orders:
- Sometimes:
index:idx_payout_tenant_id,priority:1;index:idx_payout_tenant_payment_at,priority:2 - Sometimes:
index:idx_payout_tenant_payment_at,priority:2;index:idx_payout_tenant_id,priority:1
After this fix, the order is always deterministic (alphabetical):
- Always:
index:idx_payout_tenant_id,priority:1;index:idx_payout_tenant_payment_at,priority:2
User Case Description
Problem: When using GORM gen to generate models, the index order in generated GORM tags was non-deterministic. This caused CI/CD pipelines to fail with false-positive diffs on every code generation run, even when the actual database schema hadn't changed.
Use Case:
- Developer runs
gento generate models from database schema - Models are committed to version control
- CI/CD pipeline runs the same generation command
- CI fails because the generated file has different index order (even though functionally identical)
- Developer has to manually commit the "updated" file, creating unnecessary noise in git history
Solution: By sorting indexes alphabetically by name before generating tags, the output is now deterministic and consistent across all generation runs, eliminating false-positive CI diffs.
Deterministic index ordering in generated GORM model tags
The PR adds alphabetical sorting for index definitions when building GORM tags, eliminating non-deterministic ordering that previously caused spurious diffs during repeated code-generation runs. Only one file is modified with a small, non-breaking change.
Key Changes
• Imported sort package in internal/model/tbl_column.go
• Created local slice sortedIndexes and applied sort.Slice() on it by Name()
• Replaced loop over c.Indexes with loop over sortedIndexes to ensure deterministic tag order
Affected Areas
• internal/model/tbl_column.go (function buildGormTag())
This summary was automatically generated by @propel-code-bot