Skip to content

Conversation

@tomoropy
Copy link

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

  • Do only one thing
  • Non breaking API changes
  • 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 in internal/model/tbl_column.go
  • Imported sort package 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:

  1. Developer runs gen to generate models from database schema
  2. Models are committed to version control
  3. CI/CD pipeline runs the same generation command
  4. CI fails because the generated file has different index order (even though functionally identical)
  5. 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.

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: #<issue番号>
@tomoropy tomoropy marked this pull request as ready for review November 22, 2025 07:47
@propel-code-bot
Copy link

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

Copy link

@propel-code-bot propel-code-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it! 🚢


Why was this auto-approved?
AI analysis completed with no actionable comments or suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index order in generated models is non-deterministic

1 participant