|
| 1 | +package catalog_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/sqlc-dev/sqlc/internal/engine/postgresql" |
| 8 | + "github.com/sqlc-dev/sqlc/internal/sql/ast" |
| 9 | + "github.com/sqlc-dev/sqlc/internal/sql/catalog" |
| 10 | +) |
| 11 | + |
| 12 | +// stubColumnGenerator satisfies the catalog's column generator dependency |
| 13 | +// without pulling in the full compiler. CREATE VIEW only needs a column set |
| 14 | +// to store; these tests assert on relation presence and dependency tracking, |
| 15 | +// not on the view's column types. |
| 16 | +type stubColumnGenerator struct{} |
| 17 | + |
| 18 | +func (stubColumnGenerator) OutputColumns(ast.Node) ([]*catalog.Column, error) { |
| 19 | + return []*catalog.Column{ |
| 20 | + {Name: "id", Type: ast.TypeName{Name: "int4"}}, |
| 21 | + }, nil |
| 22 | +} |
| 23 | + |
| 24 | +func update(t *testing.T, c *catalog.Catalog, sql string) { |
| 25 | + t.Helper() |
| 26 | + stmts, err := postgresql.NewParser().Parse(strings.NewReader(sql)) |
| 27 | + if err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + for _, stmt := range stmts { |
| 31 | + if err := c.Update(stmt, stubColumnGenerator{}); err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func publicSchema(t *testing.T, c *catalog.Catalog) *catalog.Schema { |
| 38 | + t.Helper() |
| 39 | + for _, s := range c.Schemas { |
| 40 | + if s.Name == "public" { |
| 41 | + return s |
| 42 | + } |
| 43 | + } |
| 44 | + t.Fatal(`schema "public" not found`) |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func tableNames(schema *catalog.Schema) []string { |
| 49 | + names := make([]string, 0, len(schema.Tables)) |
| 50 | + for _, tbl := range schema.Tables { |
| 51 | + names = append(names, tbl.Rel.Name) |
| 52 | + } |
| 53 | + return names |
| 54 | +} |
| 55 | + |
| 56 | +func TestDropTableCascadeEvictsDependentViews(t *testing.T) { |
| 57 | + c := catalog.New("public") |
| 58 | + update(t, c, ` |
| 59 | +CREATE TABLE base (id int); |
| 60 | +CREATE VIEW child AS SELECT id FROM base; |
| 61 | +CREATE VIEW grandchild AS SELECT id FROM child; |
| 62 | +`) |
| 63 | + |
| 64 | + schema := publicSchema(t, c) |
| 65 | + if got := tableNames(schema); len(got) != 3 { |
| 66 | + t.Fatalf("expected 3 relations before drop, got %v", got) |
| 67 | + } |
| 68 | + |
| 69 | + update(t, c, `DROP TABLE base CASCADE;`) |
| 70 | + |
| 71 | + // base is dropped; child depends on base and grandchild depends on child, |
| 72 | + // so CASCADE must transitively evict both views. |
| 73 | + if got := tableNames(schema); len(got) != 0 { |
| 74 | + t.Fatalf("expected cascade drop to remove base and dependent views, got %v", got) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestDropTableWithoutCascadeKeepsDependentViews(t *testing.T) { |
| 79 | + for _, tc := range []struct { |
| 80 | + name string |
| 81 | + sql string |
| 82 | + }{ |
| 83 | + {name: "restrict", sql: `DROP TABLE base RESTRICT;`}, |
| 84 | + {name: "default", sql: `DROP TABLE base;`}, |
| 85 | + } { |
| 86 | + t.Run(tc.name, func(t *testing.T) { |
| 87 | + c := catalog.New("public") |
| 88 | + update(t, c, ` |
| 89 | +CREATE TABLE base (id int); |
| 90 | +CREATE VIEW child AS SELECT id FROM base; |
| 91 | +`) |
| 92 | + |
| 93 | + schema := publicSchema(t, c) |
| 94 | + update(t, c, tc.sql) |
| 95 | + |
| 96 | + got := tableNames(schema) |
| 97 | + if len(got) != 1 || got[0] != "child" { |
| 98 | + t.Fatalf("expected dependent view to remain after %s, got %v", tc.name, got) |
| 99 | + } |
| 100 | + if len(schema.Tables[0].DependsOn) != 1 || schema.Tables[0].DependsOn[0].Name != "base" { |
| 101 | + t.Fatalf("expected remaining view dependency to be preserved, got %#v", schema.Tables[0].DependsOn) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments