fix(codegen): sort map iteration to make generated output deterministic
Table.Columns/Constraints/Indexes/Relationships are Go maps, and every writer, reader, diff, inspector, and merge code path that iterated them directly was subject to Go's randomized map order, so identical input could produce different output (or a different in-report violation/diff order) on every run. Most visibly this showed up as bun/gorm `unique:` struct tags changing order across consecutive `make models` runs with no source change. Fixed by sorting map iteration (by Sequence then Name, or alphabetically for string-keyed maps) everywhere the order affects generated output or first-match tie-break logic, across the bun, gorm, sqlite, dbml, drawdb, pgsql, prisma, graphql, typeorm, drizzle, and dctx writers; the dctx, prisma, and typeorm readers; the shared models.GetPrimaryKey/ GetForeignKeys helpers; pkg/diff, pkg/inspector, and pkg/merge; and the TUI column/relationship pickers in pkg/ui.
This commit is contained in:
@@ -51,6 +51,45 @@ func TestInspect(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestInspect_Deterministic verifies that repeated Inspect() calls against
|
||||
// the same database and config produce violations in the same order, instead
|
||||
// of following Go's randomized map iteration order over config.Rules and the
|
||||
// per-table Columns/Constraints/Indexes maps.
|
||||
func TestInspect_Deterministic(t *testing.T) {
|
||||
db := createTestDatabase()
|
||||
config := GetDefaultConfig()
|
||||
|
||||
inspector := NewInspector(db, config)
|
||||
|
||||
first, err := inspector.Inspect()
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect() returned error: %v", err)
|
||||
}
|
||||
|
||||
wantOrder := make([]string, len(first.Violations))
|
||||
for i, v := range first.Violations {
|
||||
wantOrder[i] = v.RuleName + "|" + v.Location
|
||||
}
|
||||
|
||||
for i := 0; i < 25; i++ {
|
||||
report, err := inspector.Inspect()
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect() returned error on run %d: %v", i, err)
|
||||
}
|
||||
|
||||
if len(report.Violations) != len(wantOrder) {
|
||||
t.Fatalf("run %d: got %d violations, want %d", i, len(report.Violations), len(wantOrder))
|
||||
}
|
||||
|
||||
for j, v := range report.Violations {
|
||||
got := v.RuleName + "|" + v.Location
|
||||
if got != wantOrder[j] {
|
||||
t.Fatalf("run %d: violation[%d] = %q, want %q", i, j, got, wantOrder[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectWithDisabledRules(t *testing.T) {
|
||||
db := createTestDatabase()
|
||||
config := GetDefaultConfig()
|
||||
|
||||
Reference in New Issue
Block a user