|
| 1 | +package pg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/bytebase/omni/pg/ast" |
| 8 | + "github.com/bytebase/omni/pg/catalog" |
| 9 | + |
| 10 | + storepb "github.com/bytebase/bytebase/backend/generated-go/store" |
| 11 | +) |
| 12 | + |
| 13 | +// TestLoaderInstallsCompositeTypes proves the query-span catalog loader |
| 14 | +// installs composite types from metadata with real attributes, in dependency |
| 15 | +// order (aa_nested sorts before its dependency zz_base alphabetically), and |
| 16 | +// that tables typed with them install cleanly. |
| 17 | +func TestLoaderInstallsCompositeTypes(t *testing.T) { |
| 18 | + meta := &storepb.DatabaseSchemaMetadata{ |
| 19 | + Schemas: []*storepb.SchemaMetadata{ |
| 20 | + { |
| 21 | + Name: "public", |
| 22 | + EnumTypes: []*storepb.EnumTypeMetadata{ |
| 23 | + {Name: "status", Values: []string{"a", "b"}}, |
| 24 | + }, |
| 25 | + CompositeTypes: []*storepb.CompositeTypeMetadata{ |
| 26 | + { |
| 27 | + // References zz_base only through an array suffix. |
| 28 | + Name: "aa_array_only", |
| 29 | + Attributes: []*storepb.CompositeTypeAttribute{ |
| 30 | + {Name: "items", Type: "public.zz_base[]"}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + Name: "aa_nested", |
| 35 | + Attributes: []*storepb.CompositeTypeAttribute{ |
| 36 | + {Name: "home", Type: "public.zz_base"}, |
| 37 | + {Name: "s", Type: "public.status"}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + Name: "zz_base", |
| 42 | + Attributes: []*storepb.CompositeTypeAttribute{ |
| 43 | + {Name: "street", Type: "text"}, |
| 44 | + {Name: "city", Type: "character varying(50)"}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + Tables: []*storepb.TableMetadata{ |
| 49 | + { |
| 50 | + Name: "users", |
| 51 | + Columns: []*storepb.ColumnMetadata{ |
| 52 | + {Name: "id", Type: "integer"}, |
| 53 | + {Name: "home", Type: "public.zz_base", Nullable: true}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + cat := catalog.New() |
| 62 | + cat.SetSearchPath([]string{"public"}) |
| 63 | + loader := newCatalogLoader(cat, meta) |
| 64 | + if err := loader.Load(context.Background()); err != nil { |
| 65 | + t.Fatalf("Load: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + base := cat.GetRelation("public", "zz_base") |
| 69 | + if base == nil { |
| 70 | + t.Fatal("composite type zz_base not installed") |
| 71 | + } |
| 72 | + if len(base.Columns) != 2 { |
| 73 | + t.Errorf("zz_base attribute count: got %d, want 2", len(base.Columns)) |
| 74 | + } |
| 75 | + if base.Columns[0].Name != "street" { |
| 76 | + t.Errorf("zz_base first attribute: got %q, want street", base.Columns[0].Name) |
| 77 | + } |
| 78 | + nested := cat.GetRelation("public", "aa_nested") |
| 79 | + if nested == nil { |
| 80 | + t.Fatal("composite type aa_nested not installed") |
| 81 | + } |
| 82 | + if len(nested.Columns) != 2 { |
| 83 | + t.Errorf("aa_nested attribute count: got %d, want 2", len(nested.Columns)) |
| 84 | + } |
| 85 | + arrayOnly := cat.GetRelation("public", "aa_array_only") |
| 86 | + if arrayOnly == nil { |
| 87 | + t.Fatal("composite type aa_array_only not installed") |
| 88 | + } |
| 89 | + if len(arrayOnly.Columns) != 1 || arrayOnly.Columns[0].Name != "items" { |
| 90 | + t.Errorf("aa_array_only must install with its real attribute, got %d columns", len(arrayOnly.Columns)) |
| 91 | + } |
| 92 | + if cat.GetRelation("public", "users") == nil { |
| 93 | + t.Fatal("table with composite column not installed") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TestLoaderCompositeFallbackPreservesAttributeNames forces the pseudo |
| 98 | +// fallback via a domain-typed attribute (domains are not loader objects) and |
| 99 | +// asserts the fallback keeps the metadata attribute names. |
| 100 | +func TestLoaderCompositeFallbackPreservesAttributeNames(t *testing.T) { |
| 101 | + meta := &storepb.DatabaseSchemaMetadata{ |
| 102 | + Schemas: []*storepb.SchemaMetadata{ |
| 103 | + { |
| 104 | + Name: "public", |
| 105 | + CompositeTypes: []*storepb.CompositeTypeMetadata{ |
| 106 | + { |
| 107 | + Name: "with_domain", |
| 108 | + Attributes: []*storepb.CompositeTypeAttribute{ |
| 109 | + {Name: "p", Type: "public.pos_int"}, |
| 110 | + {Name: "note", Type: "text"}, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + cat := catalog.New() |
| 119 | + cat.SetSearchPath([]string{"public"}) |
| 120 | + loader := newCatalogLoader(cat, meta) |
| 121 | + if err := loader.Load(context.Background()); err != nil { |
| 122 | + t.Fatalf("Load: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + rel := cat.GetRelation("public", "with_domain") |
| 126 | + if rel == nil { |
| 127 | + t.Fatal("composite must fall back, not vanish") |
| 128 | + } |
| 129 | + if len(rel.Columns) != 2 { |
| 130 | + t.Fatalf("fallback must preserve attribute names, got %d columns", len(rel.Columns)) |
| 131 | + } |
| 132 | + if rel.Columns[0].Name != "p" || rel.Columns[1].Name != "note" { |
| 133 | + t.Errorf("fallback attribute names: got %q, %q", rel.Columns[0].Name, rel.Columns[1].Name) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// TestRangeVarFallbackSkipsCompositeTypes proves the query-span fallback does |
| 138 | +// not expand a standalone composite type as a FROM source — PostgreSQL |
| 139 | +// rejects composite types as table sources. |
| 140 | +func TestRangeVarFallbackSkipsCompositeTypes(t *testing.T) { |
| 141 | + meta := &storepb.DatabaseSchemaMetadata{ |
| 142 | + Schemas: []*storepb.SchemaMetadata{ |
| 143 | + { |
| 144 | + Name: "public", |
| 145 | + CompositeTypes: []*storepb.CompositeTypeMetadata{ |
| 146 | + { |
| 147 | + Name: "addr", |
| 148 | + Attributes: []*storepb.CompositeTypeAttribute{ |
| 149 | + {Name: "street", Type: "text"}, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + Tables: []*storepb.TableMetadata{ |
| 154 | + { |
| 155 | + Name: "t", |
| 156 | + Columns: []*storepb.ColumnMetadata{ |
| 157 | + {Name: "id", Type: "integer"}, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | + |
| 165 | + cat := catalog.New() |
| 166 | + cat.SetSearchPath([]string{"public"}) |
| 167 | + loader := newCatalogLoader(cat, meta) |
| 168 | + if err := loader.Load(context.Background()); err != nil { |
| 169 | + t.Fatalf("Load: %v", err) |
| 170 | + } |
| 171 | + |
| 172 | + e := &omniQuerySpanExtractor{ |
| 173 | + cat: cat, |
| 174 | + searchPath: []string{"public"}, |
| 175 | + defaultDatabase: "db", |
| 176 | + } |
| 177 | + if results := e.extractColumnsFromRangeVar(&ast.RangeVar{Schemaname: "public", Relname: "addr"}); results != nil { |
| 178 | + t.Errorf("composite type must not expand as a FROM source, got %d columns", len(results)) |
| 179 | + } |
| 180 | + if results := e.extractColumnsFromRangeVar(&ast.RangeVar{Schemaname: "public", Relname: "t"}); len(results) != 1 { |
| 181 | + t.Errorf("real table must still expand, got %d columns", len(results)) |
| 182 | + } |
| 183 | +} |
0 commit comments