package metadata import ( "context" "testing" ) func TestPortabilityRejectsIndexDrift(t *testing.T) { for _, backend := range []Backend{SQLite, PostgreSQL} { for _, change := range []string{"missing", "wrong", "extra"} { t.Run(string(backend)+"/"+change, func(t *testing.T) { r := testRepository(t, backend) ctx := context.Background() if change != "extra" { if _, err := r.database.ExecContext(ctx, `DROP INDEX idx_paths_domain_object`); err != nil { t.Fatal(err) } } if change == "wrong" { if _, err := r.database.ExecContext(ctx, `CREATE INDEX idx_paths_domain_object ON paths(object_uuid)`); err != nil { t.Fatal(err) } } if change == "extra" { if _, err := r.database.ExecContext(ctx, `CREATE UNIQUE INDEX unported_unique_rule ON paths(object_uuid)`); err != nil { t.Fatal(err) } } report, err := r.InspectPortability(ctx) if err != nil || report.CoreCompatible { t.Fatal("index drift accepted", err, report) } }) } } } func TestPostgreSQLPortabilityRejectsContractDrift(t *testing.T) { changes := map[string]string{ "type": `ALTER TABLE assets ALTER COLUMN size_bytes TYPE INTEGER`, "default": `ALTER TABLE revisions ALTER COLUMN message SET DEFAULT 'different'`, "nullable": `ALTER TABLE revisions ALTER COLUMN content_type DROP NOT NULL`, "foreign-key": `ALTER TABLE paths DROP CONSTRAINT paths_object_uuid_fkey`, "collation": `ALTER TABLE paths ALTER COLUMN path TYPE TEXT COLLATE "default"`, "identity-mode": `ALTER TABLE asset_head_history ALTER COLUMN id SET GENERATED BY DEFAULT`, "identity-drop": `ALTER TABLE asset_head_history ALTER COLUMN id DROP IDENTITY`, "missing-table": `DROP TABLE asset_head_history`, "identity-step": `ALTER TABLE asset_head_history ALTER COLUMN id SET INCREMENT BY 7`, "rls": `ALTER TABLE objects ENABLE ROW LEVEL SECURITY`, "ledger-rls": `ALTER TABLE schema_migration_steps ENABLE ROW LEVEL SECURITY`, "routine": `CREATE FUNCTION unported_routine() RETURNS INTEGER LANGUAGE SQL AS 'SELECT 1'`, "sequence": `CREATE SEQUENCE unported_counter`, "rule": `CREATE RULE unported_rule AS ON DELETE TO paths DO INSTEAD NOTHING`, "duplicate-fk": `ALTER TABLE paths ADD CONSTRAINT duplicate_fk FOREIGN KEY (object_uuid) REFERENCES objects(uuid)`, } for name, q := range changes { t.Run(name, func(t *testing.T) { r := testRepository(t, PostgreSQL) ctx := context.Background() if _, err := r.database.ExecContext(ctx, q); err != nil { t.Fatal(err) } report, err := r.InspectPortability(ctx) if err != nil || report.CoreCompatible { t.Fatal("contract drift accepted", err, report) } }) } }