package pg import ( "slices" "strings" "testing" ) func TestEmbeddedMigrationVersions(t *testing.T) { versions, err := embeddedMigrationVersions() if err != nil { t.Fatalf("embeddedMigrationVersions: %v", err) } if len(versions) == 0 { t.Fatal("expected at least one embedded migration") } if !slices.IsSorted(versions) { t.Fatalf("versions not sorted: %v", versions) } } // TestMissingMigrations guards schema verification for DML-only serving pods. func TestMissingMigrations(t *testing.T) { embedded := []int64{1, 2, 3} tests := []struct { name string applied []int64 want []int64 }{ {name: "up to date", applied: []int64{1, 2, 3}, want: nil}, {name: "ahead with extra history", applied: []int64{1, 2, 3, 4}, want: nil}, {name: "pending tail migration", applied: []int64{1, 2}, want: []int64{3}}, {name: "missing middle migration", applied: []int64{1, 3}, want: []int64{2}}, {name: "empty database", applied: nil, want: []int64{1, 2, 3}}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { got := missingMigrations(embedded, tc.applied) if !slices.Equal(got, tc.want) { t.Fatalf("missingMigrations(%v, %v) = %v, want %v", embedded, tc.applied, got, tc.want) } }) } } // TestSchemaPrivileges guards the append-only privilege split for the audit // schema so runtime database credentials can never UPDATE or DELETE audit rows. func TestSchemaPrivileges(t *testing.T) { tests := []struct { schema string want string }{ {schema: "audit", want: "SELECT, INSERT"}, {schema: "governance", want: "SELECT, INSERT, UPDATE, DELETE"}, {schema: "runs", want: "SELECT, INSERT, UPDATE, DELETE"}, {schema: "", want: "SELECT, INSERT, UPDATE, DELETE"}, } for _, tc := range tests { t.Run(tc.schema, func(t *testing.T) { got := schemaPrivileges(tc.schema) if got != tc.want { t.Fatalf("schemaPrivileges(%q) = %q, want %q", tc.schema, got, tc.want) } if tc.schema == "audit" && (strings.Contains(got, "UPDATE") || strings.Contains(got, "DELETE")) { t.Fatalf("audit schema must not grant UPDATE or DELETE, got %q", got) } }) } }