fix: ensure deterministic index order in generated models #1410
+9
−1
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.
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
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:
buildGormTag()method ininternal/model/tbl_column.gosortpackage to enable index sortingExample:
Before this fix, a column with indexes
idx_payout_tenant_payment_atandidx_payout_tenant_idcould generate tags in different orders:index:idx_payout_tenant_id,priority:1;index:idx_payout_tenant_payment_at,priority:2index:idx_payout_tenant_payment_at,priority:2;index:idx_payout_tenant_id,priority:1After this fix, the order is always deterministic (alphabetical):
index:idx_payout_tenant_id,priority:1;index:idx_payout_tenant_payment_at,priority:2User 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:
gento generate models from database schemaSolution:
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.