package dbschema import ( "strings" "testing" ) func baseSnap() Snapshot { return Snapshot{ Tables: []Table{{ Schema: "public", Name: "users", Columns: []Column{{Name: "id", Type: "integer"}, {Name: "email", Type: "text", Nullable: true}, {Name: "legacy", Type: "text", Nullable: true}}, Indexes: []Index{{Name: "users_email_idx", Definition: "CREATE INDEX users_email_idx ON public.users USING btree (email)"}}, Constraints: []Constraint{{Name: "users_pkey", Type: "p", Definition: "PRIMARY KEY (id)"}}, }, {Schema: "public", Name: "old_t", Columns: []Column{{Name: "id", Type: "integer"}}}}, Views: []View{{Schema: "public", Name: "v_old", Definition: "SELECT 1"}}, Sequences: []Sequence{{Schema: "public", Name: "s1", Type: "bigint", Start: 1, Increment: 1, Min: 1, Max: 100}}, } } func headSnap() Snapshot { return Snapshot{ Tables: []Table{{ Schema: "public", Name: "users", Columns: []Column{{Name: "id", Type: "bigint"}, {Name: "email", Type: "text", Default: "''::text"}, {Name: "age", Type: "integer", Nullable: true}}, Indexes: []Index{{Name: "users_age_idx", Definition: "CREATE INDEX users_age_idx ON public.users USING btree (age)"}}, Constraints: []Constraint{{Name: "users_pkey", Type: "p", Definition: "PRIMARY KEY (id)"}, {Name: "users_age_chk", Type: "c", Definition: "CHECK ((age > 0))"}}, }, { Schema: "public", Name: "orders", Columns: []Column{{Name: "id", Type: "integer"}, {Name: "user_id", Type: "integer"}}, Indexes: []Index{{Name: "orders_user_idx", Definition: "CREATE INDEX orders_user_idx ON public.orders USING btree (user_id)"}}, Constraints: []Constraint{{Name: "orders_pkey", Type: "p", Definition: "PRIMARY KEY (id)"}, {Name: "orders_user_fk", Type: "f", Definition: "FOREIGN KEY (user_id) REFERENCES users(id)"}}, }}, Views: []View{{Schema: "public", Name: "v_new", Definition: "SELECT 2"}}, Sequences: []Sequence{{Schema: "public", Name: "s1", Type: "bigint", Start: 1, Increment: 5, Min: 1, Max: 100}}, } } func TestDiffChangesAndSummary(t *testing.T) { r := Diff(baseSnap(), headSnap()) got := map[string]bool{} for _, c := range r.Changes { got[c.Kind+" "+c.Object+" "+c.Table+"."+c.Name] = true } want := []string{ "added table .orders", "removed table .old_t", "changed column users.id", "changed column users.email", "added column users.age", "removed column users.legacy", "added index users.users_age_idx", "removed index users.users_email_idx", "added constraint users.users_age_chk", "added view .v_new", "removed view .v_old", "changed sequence .s1", } for _, w := range want { if !got[w] { t.Errorf("missing change %q in %v", w, got) } } if len(r.Changes) != len(want) { t.Errorf("got %d changes, want %d", len(r.Changes), len(want)) } if r.Summary.Added != 5 || r.Summary.Removed != 4 || r.Summary.Changed != 3 { t.Errorf("summary = %+v", r.Summary) } } func TestDiffSQL(t *testing.T) { sql := Diff(baseSnap(), headSnap()).SQL for _, w := range []string{ Header, `DROP TABLE "public"."old_t";`, `DROP INDEX "public"."users_email_idx";`, `DROP VIEW "public"."v_old";`, `CREATE TABLE "public"."orders" (`, `ALTER TABLE "public"."users" ADD COLUMN "age" integer;`, `ALTER TABLE "public"."users" DROP COLUMN "legacy";`, `ALTER TABLE "public"."users" ALTER COLUMN "id" TYPE bigint;`, `ALTER TABLE "public"."users" ALTER COLUMN "email" SET DEFAULT ''::text;`, `ALTER TABLE "public"."users" ALTER COLUMN "email" SET NOT NULL;`, `ALTER TABLE "public"."users" ADD CONSTRAINT "users_age_chk" CHECK ((age > 0));`, `ALTER TABLE "public"."orders" ADD CONSTRAINT "orders_user_fk" FOREIGN KEY (user_id) REFERENCES users(id);`, `ALTER SEQUENCE "public"."s1" AS bigint INCREMENT BY 5`, `CREATE VIEW "public"."v_new" AS`, } { if !strings.Contains(sql, w) { t.Errorf("sql missing %q\n%s", w, sql) } } // A foreign key must come after the tables it can reference exist. if strings.Index(sql, "orders_user_fk") < strings.Index(sql, `CREATE TABLE "public"."orders"`) { t.Error("foreign key emitted before CREATE TABLE") } if strings.Index(sql, "DROP TABLE") > strings.Index(sql, "CREATE TABLE") { t.Error("drops must come before creates") } } func TestDiffIdentical(t *testing.T) { r := Diff(baseSnap(), baseSnap()) if len(r.Changes) != 0 || r.SQL != "" || r.Summary != (Summary{}) { t.Fatalf("identical schemas produced %+v", r) } } func TestDiffQuotesIdentifiers(t *testing.T) { r := Diff(Snapshot{}, Snapshot{Tables: []Table{{Schema: "public", Name: `we"ird`, Columns: []Column{{Name: "a", Type: "text", Nullable: true}}}}}) if !strings.Contains(r.SQL, `"we""ird"`) { t.Fatalf("identifier not quoted:\n%s", r.SQL) } } func TestDiffTruncatedCarries(t *testing.T) { if !Diff(Snapshot{Truncated: true}, Snapshot{}).Truncated { t.Fatal("truncated flag lost") } } func TestDiffCreatesAndDropsSchemas(t *testing.T) { r := Diff(Snapshot{Schemas: []string{"public", "old"}}, Snapshot{Schemas: []string{"public", "New Schema"}}) if !strings.Contains(r.SQL, `CREATE SCHEMA "New Schema";`) || !strings.Contains(r.SQL, `DROP SCHEMA "old";`) || r.Summary.Added != 1 || r.Summary.Removed != 1 { t.Fatalf("schema diff wrong: %+v\n%s", r.Summary, r.SQL) } } func TestDiffDropsForeignKeysBeforeRemovedTables(t *testing.T) { base := Snapshot{Tables: []Table{ {Schema: "public", Name: "a_parent", Columns: []Column{{Name: "id", Type: "integer"}}, Constraints: []Constraint{{Name: "a_parent_pkey", Type: "p", Definition: "PRIMARY KEY (id)"}}}, {Schema: "public", Name: "z_child", Columns: []Column{{Name: "pid", Type: "integer"}}, Constraints: []Constraint{{Name: "z_child_fk", Type: "f", Definition: "FOREIGN KEY (pid) REFERENCES public.a_parent(id)"}}}, {Schema: "public", Name: "keeper", Columns: []Column{{Name: "pid", Type: "integer"}}, Constraints: []Constraint{{Name: "keeper_fk", Type: "f", Definition: "FOREIGN KEY (pid) REFERENCES a_parent(id)"}}}, }} head := Snapshot{Tables: []Table{base.Tables[2]}} sql := Diff(base, head).SQL dropParent := strings.Index(sql, `DROP TABLE "public"."a_parent";`) for _, w := range []string{`ALTER TABLE "public"."z_child" DROP CONSTRAINT "z_child_fk";`, `ALTER TABLE "public"."keeper" DROP CONSTRAINT "keeper_fk";`} { i := strings.Index(sql, w) if i < 0 || dropParent < 0 || i > dropParent { t.Errorf("%q missing or after the parent drop:\n%s", w, sql) } } } func TestDiffGeneratedAndIdentityColumns(t *testing.T) { head := Snapshot{Tables: []Table{{Schema: "public", Name: "t", Columns: []Column{ {Name: "id", Type: "bigint", Identity: "a"}, {Name: "seq", Type: "integer", Identity: "d"}, {Name: "total", Type: "integer", Nullable: true, Default: "(a + b)", Generated: "s"}, }}}} sql := Diff(Snapshot{}, head).SQL for _, w := range []string{`"id" bigint GENERATED ALWAYS AS IDENTITY`, `"seq" integer GENERATED BY DEFAULT AS IDENTITY`, `"total" integer GENERATED ALWAYS AS ((a + b)) STORED`} { if !strings.Contains(sql, w) { t.Errorf("create table missing %q\n%s", w, sql) } } base := Snapshot{Tables: []Table{{Schema: "public", Name: "t", Columns: []Column{{Name: "id", Type: "bigint"}, {Name: "total", Type: "integer", Nullable: true}}}}} r := Diff(base, head) if !strings.Contains(r.SQL, `ALTER COLUMN "id" ADD GENERATED ALWAYS AS IDENTITY;`) { t.Errorf("identity add missing\n%s", r.SQL) } if !strings.Contains(r.SQL, "-- unsupported change:") { t.Errorf("generation change must be flagged\n%s", r.SQL) } flagged := false for _, c := range r.Changes { if c.Object == ObjectColumn && c.Name == "total" && c.Unsupported { flagged = true } } if !flagged { t.Errorf("change not marked unsupported: %+v", r.Changes) } add := Diff(Snapshot{Tables: []Table{{Schema: "public", Name: "t", Columns: []Column{{Name: "id", Type: "integer"}}}}}, Snapshot{Tables: []Table{{Schema: "public", Name: "t", Columns: []Column{{Name: "id", Type: "integer"}, {Name: "n", Type: "integer", Nullable: true, Default: "(id * 2)", Generated: "s"}}}}}) if !strings.Contains(add.SQL, `ADD COLUMN "n" integer GENERATED ALWAYS AS ((id * 2)) STORED;`) { t.Errorf("add generated column\n%s", add.SQL) } } func TestDiffViewKindChangeDropsOldKind(t *testing.T) { ord := Snapshot{Views: []View{{Schema: "public", Name: "v", Definition: "SELECT 1"}}} mat := Snapshot{Views: []View{{Schema: "public", Name: "v", Materialized: true, Definition: "SELECT 1"}}} r := Diff(ord, mat) if !strings.Contains(r.SQL, `DROP VIEW "public"."v";`) || !strings.Contains(r.SQL, `CREATE MATERIALIZED VIEW "public"."v"`) || strings.Contains(r.SQL, "DROP MATERIALIZED VIEW") { t.Errorf("ordinary to materialized:\n%s", r.SQL) } if c := r.Changes[0]; c.BeforeKind != "VIEW" || c.AfterKind != "MATERIALIZED VIEW" { t.Errorf("kinds not recorded: %+v", c) } back := Diff(mat, ord).SQL if !strings.Contains(back, `DROP MATERIALIZED VIEW "public"."v";`) || !strings.Contains(back, `CREATE VIEW "public"."v"`) || strings.Contains(back, "CREATE OR REPLACE") { t.Errorf("materialized to ordinary:\n%s", back) } }