Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(pg): replace DDL text path with catalog loader for walk-through (#20038)

rebelice5dea44001440
diff --git a/backend/plugin/schema/pg/testdata/walk_through.yaml b/backend/plugin/schema/pg/testdata/walk_through.yaml
index 6ed9019aa9..b6d0c9c284 100644
--- a/backend/plugin/schema/pg/testdata/walk_through.yaml
+++ b/backend/plugin/schema/pg/testdata/walk_through.yaml
@@ -30,13 +30,13 @@
{
"name": "id",
"position": 1,
- "type": "integer"
+ "type": "int"
},
{
"name": "name",
"position": 2,
"nullable": true,
- "type": "character varying(20)"
+ "type": "varchar(20)"
}
]
}
@@ -44,12 +44,17 @@
"views": [
{
"name": "v1",
- "definition": " SELECT id,\n name\n FROM test;",
+ "definition": "SELECT id, name FROM test",
"dependencyColumns": [
{
"schema": "public",
"table": "test",
- "column": "*"
+ "column": "id"
+ },
+ {
+ "schema": "public",
+ "table": "test",
+ "column": "name"
}
]
}
diff --git a/backend/plugin/schema/pg/walk_through_apply.go b/backend/plugin/schema/pg/walk_through_apply.go
new file mode 100644
index 0000000000..40dba185da
--- /dev/null
+++ b/backend/plugin/schema/pg/walk_through_apply.go
@@ -0,0 +1,690 @@
+package pg
+
+import (
+ "fmt"
+ "strings"
+
+ "google.golang.org/protobuf/proto"
+
+ "github.com/bytebase/omni/pg/catalog"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+// applyDiffToMetadata applies a SchemaDiff (from user DDL execution) to a
+// clone of the original metadata proto, producing the post-DDL metadata.
+// The original proto is NOT modified.
+func applyDiffToMetadata(original *storepb.DatabaseSchemaMetadata, catBefore, catAfter *catalog.Catalog, diff *catalog.SchemaDiff) *storepb.DatabaseSchemaMetadata {
+ if diff == nil || diff.IsEmpty() {
+ return original
+ }
+
+ result, ok := proto.Clone(original).(*storepb.DatabaseSchemaMetadata)
+ if !ok {
+ return original
+ }
+
+ for _, s := range diff.Schemas {
+ switch s.Action {
+ case catalog.DiffAdd:
+ result.Schemas = append(result.Schemas, &storepb.SchemaMetadata{Name: s.Name})
+ case catalog.DiffDrop:
+ result.Schemas = removeSchema(result.Schemas, s.Name)
+ default:
+ }
+ }
+
+ for _, rel := range diff.Relations {
+ if rel.Action == catalog.DiffDrop {
+ if sm := findSchema(result, rel.SchemaName); sm != nil {
+ dropRelation(sm, rel)
+ }
+ continue
+ }
+ sm := findOrCreateSchema(result, rel.SchemaName)
+ switch rel.Action {
+ case catalog.DiffAdd:
+ addRelation(sm, catAfter, rel)
+ case catalog.DiffModify:
+ modifyRelation(sm, catBefore, catAfter, rel)
+ default:
+ }
+ }
+
+ for _, seq := range diff.Sequences {
+ if seq.Action == catalog.DiffDrop {
+ if sm := findSchema(result, seq.SchemaName); sm != nil {
+ sm.Sequences = removeSequenceByName(sm.Sequences, seq.Name)
+ }
+ continue
+ }
+ sm := findOrCreateSchema(result, seq.SchemaName)
+ switch seq.Action {
+ case catalog.DiffAdd:
+ if seq.To != nil {
+ sm.Sequences = append(sm.Sequences, sequenceToProto(catAfter, seq.To))
+ }
+ case catalog.DiffModify:
+ if seq.To != nil {
+ for i, s := range sm.Sequences {
+ if s.Name == seq.Name {
+ sm.Sequences[i] = sequenceToProto(catAfter, seq.To)
+ break
+ }
+ }
+ }
+ default:
+ }
+ }
+
+ for _, e := range diff.Enums {
+ if e.Action == catalog.DiffDrop {
+ if sm := findSchema(result, e.SchemaName); sm != nil {
+ sm.EnumTypes = removeEnumByName(sm.EnumTypes, e.Name)
+ }
+ continue
+ }
+ sm := findOrCreateSchema(result, e.SchemaName)
+ switch e.Action {
+ case catalog.DiffAdd:
+ sm.EnumTypes = append(sm.EnumTypes, &storepb.EnumTypeMetadata{
+ Name: e.Name,
+ Values: e.ToValues,
+ })
+ case catalog.DiffModify:
+ for i, et := range sm.EnumTypes {
+ if et.Name == e.Name {
+ sm.EnumTypes[i].Values = e.ToValues
+ break
+ }
+ }
+ default:
+ }
+ }
+
+ for _, f := range diff.Functions {
+ isProcedure := f.To != nil && f.To.Kind == 'p'
+ wasProcedure := f.From != nil && f.From.Kind == 'p'
+ if f.Action == catalog.DiffDrop {
+ if sm := findSchema(result, f.SchemaName); sm != nil {
+ if wasProcedure {
+ sm.Procedures = removeProcedureByIdentity(sm.Procedures, f.Identity)
+ } else {
+ sm.Functions = removeFunctionByIdentity(sm.Functions, f.Identity)
+ }
+ }
+ continue
+ }
+ sm := findOrCreateSchema(result, f.SchemaName)
+ switch f.Action {
+ case catalog.DiffAdd:
+ if f.To != nil {
+ if isProcedure {
+ sm.Procedures = append(sm.Procedures, procedureToProto(catAfter, f.To, f.Identity))
+ } else {
+ sm.Functions = append(sm.Functions, functionToProto(catAfter, f.To, f.Identity))
+ }
+ }
+ case catalog.DiffModify:
+ if wasProcedure {
+ sm.Procedures = removeProcedureByIdentity(sm.Procedures, f.Identity)
+ } else {
+ sm.Functions = removeFunctionByIdentity(sm.Functions, f.Identity)
+ }
+ if f.To != nil {
+ if isProcedure {
+ sm.Procedures = append(sm.Procedures, procedureToProto(catAfter, f.To, f.Identity))
+ } else {
+ sm.Functions = append(sm.Functions, functionToProto(catAfter, f.To, f.Identity))
+ }
+ }
+ default:
+ }
+ }
+
+ return result
+}
+
+func findSchema(meta *storepb.DatabaseSchemaMetadata, name string) *storepb.SchemaMetadata {
+ for _, s := range meta.Schemas {
+ if s.Name == name {
+ return s
+ }
+ }
+ return nil
+}
+
+func findOrCreateSchema(meta *storepb.DatabaseSchemaMetadata, name string) *storepb.SchemaMetadata {
+ for _, s := range meta.Schemas {
+ if s.Name == name {
+ return s
+ }
+ }
+ s := &storepb.SchemaMetadata{Name: name}
+ meta.Schemas = append(meta.Schemas, s)
+ return s
+}
+
+func removeSchema(schemas []*storepb.SchemaMetadata, name string) []*storepb.SchemaMetadata {
+ out := make([]*storepb.SchemaMetadata, 0, len(schemas))
+ for _, s := range schemas {
+ if s.Name != name {
+ out = append(out, s)
+ }
+ }
+ return out
+}
+
+func addRelation(sm *storepb.SchemaMetadata, cat *catalog.Catalog, rel catalog.RelationDiffEntry) {
+ if rel.To == nil {
+ return
+ }
+ switch rel.To.RelKind {
+ case 'r', 'p', 'f':
+ sm.Tables = append(sm.Tables, relationToTableProto(cat, rel.To))
+ case 'v':
+ sm.Views = append(sm.Views, relationToViewProto(cat, rel.To))
+ case 'm':
+ sm.MaterializedViews = append(sm.MaterializedViews, relationToMatViewProto(cat, rel.To))
+ default:
+ }
+}
+
+func dropRelation(sm *storepb.SchemaMetadata, rel catalog.RelationDiffEntry) {
+ name := rel.Name
+ sm.Tables = removeTableByName(sm.Tables, name)
+ sm.Views = removeViewByName(sm.Views, name)
+ sm.MaterializedViews = removeMatViewByName(sm.MaterializedViews, name)
+}
+
+func modifyRelation(sm *storepb.SchemaMetadata, catBefore, cat *catalog.Catalog, rel catalog.RelationDiffEntry) {
+ if rel.To == nil {
+ return
+ }
+ switch rel.To.RelKind {
+ case 'r', 'p', 'f':
+ tbl := findTable(sm, rel.Name)
+ if tbl == nil {
+ sm.Tables = append(sm.Tables, relationToTableProto(cat, rel.To))
+ return
+ }
+ applyColumnDiffs(tbl, cat, rel)
+ applyIndexDiffs(tbl, cat, rel)
+ applyConstraintDiffs(tbl, catBefore, cat, rel)
+ case 'v':
+ sm.Views = removeViewByName(sm.Views, rel.Name)
+ sm.Views = append(sm.Views, relationToViewProto(cat, rel.To))
+ case 'm':
+ sm.MaterializedViews = removeMatViewByName(sm.MaterializedViews, rel.Name)
+ sm.MaterializedViews = append(sm.MaterializedViews, relationToMatViewProto(cat, rel.To))
+ default:
+ }
+}
+
+func applyColumnDiffs(tbl *storepb.TableMetadata, cat *catalog.Catalog, rel catalog.RelationDiffEntry) {
+ for _, cd := range rel.Columns {
+ switch cd.Action {
+ case catalog.DiffAdd:
+ if cd.To != nil {
+ tbl.Columns = append(tbl.Columns, columnToProto(cat, cd.To))
+ }
+ case catalog.DiffDrop:
+ tbl.Columns = removeColumnByName(tbl.Columns, cd.Name)
+ case catalog.DiffModify:
+ if cd.To != nil {
+ for i, col := range tbl.Columns {
+ if col.Name == cd.Name {
+ tbl.Columns[i] = columnToProto(cat, cd.To)
+ break
+ }
+ }
+ }
+ default:
+ }
+ }
+}
+
+func applyIndexDiffs(tbl *storepb.TableMetadata, cat *catalog.Catalog, rel catalog.RelationDiffEntry) {
+ for _, id := range rel.Indexes {
+ switch id.Action {
+ case catalog.DiffAdd:
+ if id.To != nil {
+ tbl.Indexes = append(tbl.Indexes, indexToProto(cat, rel.To, id.To))
+ }
+ case catalog.DiffDrop:
+ tbl.Indexes = removeIndexByName(tbl.Indexes, id.Name)
+ case catalog.DiffModify:
+ if id.To != nil {
+ for i, idx := range tbl.Indexes {
+ if idx.Name == id.Name {
+ tbl.Indexes[i] = indexToProto(cat, rel.To, id.To)
+ break
+ }
+ }
+ }
+ default:
+ }
+ }
+}
+
+func applyConstraintDiffs(tbl *storepb.TableMetadata, catBefore, catAfter *catalog.Catalog, rel catalog.RelationDiffEntry) {
+ for _, cd := range rel.Constraints {
+ switch cd.Action {
+ case catalog.DiffAdd:
+ if cd.To != nil {
+ addConstraintToTable(tbl, catAfter, rel, cd.To)
+ }
+ case catalog.DiffDrop:
+ if cd.From != nil {
+ removeConstraintFromTable(tbl, catBefore, cd.From)
+ }
+ case catalog.DiffModify:
+ if cd.From != nil {
+ removeConstraintFromTable(tbl, catBefore, cd.From)
+ }
+ if cd.To != nil {
+ addConstraintToTable(tbl, catAfter, rel, cd.To)
+ }
+ default:
+ }
+ }
+}
+
+func addConstraintToTable(tbl *storepb.TableMetadata, cat *catalog.Catalog, rel catalog.RelationDiffEntry, con *catalog.Constraint) {
+ switch con.Type {
+ case catalog.ConstraintPK, catalog.ConstraintUnique:
+ idx := cat.GetIndexByOID(con.IndexOID)
+ if idx != nil && rel.To != nil {
+ tbl.Indexes = append(tbl.Indexes, indexToProto(cat, rel.To, idx))
+ }
+ case catalog.ConstraintFK:
+ tbl.ForeignKeys = append(tbl.ForeignKeys, constraintToFKProto(cat, rel.To, con))
+ case catalog.ConstraintCheck:
+ tbl.CheckConstraints = append(tbl.CheckConstraints, &storepb.CheckConstraintMetadata{
+ Name: con.Name,
+ Expression: con.CheckExpr,
+ })
+ case catalog.ConstraintExclude:
+ tbl.ExcludeConstraints = append(tbl.ExcludeConstraints, &storepb.ExcludeConstraintMetadata{
+ Name: con.Name,
+ Expression: con.CheckExpr,
+ })
+ default:
+ }
+}
+
+func removeConstraintFromTable(tbl *storepb.TableMetadata, cat *catalog.Catalog, con *catalog.Constraint) {
+ switch con.Type {
+ case catalog.ConstraintPK, catalog.ConstraintUnique:
+ // Constraint name and backing index name may differ; resolve via IndexOID.
+ idxName := con.Name
+ if idx := cat.GetIndexByOID(con.IndexOID); idx != nil {
+ idxName = idx.Name
+ }
+ tbl.Indexes = removeIndexByName(tbl.Indexes, idxName)
+ case catalog.ConstraintFK:
+ out := make([]*storepb.ForeignKeyMetadata, 0, len(tbl.ForeignKeys))
+ for _, fk := range tbl.ForeignKeys {
+ if fk.Name != con.Name {
+ out = append(out, fk)
+ }
+ }
+ tbl.ForeignKeys = out
+ case catalog.ConstraintCheck:
+ out := make([]*storepb.CheckConstraintMetadata, 0, len(tbl.CheckConstraints))
+ for _, c := range tbl.CheckConstraints {
+ if c.Name != con.Name {
+ out = append(out, c)
+ }
+ }
+ tbl.CheckConstraints = out
+ case catalog.ConstraintExclude:
+ out := make([]*storepb.ExcludeConstraintMetadata, 0, len(tbl.ExcludeConstraints))
+ for _, c := range tbl.ExcludeConstraints {
+ if c.Name != con.Name {
+ out = append(out, c)
+ }
+ }
+ tbl.ExcludeConstraints = out
+ default:
+ }
+}
+
+// --- Conversion helpers: omni types → proto types ---
+
+func columnToProto(cat *catalog.Catalog, col *catalog.Column) *storepb.ColumnMetadata {
+ cm := &storepb.ColumnMetadata{
+ Name: col.Name,
+ Position: int32(col.AttNum),
+ Type: cat.FormatType(col.TypeOID, col.TypeMod),
+ Nullable: !col.NotNull,
+ Default: col.Default,
+ }
+ if col.Generated == 's' {
+ cm.Generation = &storepb.GenerationMetadata{
+ Type: storepb.GenerationMetadata_TYPE_STORED,
+ Expression: col.GenerationExpr,
+ }
+ }
+ if col.Identity != 0 {
+ cm.IsIdentity = true
+ switch col.Identity {
+ case 'a':
+ cm.IdentityGeneration = storepb.ColumnMetadata_ALWAYS
+ case 'd':
+ cm.IdentityGeneration = storepb.ColumnMetadata_BY_DEFAULT
+ default:
+ }
+ }
+ return cm
+}
+
+func indexToProto(_ *catalog.Catalog, rel *catalog.Relation, idx *catalog.Index) *storepb.IndexMetadata {
+ im := &storepb.IndexMetadata{
+ Name: idx.Name,
+ Type: idx.AccessMethod,
+ Unique: idx.IsUnique,
+ Primary: idx.IsPrimary,
+ IsConstraint: idx.ConstraintOID != 0,
+ }
+
+ exprIdx := 0
+ for i, attnum := range idx.Columns {
+ if attnum == 0 {
+ if exprIdx < len(idx.Exprs) {
+ im.Expressions = append(im.Expressions, idx.Exprs[exprIdx])
+ exprIdx++
+ }
+ } else {
+ colName := ""
+ if rel != nil {
+ for _, col := range rel.Columns {
+ if col.AttNum == attnum {
+ colName = col.Name
+ break
+ }
+ }
+ }
+ im.Expressions = append(im.Expressions, colName)
+ }
+
+ if i < len(idx.IndOption) {
+ im.Descending = append(im.Descending, idx.IndOption[i]&1 != 0)
+ }
+ }
+
+ return im
+}
+
+func constraintToFKProto(cat *catalog.Catalog, rel *catalog.Relation, con *catalog.Constraint) *storepb.ForeignKeyMetadata {
+ fk := &storepb.ForeignKeyMetadata{
+ Name: con.Name,
+ }
+
+ for _, attnum := range con.Columns {
+ if rel != nil {
+ for _, col := range rel.Columns {
+ if col.AttNum == attnum {
+ fk.Columns = append(fk.Columns, col.Name)
+ break
+ }
+ }
+ }
+ }
+
+ fRel := cat.GetRelationByOID(con.FRelOID)
+ if fRel != nil {
+ if fRel.Schema != nil {
+ fk.ReferencedSchema = fRel.Schema.Name
+ }
+ fk.ReferencedTable = fRel.Name
+ for _, attnum := range con.FColumns {
+ for _, col := range fRel.Columns {
+ if col.AttNum == attnum {
+ fk.ReferencedColumns = append(fk.ReferencedColumns, col.Name)
+ break
+ }
+ }
+ }
+ }
+
+ fk.OnUpdate = wtFKActionToString(con.FKUpdAction)
+ fk.OnDelete = wtFKActionToString(con.FKDelAction)
+ fk.MatchType = wtFKMatchToString(con.FKMatchType)
+ return fk
+}
+
+func sequenceToProto(cat *catalog.Catalog, seq *catalog.Sequence) *storepb.SequenceMetadata {
+ return &storepb.SequenceMetadata{
+ Name: seq.Name,
+ DataType: cat.FormatType(seq.TypeOID, -1),
+ Start: fmt.Sprintf("%d", seq.Start),
+ MinValue: fmt.Sprintf("%d", seq.MinValue),
+ MaxValue: fmt.Sprintf("%d", seq.MaxValue),
+ Increment: fmt.Sprintf("%d", seq.Increment),
+ Cycle: seq.Cycle,
+ CacheSize: fmt.Sprintf("%d", seq.CacheValue),
+ }
+}
+
+func wtFKActionToString(action byte) string {
+ switch action {
+ case 'r':
+ return "RESTRICT"
+ case 'c':
+ return "CASCADE"
+ case 'n':
+ return "SET NULL"
+ case 'd':
+ return "SET DEFAULT"
+ default:
+ return "NO ACTION"
+ }
+}
+
+func wtFKMatchToString(match byte) string {
+ switch match {
+ case 'f':
+ return "FULL"
+ case 'p':
+ return "PARTIAL"
+ default:
+ return "SIMPLE"
+ }
+}
+
+// --- Slice helpers ---
+
+func findTable(sm *storepb.SchemaMetadata, name string) *storepb.TableMetadata {
+ for _, t := range sm.Tables {
+ if t.Name == name {
+ return t
+ }
+ }
+ return nil
+}
+
+func removeTableByName(tables []*storepb.TableMetadata, name string) []*storepb.TableMetadata {
+ out := make([]*storepb.TableMetadata, 0, len(tables))
+ for _, t := range tables {
+ if t.Name != name {
+ out = append(out, t)
+ }
+ }
+ return out
+}
+
+func removeViewByName(views []*storepb.ViewMetadata, name string) []*storepb.ViewMetadata {
+ out := make([]*storepb.ViewMetadata, 0, len(views))
+ for _, v := range views {
+ if v.Name != name {
+ out = append(out, v)
+ }
+ }
+ return out
+}
+
+func removeMatViewByName(mvs []*storepb.MaterializedViewMetadata, name string) []*storepb.MaterializedViewMetadata {
+ out := make([]*storepb.MaterializedViewMetadata, 0, len(mvs))
+ for _, m := range mvs {
+ if m.Name != name {
+ out = append(out, m)
+ }
+ }
+ return out
+}
+
+func removeSequenceByName(seqs []*storepb.SequenceMetadata, name string) []*storepb.SequenceMetadata {
+ out := make([]*storepb.SequenceMetadata, 0, len(seqs))
+ for _, s := range seqs {
+ if s.Name != name {
+ out = append(out, s)
+ }
+ }
+ return out
+}
+
+func removeColumnByName(cols []*storepb.ColumnMetadata, name string) []*storepb.ColumnMetadata {
+ out := make([]*storepb.ColumnMetadata, 0, len(cols))
+ for _, c := range cols {
+ if c.Name != name {
+ out = append(out, c)
+ }
+ }
+ return out
+}
+
+func removeIndexByName(indexes []*storepb.IndexMetadata, name string) []*storepb.IndexMetadata {
+ out := make([]*storepb.IndexMetadata, 0, len(indexes))
+ for _, i := range indexes {
+ if i.Name != name {
+ out = append(out, i)
+ }
+ }
+ return out
+}
+
+func removeEnumByName(enums []*storepb.EnumTypeMetadata, name string) []*storepb.EnumTypeMetadata {
+ out := make([]*storepb.EnumTypeMetadata, 0, len(enums))
+ for _, e := range enums {
+ if e.Name != name {
+ out = append(out, e)
+ }
+ }
+ return out
+}
+
+func functionToProto(cat *catalog.Catalog, up *catalog.UserProc, identity string) *storepb.FunctionMetadata {
+ return &storepb.FunctionMetadata{
+ Name: up.Name,
+ Definition: buildUserProcDDL(cat, up),
+ Signature: identity,
+ }
+}
+
+func procedureToProto(cat *catalog.Catalog, up *catalog.UserProc, identity string) *storepb.ProcedureMetadata {
+ return &storepb.ProcedureMetadata{
+ Name: up.Name,
+ Definition: buildUserProcDDL(cat, up),
+ Signature: identity,
+ }
+}
+
+func buildUserProcDDL(cat *catalog.Catalog, up *catalog.UserProc) string {
+ var b strings.Builder
+ if up.Kind == 'p' {
+ b.WriteString("CREATE OR REPLACE PROCEDURE ")
+ } else {
+ b.WriteString("CREATE OR REPLACE FUNCTION ")
+ }
+ if up.Schema != nil && up.Schema.Name != "" {
+ b.WriteString(up.Schema.Name)
+ b.WriteByte('.')
+ }
+ b.WriteString(up.Name)
+ b.WriteByte('(')
+ argTypes := up.ArgTypes
+ if len(up.AllArgTypes) > 0 {
+ argTypes = up.AllArgTypes
+ }
+ for i, t := range argTypes {
+ if i > 0 {
+ b.WriteString(", ")
+ }
+ if i < len(up.ArgModes) {
+ switch up.ArgModes[i] {
+ case 'o':
+ b.WriteString("OUT ")
+ case 'b':
+ b.WriteString("INOUT ")
+ case 'v':
+ b.WriteString("VARIADIC ")
+ default:
+ }
+ }
+ if i < len(up.ArgNames) && up.ArgNames[i] != "" {
+ b.WriteString(up.ArgNames[i])
+ b.WriteByte(' ')
+ }
+ b.WriteString(cat.FormatType(t, -1))
+ }
+ b.WriteString(")\n")
+ if up.Kind != 'p' {
+ b.WriteString(" RETURNS ")
+ if up.RetSet {
+ b.WriteString("SETOF ")
+ }
+ b.WriteString(cat.FormatType(up.RetType, -1))
+ b.WriteByte('\n')
+ }
+ b.WriteString(" LANGUAGE ")
+ b.WriteString(up.Language)
+ b.WriteByte('\n')
+ switch up.Volatile {
+ case 'i':
+ b.WriteString(" IMMUTABLE\n")
+ case 's':
+ b.WriteString(" STABLE\n")
+ default:
+ }
+ if up.IsStrict {
+ b.WriteString(" STRICT\n")
+ }
+ if up.SecDef {
+ b.WriteString(" SECURITY DEFINER\n")
+ }
+ if up.LeakProof {
+ b.WriteString(" LEAKPROOF\n")
+ }
+ switch up.Parallel {
+ case 's':
+ b.WriteString(" PARALLEL SAFE\n")
+ case 'r':
+ b.WriteString(" PARALLEL RESTRICTED\n")
+ default:
+ }
+ b.WriteString("AS $function$")
+ b.WriteString(up.Body)
+ b.WriteString("$function$\n")
+ return b.String()
+}
+
+func removeProcedureByIdentity(procs []*storepb.ProcedureMetadata, identity string) []*storepb.ProcedureMetadata {
+ out := make([]*storepb.ProcedureMetadata, 0, len(procs))
+ for _, p := range procs {
+ if p.Signature != identity {
+ out = append(out, p)
+ }
+ }
+ return out
+}
+
+func removeFunctionByIdentity(funcs []*storepb.FunctionMetadata, identity string) []*storepb.FunctionMetadata {
+ out := make([]*storepb.FunctionMetadata, 0, len(funcs))
+ for _, f := range funcs {
+ if f.Signature != identity {
+ out = append(out, f)
+ }
+ }
+ return out
+}
diff --git a/backend/plugin/schema/pg/walk_through_clone_test.go b/backend/plugin/schema/pg/walk_through_clone_test.go
new file mode 100644
index 0000000000..0f581fdd9c
--- /dev/null
+++ b/backend/plugin/schema/pg/walk_through_clone_test.go
@@ -0,0 +1,215 @@
+package pg
+
+import (
+ "context"
+ "testing"
+
+ "github.com/bytebase/omni/pg/catalog"
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+// TestClone_BasicIsolation verifies that Clone produces an independent catalog:
+// mutations on the clone do not affect the original.
+func TestClone_BasicIsolation(t *testing.T) {
+ original := catalog.New()
+ _, err := original.Exec(`
+ CREATE SCHEMA test_schema;
+ CREATE TABLE test_schema.t1 (id serial PRIMARY KEY, name text NOT NULL);
+ CREATE INDEX t1_name_idx ON test_schema.t1 (name);
+ `, nil)
+ require.NoError(t, err)
+
+ clone := original.Clone()
+
+ // Mutate clone
+ _, err = clone.Exec(`
+ ALTER TABLE test_schema.t1 ADD COLUMN email text;
+ CREATE TABLE test_schema.t2 (id int PRIMARY KEY);
+ DROP INDEX test_schema.t1_name_idx;
+ `, nil)
+ require.NoError(t, err)
+
+ // Verify original is untouched
+ origRel := original.GetRelation("test_schema", "t1")
+ require.NotNil(t, origRel, "original t1 should still exist")
+ require.Equal(t, 2, len(origRel.Columns), "original t1 should have 2 columns (id, name), not 3")
+
+ require.Nil(t, original.GetRelation("test_schema", "t2"), "original should NOT have t2")
+
+ origIndexes := original.IndexesOf(origRel.OID)
+ found := false
+ for _, idx := range origIndexes {
+ if idx.Name == "t1_name_idx" {
+ found = true
+ }
+ }
+ require.True(t, found, "original should still have t1_name_idx")
+
+ // Verify clone has the changes
+ cloneRel := clone.GetRelation("test_schema", "t1")
+ require.NotNil(t, cloneRel)
+ require.Equal(t, 3, len(cloneRel.Columns), "clone t1 should have 3 columns")
+ require.NotNil(t, clone.GetRelation("test_schema", "t2"), "clone should have t2")
+
+ // Diff should show changes
+ diff := catalog.Diff(original, clone)
+ require.False(t, diff.IsEmpty(), "diff should not be empty")
+ require.Greater(t, len(diff.Relations), 0, "should have relation diffs")
+}
+
+// TestClone_WalkThroughIntegration tests the full walk-through flow using Clone:
+// load catalog → clone → exec user DDL on clone → diff → apply.
+func TestClone_WalkThroughIntegration(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "postgres",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "name", Type: "text", Position: 2, Nullable: true},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {Name: "users_pkey", Expressions: []string{"id"}, Unique: true, Primary: true},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ catBefore := catalog.New()
+ err := loadWalkThroughCatalog(context.Background(), catBefore, meta)
+ require.NoError(t, err)
+
+ catAfter := catBefore.Clone()
+
+ userSQL := `
+ ALTER TABLE public.users ADD COLUMN email text NOT NULL DEFAULT 'unknown';
+ CREATE INDEX users_email_idx ON public.users (email);
+ CREATE TABLE public.posts (id serial PRIMARY KEY, user_id int REFERENCES public.users(id), title text);
+ `
+ results, err := catAfter.Exec(userSQL, &catalog.ExecOptions{ContinueOnError: true})
+ require.NoError(t, err)
+ for _, r := range results {
+ require.NoError(t, r.Error, "DDL should succeed: %s", r.SQL)
+ }
+
+ // Diff
+ diff := catalog.Diff(catBefore, catAfter)
+ require.False(t, diff.IsEmpty())
+
+ // Apply diff to original metadata
+ newProto := applyDiffToMetadata(meta, catBefore, catAfter, diff)
+ newMeta := model.NewDatabaseMetadata(newProto, nil, nil, storepb.Engine_POSTGRES, true)
+
+ // Verify: users table should have 3 columns
+ usersSchema := newMeta.GetSchemaMetadata("public")
+ require.NotNil(t, usersSchema)
+ usersTbl := usersSchema.GetTable("users")
+ require.NotNil(t, usersTbl)
+ require.Equal(t, 3, len(usersTbl.GetProto().Columns), "users should have id, name, email")
+
+ // Verify: users should have 2 indexes (pkey + email)
+ require.GreaterOrEqual(t, len(usersTbl.GetProto().Indexes), 2)
+
+ // Verify: posts table should exist
+ postsTbl := usersSchema.GetTable("posts")
+ require.NotNil(t, postsTbl, "posts table should exist")
+
+ // Verify: original metadata should NOT have posts or email column
+ origMeta := model.NewDatabaseMetadata(meta, nil, nil, storepb.Engine_POSTGRES, true)
+ origUsers := origMeta.GetSchemaMetadata("public").GetTable("users")
+ require.Equal(t, 2, len(origUsers.GetProto().Columns), "original should still have 2 columns")
+ require.Nil(t, origMeta.GetSchemaMetadata("public").GetTable("posts"), "original should not have posts")
+}
+
+// TestClone_SearchPath tests Clone preserves search path and session user.
+func TestClone_SearchPath(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "postgres",
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ {Name: "alice"},
+ },
+ }
+
+ catBefore := catalog.New()
+ catBefore.SetSessionUser("alice")
+ catBefore.SetSearchPath([]string{"$user", "public"})
+ err := loadWalkThroughCatalog(context.Background(), catBefore, meta)
+ require.NoError(t, err)
+
+ catAfter := catBefore.Clone()
+
+ // CREATE TABLE without schema should use search path ($user → alice)
+ _, err = catAfter.Exec(`CREATE TABLE my_table (id int);`, nil)
+ require.NoError(t, err)
+
+ // Table should be in alice schema on clone
+ require.NotNil(t, catAfter.GetRelation("alice", "my_table"))
+
+ // Original should NOT have it
+ require.Nil(t, catBefore.GetRelation("alice", "my_table"))
+
+ // Diff should show the new table
+ diff := catalog.Diff(catBefore, catAfter)
+ require.False(t, diff.IsEmpty())
+
+ foundAliceTable := false
+ for _, rel := range diff.Relations {
+ if rel.SchemaName == "alice" && rel.Name == "my_table" && rel.Action == catalog.DiffAdd {
+ foundAliceTable = true
+ }
+ }
+ require.True(t, foundAliceTable, "diff should show alice.my_table as added")
+}
+
+// TestClone_WalkThroughOmniFunction tests the actual WalkThroughOmni function
+// using Clone (if enabled) produces correct FinalMetadata.
+func TestClone_WalkThroughOmniFunction(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "postgres",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "test",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "name", Type: "text", Position: 2, Nullable: true},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {Name: "test_pkey", Expressions: []string{"id"}, Unique: true, Primary: true},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ state := model.NewDatabaseMetadata(meta, nil, nil, storepb.Engine_POSTGRES, true)
+ ctx := schema.WalkThroughContext{
+ RawSQL: `CREATE TABLE public.new_table (id int PRIMARY KEY, val text);`,
+ }
+
+ advice := WalkThroughOmni(ctx, state, nil)
+ require.Nil(t, advice, "walk-through should succeed")
+
+ // Check FinalMetadata has the new table
+ newTbl := state.GetSchemaMetadata("public").GetTable("new_table")
+ require.NotNil(t, newTbl, "new_table should exist in FinalMetadata")
+
+ // Check original table is preserved
+ origTbl := state.GetSchemaMetadata("public").GetTable("test")
+ require.NotNil(t, origTbl, "test table should still exist")
+ require.Equal(t, 2, len(origTbl.GetProto().Columns))
+}
diff --git a/backend/plugin/schema/pg/walk_through_loader.go b/backend/plugin/schema/pg/walk_through_loader.go
new file mode 100644
index 0000000000..7df2aa9d99
--- /dev/null
+++ b/backend/plugin/schema/pg/walk_through_loader.go
@@ -0,0 +1,1412 @@
+//nolint:unused
+package pg
+
+import (
+ "cmp"
+ "context"
+ "fmt"
+ "regexp"
+ "slices"
+ "strings"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/omni/pg/ast"
+ "github.com/bytebase/omni/pg/catalog"
+ omniparser "github.com/bytebase/omni/pg/parser"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+// loadWalkThroughCatalog installs every schema object from DatabaseSchemaMetadata
+// into the omni catalog in dependency-topological order with pseudo fallback.
+// Indexes and constraints are installed after their parent tables/views.
+func loadWalkThroughCatalog(ctx context.Context, cat *catalog.Catalog, meta *storepb.DatabaseSchemaMetadata) error {
+ if cat == nil {
+ return errors.New("loadWalkThroughCatalog: nil catalog")
+ }
+ if meta == nil {
+ return nil
+ }
+
+ objects := wtCollectObjects(meta)
+ sorted := wtTopoSort(objects)
+ for _, obj := range sorted {
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+ wtInstallOne(cat, obj)
+ }
+ return nil
+}
+
+// wtObjectKind identifies the kind of a walk-through loader object.
+type wtObjectKind int
+
+const (
+ kindWTSchema wtObjectKind = iota
+ kindWTEnum
+ kindWTSequence
+ kindWTTable
+ kindWTView
+ kindWTMatView
+ kindWTFunction
+ kindWTIndex
+ kindWTConstraint
+)
+
+// wtObjectEntry is one unit of work for the walk-through loader.
+type wtObjectEntry struct {
+ kind wtObjectKind
+ schema string
+ name string
+
+ // Exactly one of the following is set based on kind.
+ enumMeta *storepb.EnumTypeMetadata
+ seqMeta *storepb.SequenceMetadata
+ tableMeta *storepb.TableMetadata
+ viewMeta *storepb.ViewMetadata
+ matViewMeta *storepb.MaterializedViewMetadata
+ funcMeta *storepb.FunctionMetadata
+ idxMeta *storepb.IndexMetadata
+ // For kindWTConstraint: the parent table metadata.
+ constraintTable *storepb.TableMetadata
+ // For kindWTConstraint, kindWTIndex: the parent relation name.
+ parentName string
+}
+
+func (e *wtObjectEntry) key() string {
+ switch e.kind {
+ case kindWTSchema:
+ return "schema:" + e.schema
+ case kindWTEnum:
+ return "type:" + e.schema + "." + e.name
+ case kindWTSequence:
+ return "seq:" + e.schema + "." + e.name
+ case kindWTTable, kindWTView, kindWTMatView:
+ return "rel:" + e.schema + "." + e.name
+ case kindWTFunction:
+ return "func:" + e.schema + "." + e.name + "|" + wtFuncSigKey(e.funcMeta)
+ case kindWTIndex:
+ return "idx:" + e.schema + "." + e.parentName + "." + e.name
+ case kindWTConstraint:
+ return "con:" + e.schema + "." + e.parentName + "." + e.name
+ }
+ return "unknown:" + e.schema + "." + e.name
+}
+
+func (e *wtObjectEntry) sortKey() string {
+ base := e.schema + "\x00" + e.name + "\x00" + wtKindLabel(e.kind)
+ if e.kind == kindWTFunction {
+ base += "\x00" + wtFuncSigKey(e.funcMeta)
+ }
+ if e.kind == kindWTIndex || e.kind == kindWTConstraint {
+ base += "\x00" + e.parentName
+ }
+ return base
+}
+
+func wtFuncSigKey(fn *storepb.FunctionMetadata) string {
+ if fn == nil {
+ return ""
+ }
+ if fn.Signature != "" {
+ return fn.Signature
+ }
+ return fn.Definition
+}
+
+func wtKindLabel(k wtObjectKind) string {
+ switch k {
+ case kindWTSchema:
+ return "0schema"
+ case kindWTEnum:
+ return "1enum"
+ case kindWTSequence:
+ return "2seq"
+ case kindWTTable:
+ return "3table"
+ case kindWTView:
+ return "4view"
+ case kindWTMatView:
+ return "5matview"
+ case kindWTFunction:
+ return "6function"
+ case kindWTIndex:
+ return "7index"
+ case kindWTConstraint:
+ return "8constraint"
+ }
+ return "9unknown"
+}
+
+// wtCollectObjects flattens DatabaseSchemaMetadata into wtObjectEntry values.
+func wtCollectObjects(meta *storepb.DatabaseSchemaMetadata) []*wtObjectEntry {
+ var out []*wtObjectEntry
+ for _, sm := range meta.Schemas {
+ if sm.Name == "" {
+ continue
+ }
+ out = append(out, &wtObjectEntry{
+ kind: kindWTSchema,
+ schema: sm.Name,
+ name: sm.Name,
+ })
+ for _, enum := range sm.EnumTypes {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTEnum,
+ schema: sm.Name,
+ name: enum.Name,
+ enumMeta: enum,
+ })
+ }
+ for _, seq := range sm.Sequences {
+ if wtIsIdentitySequence(seq, sm.Tables) {
+ continue
+ }
+ out = append(out, &wtObjectEntry{
+ kind: kindWTSequence,
+ schema: sm.Name,
+ name: seq.Name,
+ seqMeta: seq,
+ })
+ }
+ for _, tbl := range sm.Tables {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTTable,
+ schema: sm.Name,
+ name: tbl.Name,
+ tableMeta: tbl,
+ })
+ for _, idx := range tbl.Indexes {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTIndex,
+ schema: sm.Name,
+ name: idx.Name,
+ parentName: tbl.Name,
+ idxMeta: idx,
+ })
+ }
+ for _, fk := range tbl.ForeignKeys {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTConstraint,
+ schema: sm.Name,
+ name: fk.Name,
+ parentName: tbl.Name,
+ constraintTable: tbl,
+ })
+ }
+ for _, chk := range tbl.CheckConstraints {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTConstraint,
+ schema: sm.Name,
+ name: chk.Name,
+ parentName: tbl.Name,
+ constraintTable: tbl,
+ })
+ }
+ for _, exc := range tbl.ExcludeConstraints {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTConstraint,
+ schema: sm.Name,
+ name: exc.Name,
+ parentName: tbl.Name,
+ constraintTable: tbl,
+ })
+ }
+ }
+ for _, view := range sm.Views {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTView,
+ schema: sm.Name,
+ name: view.Name,
+ viewMeta: view,
+ })
+ }
+ for _, mv := range sm.MaterializedViews {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTMatView,
+ schema: sm.Name,
+ name: mv.Name,
+ matViewMeta: mv,
+ })
+ for _, idx := range mv.Indexes {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTIndex,
+ schema: sm.Name,
+ name: idx.Name,
+ parentName: mv.Name,
+ idxMeta: idx,
+ })
+ }
+ }
+ for _, fn := range sm.Functions {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTFunction,
+ schema: sm.Name,
+ name: fn.Name,
+ funcMeta: fn,
+ })
+ }
+ for _, proc := range sm.Procedures {
+ out = append(out, &wtObjectEntry{
+ kind: kindWTFunction,
+ schema: sm.Name,
+ name: proc.Name,
+ funcMeta: &storepb.FunctionMetadata{
+ Name: proc.Name,
+ Definition: proc.Definition,
+ Signature: proc.Signature,
+ },
+ })
+ }
+ }
+ return out
+}
+
+// wtTopoSort orders objects by dependency using Tarjan SCC.
+func wtTopoSort(objects []*wtObjectEntry) []*wtObjectEntry {
+ if len(objects) == 0 {
+ return nil
+ }
+ index := make(map[string]*wtObjectEntry, len(objects))
+ for _, obj := range objects {
+ index[obj.key()] = obj
+ }
+
+ edges := wtBuildEdges(objects, index)
+ sccs := wtTarjanSCC(objects, edges)
+
+ sccOf := make(map[string]int, len(objects))
+ for i, scc := range sccs {
+ for _, obj := range scc {
+ sccOf[obj.key()] = i
+ }
+ }
+
+ condensedEdges := make([][]int, len(sccs))
+ inDegree := make([]int, len(sccs))
+ seenEdge := make(map[[2]int]bool)
+ for src, dsts := range edges {
+ srcSCC, ok := sccOf[src]
+ if !ok {
+ continue
+ }
+ for _, dst := range dsts {
+ dstSCC, ok2 := sccOf[dst]
+ if !ok2 || dstSCC == srcSCC {
+ continue
+ }
+ edge := [2]int{dstSCC, srcSCC}
+ if seenEdge[edge] {
+ continue
+ }
+ seenEdge[edge] = true
+ condensedEdges[dstSCC] = append(condensedEdges[dstSCC], srcSCC)
+ inDegree[srcSCC]++
+ }
+ }
+
+ ready := make([]int, 0)
+ for i := range sccs {
+ if inDegree[i] == 0 {
+ ready = append(ready, i)
+ }
+ }
+ wtSortSCCsByMin(ready, sccs)
+
+ var flat []*wtObjectEntry
+ for len(ready) > 0 {
+ next := ready[0]
+ ready = ready[1:]
+ flat = append(flat, wtSortedSCCMembers(sccs[next])...)
+ for _, nb := range condensedEdges[next] {
+ inDegree[nb]--
+ if inDegree[nb] == 0 {
+ ready = append(ready, nb)
+ }
+ }
+ wtSortSCCsByMin(ready, sccs)
+ }
+
+ if len(flat) != len(objects) {
+ emitted := make(map[string]bool, len(flat))
+ for _, o := range flat {
+ emitted[o.key()] = true
+ }
+ var missed []*wtObjectEntry
+ for _, o := range objects {
+ if !emitted[o.key()] {
+ missed = append(missed, o)
+ }
+ }
+ slices.SortStableFunc(missed, func(a, b *wtObjectEntry) int {
+ return cmp.Compare(a.sortKey(), b.sortKey())
+ })
+ flat = append(flat, missed...)
+ }
+ return flat
+}
+
+// wtBuildEdges computes dependency edges for walk-through objects.
+func wtBuildEdges(objects []*wtObjectEntry, index map[string]*wtObjectEntry) map[string][]string {
+ edges := make(map[string][]string, len(objects))
+ for _, obj := range objects {
+ var deps []string
+
+ // Every non-schema object depends on its schema.
+ if obj.kind != kindWTSchema {
+ if key := "schema:" + obj.schema; index[key] != nil {
+ deps = append(deps, key)
+ }
+ }
+
+ switch obj.kind {
+ case kindWTTable:
+ for _, col := range obj.tableMeta.Columns {
+ for _, ref := range wtExtractUserTypeRefs(col.Type) {
+ if k := "type:" + ref.Schema + "." + ref.Name; index[k] != nil {
+ deps = append(deps, k)
+ }
+ }
+ // Sequence dependency from nextval('schema.seq'::regclass).
+ if col.Default != "" {
+ if seqKey := wtExtractSeqRef(col.Default, obj.schema, index); seqKey != "" {
+ deps = append(deps, seqKey)
+ }
+ }
+ }
+ case kindWTView:
+ for _, dep := range obj.viewMeta.DependencyColumns {
+ if k := "rel:" + dep.Schema + "." + dep.Table; index[k] != nil {
+ deps = append(deps, k)
+ }
+ }
+ case kindWTMatView:
+ for _, dep := range obj.matViewMeta.DependencyColumns {
+ if k := "rel:" + dep.Schema + "." + dep.Table; index[k] != nil {
+ deps = append(deps, k)
+ }
+ }
+ case kindWTFunction:
+ argTypes, _ := wtParseFuncArgTypes(obj.funcMeta.Signature)
+ for _, at := range argTypes {
+ for _, ref := range wtExtractUserTypeRefs(at) {
+ if k := "type:" + ref.Schema + "." + ref.Name; index[k] != nil {
+ deps = append(deps, k)
+ }
+ }
+ }
+ case kindWTIndex:
+ if k := "rel:" + obj.schema + "." + obj.parentName; index[k] != nil {
+ deps = append(deps, k)
+ }
+ case kindWTConstraint:
+ if k := "rel:" + obj.schema + "." + obj.parentName; index[k] != nil {
+ deps = append(deps, k)
+ }
+ if obj.constraintTable != nil {
+ for _, fk := range obj.constraintTable.ForeignKeys {
+ if fk.Name == obj.name {
+ refSchema := fk.ReferencedSchema
+ if refSchema == "" {
+ refSchema = obj.schema
+ }
+ if k := "rel:" + refSchema + "." + fk.ReferencedTable; index[k] != nil {
+ deps = append(deps, k)
+ }
+ // FK depends on referenced table's PK/Unique indexes.
+ for _, o := range objects {
+ if o.kind == kindWTIndex && o.schema == refSchema &&
+ o.parentName == fk.ReferencedTable &&
+ (o.idxMeta.Primary || (o.idxMeta.IsConstraint && o.idxMeta.Unique)) {
+ if index[o.key()] != nil {
+ deps = append(deps, o.key())
+ }
+ }
+ }
+ break
+ }
+ }
+ }
+ default:
+ // kindWTSchema, kindWTEnum, kindWTSequence have no additional deps.
+ }
+
+ if len(deps) > 0 {
+ edges[obj.key()] = wtDedupStrings(deps)
+ }
+ }
+ return edges
+}
+
+// wtTarjanSCC runs Tarjan's SCC algorithm over walk-through objects.
+func wtTarjanSCC(objects []*wtObjectEntry, edges map[string][]string) [][]*wtObjectEntry {
+ type state struct {
+ index, low int
+ onStack bool
+ }
+ st := make(map[string]*state, len(objects))
+ byKey := make(map[string]*wtObjectEntry, len(objects))
+ for _, obj := range objects {
+ byKey[obj.key()] = obj
+ }
+
+ keys := make([]string, 0, len(objects))
+ for _, obj := range objects {
+ keys = append(keys, obj.key())
+ }
+ slices.Sort(keys)
+
+ var (
+ idx int
+ stack []string
+ result [][]*wtObjectEntry
+ )
+
+ var strongconnect func(v string)
+ strongconnect = func(v string) {
+ st[v] = &state{index: idx, low: idx, onStack: true}
+ idx++
+ stack = append(stack, v)
+ for _, w := range edges[v] {
+ if _, ok := byKey[w]; !ok {
+ continue
+ }
+ if _, seen := st[w]; !seen {
+ strongconnect(w)
+ st[v].low = min(st[v].low, st[w].low)
+ } else if st[w].onStack {
+ st[v].low = min(st[v].low, st[w].index)
+ }
+ }
+ if st[v].low == st[v].index {
+ var scc []*wtObjectEntry
+ for {
+ w := stack[len(stack)-1]
+ stack = stack[:len(stack)-1]
+ st[w].onStack = false
+ scc = append(scc, byKey[w])
+ if w == v {
+ break
+ }
+ }
+ result = append(result, scc)
+ }
+ }
+
+ for _, k := range keys {
+ if _, seen := st[k]; !seen {
+ strongconnect(k)
+ }
+ }
+ return result
+}
+
+func wtSortedSCCMembers(scc []*wtObjectEntry) []*wtObjectEntry {
+ out := make([]*wtObjectEntry, len(scc))
+ copy(out, scc)
+ slices.SortStableFunc(out, func(a, b *wtObjectEntry) int {
+ return cmp.Compare(a.sortKey(), b.sortKey())
+ })
+ return out
+}
+
+func wtSortSCCsByMin(indices []int, sccs [][]*wtObjectEntry) {
+ slices.SortStableFunc(indices, func(a, b int) int {
+ return cmp.Compare(wtMinSortKey(sccs[a]), wtMinSortKey(sccs[b]))
+ })
+}
+
+func wtMinSortKey(scc []*wtObjectEntry) string {
+ if len(scc) == 0 {
+ return ""
+ }
+ best := scc[0].sortKey()
+ for _, o := range scc[1:] {
+ if k := o.sortKey(); k < best {
+ best = k
+ }
+ }
+ return best
+}
+
+func wtDedupStrings(in []string) []string {
+ if len(in) < 2 {
+ return in
+ }
+ seen := make(map[string]bool, len(in))
+ out := in[:0]
+ for _, s := range in {
+ if !seen[s] {
+ seen[s] = true
+ out = append(out, s)
+ }
+ }
+ return out
+}
+
+// wtInstallOne attempts real install, falls back to pseudo for tables/views/matviews.
+func wtInstallOne(cat *catalog.Catalog, obj *wtObjectEntry) {
+ if err := wtInstallReal(cat, obj); err == nil {
+ return
+ }
+ // Only pseudo-fallback for relations; other kinds are left uninstalled on failure.
+ switch obj.kind {
+ case kindWTTable:
+ cols := wtColNames(obj.tableMeta)
+ _ = cat.DefineRelation(wtPseudoTableStmt(obj.schema, obj.name, cols), 'r')
+ case kindWTView:
+ cols := wtViewColNames(obj.viewMeta)
+ if stmt, err := wtPseudoViewStmt(obj.schema, obj.name, cols); err == nil {
+ _ = cat.DefineView(stmt)
+ }
+ case kindWTMatView:
+ cols := wtMatViewColNames(obj.matViewMeta)
+ if stmt, err := wtPseudoMatViewStmt(obj.schema, obj.name, cols); err == nil {
+ _ = cat.ExecCreateTableAs(stmt)
+ }
+ default:
+ // No pseudo form for schemas, enums, sequences, functions, indexes, constraints.
+ }
+}
+
+// wtInstallReal performs the real catalog install for one object.
+func wtInstallReal(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ switch obj.kind {
+ case kindWTSchema:
+ err := cat.CreateSchemaCommand(&ast.CreateSchemaStmt{Schemaname: obj.schema})
+ var cErr *catalog.Error
+ if errors.As(err, &cErr) && cErr.Code == catalog.CodeDuplicateSchema {
+ return nil
+ }
+ return err
+
+ case kindWTEnum:
+ vals := make([]ast.Node, 0, len(obj.enumMeta.Values))
+ for _, v := range obj.enumMeta.Values {
+ vals = append(vals, &ast.String{Str: v})
+ }
+ return cat.DefineEnum(&ast.CreateEnumStmt{
+ TypeName: wtQualifiedList(obj.schema, obj.name),
+ Vals: &ast.List{Items: vals},
+ })
+
+ case kindWTSequence:
+ return wtInstallSequence(cat, obj)
+
+ case kindWTTable:
+ return wtInstallTable(cat, obj)
+
+ case kindWTView:
+ return wtInstallView(cat, obj)
+
+ case kindWTMatView:
+ return wtInstallMatView(cat, obj)
+
+ case kindWTFunction:
+ return wtInstallFunction(cat, obj)
+
+ case kindWTIndex:
+ if obj.idxMeta.Primary {
+ return cat.AddConstraint(obj.schema, obj.parentName, catalog.ConstraintDef{
+ Name: obj.idxMeta.Name,
+ Type: catalog.ConstraintPK,
+ Columns: wtUnquoteColumns(obj.idxMeta.Expressions),
+ })
+ }
+ if obj.idxMeta.IsConstraint && obj.idxMeta.Unique {
+ return cat.AddConstraint(obj.schema, obj.parentName, catalog.ConstraintDef{
+ Name: obj.idxMeta.Name,
+ Type: catalog.ConstraintUnique,
+ Columns: wtUnquoteColumns(obj.idxMeta.Expressions),
+ })
+ }
+ return wtInstallIndex(cat, obj)
+
+ case kindWTConstraint:
+ return wtInstallConstraint(cat, obj)
+ }
+ return errors.Errorf("unknown object kind %d", obj.kind)
+}
+
+func wtInstallSequence(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ seq := obj.seqMeta
+ if seq == nil {
+ return errors.New("sequence has no metadata")
+ }
+
+ stmt := &ast.CreateSeqStmt{
+ Sequence: &ast.RangeVar{
+ Schemaname: obj.schema,
+ Relname: seq.Name,
+ Relpersistence: 'p',
+ },
+ }
+
+ var opts []ast.Node
+ if seq.Increment != "" && seq.Increment != "0" {
+ opts = append(opts, &ast.DefElem{Defname: "increment", Arg: &ast.Integer{Ival: wtParseInt64(seq.Increment)}})
+ }
+ if seq.MinValue != "" && seq.MinValue != "0" {
+ opts = append(opts, &ast.DefElem{Defname: "minvalue", Arg: &ast.Integer{Ival: wtParseInt64(seq.MinValue)}})
+ }
+ if seq.MaxValue != "" && seq.MaxValue != "0" {
+ opts = append(opts, &ast.DefElem{Defname: "maxvalue", Arg: &ast.Integer{Ival: wtParseInt64(seq.MaxValue)}})
+ }
+ if seq.Start != "" && seq.Start != "0" {
+ opts = append(opts, &ast.DefElem{Defname: "start", Arg: &ast.Integer{Ival: wtParseInt64(seq.Start)}})
+ }
+ if seq.CacheSize != "" && seq.CacheSize != "0" {
+ opts = append(opts, &ast.DefElem{Defname: "cache", Arg: &ast.Integer{Ival: wtParseInt64(seq.CacheSize)}})
+ }
+ if seq.Cycle {
+ opts = append(opts, &ast.DefElem{Defname: "cycle", Arg: &ast.Boolean{Boolval: true}})
+ }
+ if len(opts) > 0 {
+ stmt.Options = &ast.List{Items: opts}
+ }
+
+ return cat.DefineSequence(stmt)
+}
+
+// wtIsIdentitySequence checks if a sequence is owned by an identity column.
+// Identity columns auto-create their sequence during DefineRelation, so
+// pre-creating it would cause a duplicate error.
+func wtIsIdentitySequence(seq *storepb.SequenceMetadata, tables []*storepb.TableMetadata) bool {
+ if seq.OwnerTable == "" || seq.OwnerColumn == "" {
+ return false
+ }
+ for _, tbl := range tables {
+ if tbl.Name != seq.OwnerTable {
+ continue
+ }
+ for _, col := range tbl.Columns {
+ if col.Name == seq.OwnerColumn && col.IsIdentity {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func wtParseInt64(s string) int64 {
+ var v int64
+ _, _ = fmt.Sscanf(s, "%d", &v)
+ return v
+}
+
+func wtInstallTable(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ items := make([]ast.Node, 0, len(obj.tableMeta.Columns))
+ for _, col := range obj.tableMeta.Columns {
+ if col.Name == "" {
+ continue
+ }
+ if col.Type == "" {
+ return errors.Errorf("column %q: empty type", col.Name)
+ }
+ tn, err := wtTypeNameFromString(col.Type)
+ if err != nil {
+ return errors.Wrapf(err, "column %q", col.Name)
+ }
+ colDef := &ast.ColumnDef{
+ Colname: col.Name,
+ TypeName: tn,
+ IsNotNull: !col.Nullable,
+ }
+ if col.Default != "" && col.Generation == nil {
+ if rawDefault, err := wtParseDefaultExpr(col.Default); err == nil {
+ colDef.RawDefault = rawDefault
+ }
+ }
+ if col.Generation != nil && col.Generation.Type == storepb.GenerationMetadata_TYPE_STORED && col.Generation.Expression != "" {
+ colDef.Generated = 's'
+ if genExpr := wtParseExpr(col.Generation.Expression); genExpr != nil {
+ colDef.Constraints = &ast.List{Items: []ast.Node{
+ &ast.Constraint{Contype: ast.CONSTR_GENERATED, RawExpr: genExpr},
+ }}
+ }
+ }
+ if col.IsIdentity {
+ switch col.IdentityGeneration {
+ case storepb.ColumnMetadata_ALWAYS:
+ colDef.Identity = 'a'
+ case storepb.ColumnMetadata_BY_DEFAULT:
+ colDef.Identity = 'd'
+ default:
+ colDef.Identity = 'd'
+ }
+ }
+ items = append(items, colDef)
+ }
+ return cat.DefineRelation(&ast.CreateStmt{
+ Relation: &ast.RangeVar{
+ Schemaname: obj.schema,
+ Relname: obj.tableMeta.Name,
+ Relpersistence: 'p',
+ },
+ TableElts: &ast.List{Items: items},
+ }, 'r')
+}
+
+func wtInstallView(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ if obj.viewMeta.Definition == "" {
+ return errors.New("empty view definition")
+ }
+ sel, err := wtParseSelectBody(obj.viewMeta.Definition)
+ if err != nil {
+ return errors.Wrapf(err, "view %q", obj.name)
+ }
+ return cat.DefineView(&ast.ViewStmt{
+ View: &ast.RangeVar{
+ Schemaname: obj.schema,
+ Relname: obj.name,
+ Relpersistence: 'p',
+ },
+ Query: sel,
+ })
+}
+
+func wtInstallMatView(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ if obj.matViewMeta.Definition == "" {
+ return errors.New("empty matview definition")
+ }
+ sel, err := wtParseSelectBody(obj.matViewMeta.Definition)
+ if err != nil {
+ return errors.Wrapf(err, "matview %q", obj.name)
+ }
+ return cat.ExecCreateTableAs(&ast.CreateTableAsStmt{
+ Query: sel,
+ Objtype: ast.OBJECT_MATVIEW,
+ Into: &ast.IntoClause{
+ Rel: &ast.RangeVar{
+ Schemaname: obj.schema,
+ Relname: obj.name,
+ Relpersistence: 'p',
+ },
+ },
+ })
+}
+
+func wtInstallFunction(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ fn := obj.funcMeta
+ if fn.Definition != "" {
+ nodes, err := omniparser.Parse(fn.Definition)
+ if err == nil && nodes != nil && len(nodes.Items) == 1 {
+ node := nodes.Items[0]
+ if raw, ok := node.(*ast.RawStmt); ok {
+ node = raw.Stmt
+ }
+ if parsed, ok := node.(*ast.CreateFunctionStmt); ok {
+ return cat.CreateFunctionStmt(parsed)
+ }
+ }
+ }
+ // Fallback: build from signature.
+ argTypes, err := wtParseFuncArgTypes(fn.Signature)
+ if err != nil {
+ return errors.Wrapf(err, "function %q signature %q", fn.Name, fn.Signature)
+ }
+ params := make([]ast.Node, 0, len(argTypes))
+ for i, at := range argTypes {
+ tn, err := wtTypeNameFromString(at)
+ if err != nil {
+ return errors.Wrapf(err, "function %q arg %d", fn.Name, i)
+ }
+ params = append(params, &ast.FunctionParameter{
+ ArgType: tn,
+ Mode: ast.FUNC_PARAM_IN,
+ })
+ }
+ stmt := &ast.CreateFunctionStmt{
+ Funcname: wtQualifiedList(obj.schema, fn.Name),
+ ReturnType: wtPseudoTextTypeName(),
+ }
+ if len(params) > 0 {
+ stmt.Parameters = &ast.List{Items: params}
+ }
+ stmt.Options = &ast.List{Items: []ast.Node{
+ &ast.DefElem{Defname: "language", Arg: &ast.String{Str: "sql"}},
+ &ast.DefElem{
+ Defname: "as",
+ Arg: &ast.List{Items: []ast.Node{&ast.String{Str: "SELECT NULL::text"}}},
+ },
+ }}
+ return cat.CreateFunctionStmt(stmt)
+}
+
+func wtInstallIndex(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ idx := obj.idxMeta
+ if idx.Name == "" {
+ return errors.New("index has no name")
+ }
+ params := make([]ast.Node, 0, len(idx.Expressions))
+ for i, expr := range idx.Expressions {
+ if expr == "" {
+ continue
+ }
+ elem := &ast.IndexElem{}
+ if i < len(idx.Descending) && idx.Descending[i] {
+ elem.Ordering = ast.SORTBY_DESC
+ }
+ bare := wtUnquoteIdent(expr)
+ if wtIsExpressionIndex(bare) {
+ if parsed := wtParseExpr(expr); parsed != nil {
+ elem.Expr = parsed
+ } else {
+ elem.Name = bare
+ }
+ } else {
+ elem.Name = bare
+ }
+ params = append(params, elem)
+ }
+ return cat.DefineIndex(&ast.IndexStmt{
+ Idxname: idx.Name,
+ Relation: &ast.RangeVar{Schemaname: obj.schema, Relname: obj.parentName},
+ AccessMethod: idx.Type,
+ IndexParams: &ast.List{Items: params},
+ Unique: idx.Unique,
+ Primary: idx.Primary,
+ })
+}
+
+func wtInstallConstraint(cat *catalog.Catalog, obj *wtObjectEntry) error {
+ tbl := obj.constraintTable
+ if tbl == nil {
+ return errors.New("constraint has no parent table")
+ }
+
+ // Find which constraint this entry represents.
+ for _, fk := range tbl.ForeignKeys {
+ if fk.Name != obj.name {
+ continue
+ }
+ refSchema := fk.ReferencedSchema
+ if refSchema == "" {
+ refSchema = obj.schema
+ }
+ def := catalog.ConstraintDef{
+ Name: fk.Name,
+ Type: catalog.ConstraintFK,
+ Columns: fk.Columns,
+ RefSchema: refSchema,
+ RefTable: fk.ReferencedTable,
+ RefColumns: fk.ReferencedColumns,
+ FKUpdAction: wtFKActionByte(fk.OnUpdate),
+ FKDelAction: wtFKActionByte(fk.OnDelete),
+ FKMatchType: wtFKMatchByte(fk.MatchType),
+ }
+ return cat.AddConstraint(obj.schema, obj.parentName, def)
+ }
+ for _, chk := range tbl.CheckConstraints {
+ if chk.Name != obj.name {
+ continue
+ }
+ return cat.AddConstraint(obj.schema, obj.parentName, catalog.ConstraintDef{
+ Name: chk.Name,
+ Type: catalog.ConstraintCheck,
+ CheckExpr: chk.Expression,
+ })
+ }
+ for _, exc := range tbl.ExcludeConstraints {
+ if exc.Name != obj.name {
+ continue
+ }
+ return cat.AddConstraint(obj.schema, obj.parentName, catalog.ConstraintDef{
+ Name: exc.Name,
+ Type: catalog.ConstraintExclude,
+ CheckExpr: exc.Expression,
+ })
+ }
+ return errors.Errorf("constraint %q not found in table %q", obj.name, obj.parentName)
+}
+
+// ---- pseudo forms ----
+
+func wtPseudoTableStmt(schema, name string, cols []string) *ast.CreateStmt {
+ items := make([]ast.Node, 0, len(cols))
+ for _, col := range cols {
+ if col == "" {
+ continue
+ }
+ items = append(items, &ast.ColumnDef{
+ Colname: col,
+ TypeName: wtPseudoTextTypeName(),
+ })
+ }
+ return &ast.CreateStmt{
+ Relation: &ast.RangeVar{
+ Schemaname: schema,
+ Relname: name,
+ Relpersistence: 'p',
+ },
+ TableElts: &ast.List{Items: items},
+ }
+}
+
+func wtPseudoViewStmt(schema, name string, cols []string) (*ast.ViewStmt, error) {
+ sel, err := wtPseudoConstantSelect(cols)
+ if err != nil {
+ return nil, err
+ }
+ return &ast.ViewStmt{
+ View: &ast.RangeVar{
+ Schemaname: schema,
+ Relname: name,
+ Relpersistence: 'p',
+ },
+ Query: sel,
+ }, nil
+}
+
+func wtPseudoMatViewStmt(schema, name string, cols []string) (*ast.CreateTableAsStmt, error) {
+ sel, err := wtPseudoConstantSelect(cols)
+ if err != nil {
+ return nil, err
+ }
+ return &ast.CreateTableAsStmt{
+ Query: sel,
+ Objtype: ast.OBJECT_MATVIEW,
+ Into: &ast.IntoClause{
+ Rel: &ast.RangeVar{
+ Schemaname: schema,
+ Relname: name,
+ Relpersistence: 'p',
+ },
+ },
+ }, nil
+}
+
+func wtPseudoConstantSelect(cols []string) (*ast.SelectStmt, error) {
+ var targets []string
+ for _, col := range cols {
+ if col == "" {
+ continue
+ }
+ targets = append(targets, "NULL::text AS "+wtQuoteIdent(col))
+ }
+ if len(targets) == 0 {
+ targets = []string{"NULL::text"}
+ }
+ sql := "SELECT " + strings.Join(targets, ", ")
+ nodes, err := omniparser.Parse(sql)
+ if err != nil {
+ return nil, errors.Wrap(err, "parse pseudo constant select")
+ }
+ if nodes == nil || len(nodes.Items) != 1 {
+ return nil, errors.New("pseudo constant select: expected 1 statement")
+ }
+ node := nodes.Items[0]
+ if raw, ok := node.(*ast.RawStmt); ok {
+ node = raw.Stmt
+ }
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return nil, errors.Errorf("pseudo constant select: expected SelectStmt, got %T", node)
+ }
+ return sel, nil
+}
+
+// ---- column name helpers ----
+
+func wtColNames(tbl *storepb.TableMetadata) []string {
+ if tbl == nil {
+ return nil
+ }
+ seen := make(map[string]bool, len(tbl.Columns))
+ out := make([]string, 0, len(tbl.Columns))
+ for _, col := range tbl.Columns {
+ if col.Name == "" || seen[col.Name] {
+ continue
+ }
+ seen[col.Name] = true
+ out = append(out, col.Name)
+ }
+ return out
+}
+
+func wtViewColNames(v *storepb.ViewMetadata) []string {
+ if v == nil {
+ return nil
+ }
+ if len(v.Columns) > 0 {
+ seen := make(map[string]bool, len(v.Columns))
+ out := make([]string, 0, len(v.Columns))
+ for _, col := range v.Columns {
+ if col.Name == "" || seen[col.Name] {
+ continue
+ }
+ seen[col.Name] = true
+ out = append(out, col.Name)
+ }
+ return out
+ }
+ seen := make(map[string]bool)
+ var out []string
+ for _, dc := range v.DependencyColumns {
+ if dc.Column == "" || seen[dc.Column] {
+ continue
+ }
+ seen[dc.Column] = true
+ out = append(out, dc.Column)
+ }
+ return out
+}
+
+func wtMatViewColNames(m *storepb.MaterializedViewMetadata) []string {
+ if m == nil {
+ return nil
+ }
+ seen := make(map[string]bool)
+ var out []string
+ for _, dc := range m.DependencyColumns {
+ if dc.Column == "" || seen[dc.Column] {
+ continue
+ }
+ seen[dc.Column] = true
+ out = append(out, dc.Column)
+ }
+ return out
+}
+
+// ---- type parsing helpers ----
+
+// wtTypeNameFromString parses a PG type string into *ast.TypeName via
+// "SELECT NULL::<type>" to reuse omni's SELECT parse path.
+func wtTypeNameFromString(typeStr string) (*ast.TypeName, error) {
+ nodes, err := omniparser.Parse("SELECT NULL::" + typeStr)
+ if err != nil {
+ return nil, errors.Wrapf(err, "parse type %q", typeStr)
+ }
+ if nodes == nil || len(nodes.Items) != 1 {
+ return nil, errors.Errorf("type %q: expected 1 statement", typeStr)
+ }
+ node := nodes.Items[0]
+ if raw, ok := node.(*ast.RawStmt); ok {
+ node = raw.Stmt
+ }
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return nil, errors.Errorf("type %q: expected SelectStmt, got %T", typeStr, node)
+ }
+ if sel.TargetList == nil || len(sel.TargetList.Items) != 1 {
+ return nil, errors.Errorf("type %q: expected 1 target", typeStr)
+ }
+ rt, ok := sel.TargetList.Items[0].(*ast.ResTarget)
+ if !ok {
+ return nil, errors.Errorf("type %q: expected ResTarget, got %T", typeStr, sel.TargetList.Items[0])
+ }
+ cast, ok := rt.Val.(*ast.TypeCast)
+ if !ok {
+ return nil, errors.Errorf("type %q: expected TypeCast, got %T", typeStr, rt.Val)
+ }
+ return cast.TypeName, nil
+}
+
+// wtParseDefaultExpr parses a default expression string and returns the AST node.
+func wtParseDefaultExpr(expr string) (ast.Node, error) {
+ nodes, err := omniparser.Parse("SELECT " + expr)
+ if err != nil {
+ return nil, errors.Wrapf(err, "parse default %q", expr)
+ }
+ if nodes == nil || len(nodes.Items) != 1 {
+ return nil, errors.Errorf("default %q: expected 1 statement", expr)
+ }
+ node := nodes.Items[0]
+ if raw, ok := node.(*ast.RawStmt); ok {
+ node = raw.Stmt
+ }
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return nil, errors.Errorf("default %q: expected SelectStmt", expr)
+ }
+ if sel.TargetList == nil || len(sel.TargetList.Items) != 1 {
+ return nil, errors.Errorf("default %q: expected 1 target", expr)
+ }
+ rt, ok := sel.TargetList.Items[0].(*ast.ResTarget)
+ if !ok {
+ return nil, errors.Errorf("default %q: expected ResTarget", expr)
+ }
+ return rt.Val, nil
+}
+
+// wtParseSelectBody parses a SQL string into *ast.SelectStmt.
+func wtParseSelectBody(sql string) (*ast.SelectStmt, error) {
+ nodes, err := omniparser.Parse(sql)
+ if err != nil {
+ return nil, errors.Wrap(err, "parse")
+ }
+ if nodes == nil || len(nodes.Items) != 1 {
+ return nil, errors.Errorf("expected 1 statement, got %d", len(nodes.Items))
+ }
+ node := nodes.Items[0]
+ if raw, ok := node.(*ast.RawStmt); ok {
+ node = raw.Stmt
+ }
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return nil, errors.Errorf("expected SelectStmt, got %T", node)
+ }
+ return sel, nil
+}
+
+// ---- type ref extraction (local, no import from parser/pg) ----
+
+type wtUserTypeRef struct {
+ Schema string
+ Name string
+}
+
+func wtExtractUserTypeRefs(typeStr string) []wtUserTypeRef {
+ if typeStr == "" {
+ return nil
+ }
+ base := wtStripTypeModifiers(typeStr)
+ if base == "" || wtIsBuiltinType(base) {
+ return nil
+ }
+ if strings.HasPrefix(base, "_") {
+ return nil
+ }
+ schema, name, ok := wtSplitQualifiedName(base)
+ if !ok {
+ return nil
+ }
+ if wtIsSystemSchema(schema) {
+ return nil
+ }
+ return []wtUserTypeRef{{Schema: schema, Name: name}}
+}
+
+func wtStripTypeModifiers(s string) string {
+ s = strings.TrimSpace(s)
+ lower := strings.ToLower(s)
+ for _, sfx := range []string{" with time zone", " without time zone"} {
+ if strings.HasSuffix(lower, sfx) {
+ s = s[:len(s)-len(sfx)]
+ lower = lower[:len(lower)-len(sfx)]
+ }
+ }
+ if i := strings.Index(s, "("); i >= 0 {
+ s = strings.TrimSpace(s[:i])
+ }
+ return s
+}
+
+func wtSplitQualifiedName(s string) (schema, name string, ok bool) {
+ var parts []string
+ var cur strings.Builder
+ inQuote := false
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ switch {
+ case c == '"':
+ inQuote = !inQuote
+ case c == '.' && !inQuote:
+ parts = append(parts, cur.String())
+ cur.Reset()
+ default:
+ cur.WriteByte(c)
+ }
+ }
+ if inQuote {
+ return "", "", false
+ }
+ parts = append(parts, cur.String())
+ if len(parts) != 2 {
+ return "", "", false
+ }
+ return parts[0], parts[1], true
+}
+
+func wtIsSystemSchema(s string) bool {
+ switch s {
+ case "pg_catalog", "pg_toast", "information_schema":
+ return true
+ }
+ return strings.HasPrefix(s, "pg_")
+}
+
+func wtIsBuiltinType(s string) bool {
+ _, ok := wtBuiltinTypes[strings.ToLower(s)]
+ return ok
+}
+
+var wtBuiltinTypes = map[string]struct{}{
+ "smallint": {}, "integer": {}, "int": {}, "int2": {}, "int4": {}, "int8": {},
+ "bigint": {}, "decimal": {}, "numeric": {}, "real": {}, "double precision": {},
+ "float4": {}, "float8": {}, "smallserial": {}, "serial": {}, "bigserial": {},
+ "money": {}, "character": {}, "character varying": {}, "char": {}, "varchar": {},
+ "text": {}, "bytea": {}, "bpchar": {}, "name": {}, "bit": {}, "bit varying": {},
+ "varbit": {}, "date": {}, "time": {}, "timetz": {}, "timestamp": {}, "timestamptz": {},
+ "interval": {}, "boolean": {}, "bool": {}, "json": {}, "jsonb": {}, "xml": {},
+ "uuid": {}, "point": {}, "line": {}, "lseg": {}, "box": {}, "path": {},
+ "polygon": {}, "circle": {}, "cidr": {}, "inet": {}, "macaddr": {}, "macaddr8": {},
+ "tsvector": {}, "tsquery": {}, "void": {}, "record": {}, "anyelement": {},
+ "anyarray": {}, "anynonarray": {}, "anyenum": {}, "anyrange": {}, "any": {},
+ "trigger": {}, "event_trigger": {}, "cstring": {}, "internal": {},
+ "language_handler": {}, "fdw_handler": {}, "index_am_handler": {}, "tsm_handler": {},
+ "pg_lsn": {}, "oid": {}, "regclass": {}, "regproc": {}, "regprocedure": {},
+ "regoper": {}, "regoperator": {}, "regtype": {}, "regconfig": {}, "regdictionary": {},
+ "regnamespace": {}, "regrole": {},
+}
+
+// wtExtractSeqRef extracts the seq key from a nextval('schema.seq'::regclass) default.
+// Returns the "seq:schema.name" key if found, empty string otherwise.
+var reNextval = regexp.MustCompile(`nextval\('([^']+)'`)
+
+func wtExtractSeqRef(defaultExpr, tableSchema string, index map[string]*wtObjectEntry) string {
+ m := reNextval.FindStringSubmatch(defaultExpr)
+ if m == nil {
+ return ""
+ }
+ // m[1] is the content: "schema.seq_name" or "seq_name"
+ raw := strings.TrimSuffix(m[1], "::regclass")
+ raw = strings.Trim(raw, "\"")
+ var seqSchema, seqName string
+ if dot := strings.Index(raw, "."); dot >= 0 {
+ seqSchema = strings.Trim(raw[:dot], "\"")
+ seqName = strings.Trim(raw[dot+1:], "\"")
+ } else {
+ seqSchema = tableSchema
+ seqName = raw
+ }
+ k := "seq:" + seqSchema + "." + seqName
+ if index[k] != nil {
+ return k
+ }
+ return ""
+}
+
+// ---- function signature helpers ----
+
+func wtParseFuncArgTypes(signature string) ([]string, error) {
+ open := strings.Index(signature, "(")
+ if open < 0 {
+ return nil, nil
+ }
+ closeIdx := strings.LastIndex(signature, ")")
+ if closeIdx < 0 || closeIdx <= open {
+ return nil, errors.Errorf("signature %q: unbalanced parens", signature)
+ }
+ return wtSplitTopLevelCommas(signature[open+1 : closeIdx]), nil
+}
+
+func wtSplitTopLevelCommas(s string) []string {
+ s = strings.TrimSpace(s)
+ if s == "" {
+ return nil
+ }
+ var parts []string
+ depth := 0
+ start := 0
+ for i := 0; i < len(s); i++ {
+ switch s[i] {
+ case '(':
+ depth++
+ case ')':
+ if depth > 0 {
+ depth--
+ }
+ case ',':
+ if depth == 0 {
+ parts = append(parts, strings.TrimSpace(s[start:i]))
+ start = i + 1
+ }
+ default:
+ // other bytes are part of the current token
+ }
+ }
+ parts = append(parts, strings.TrimSpace(s[start:]))
+ return parts
+}
+
+// ---- FK action helpers ----
+
+func wtFKActionByte(action string) byte {
+ switch strings.ToUpper(action) {
+ case "RESTRICT":
+ return 'r'
+ case "CASCADE":
+ return 'c'
+ case "SET NULL":
+ return 'n'
+ case "SET DEFAULT":
+ return 'd'
+ default:
+ return 'a' // NO ACTION
+ }
+}
+
+func wtFKMatchByte(match string) byte {
+ switch strings.ToUpper(match) {
+ case "FULL":
+ return 'f'
+ case "PARTIAL":
+ return 'p'
+ default:
+ return 's' // SIMPLE
+ }
+}
+
+// ---- small AST helpers ----
+
+func wtQualifiedList(schema, name string) *ast.List {
+ items := make([]ast.Node, 0, 2)
+ if schema != "" {
+ items = append(items, &ast.String{Str: schema})
+ }
+ items = append(items, &ast.String{Str: name})
+ return &ast.List{Items: items}
+}
+
+func wtPseudoTextTypeName() *ast.TypeName {
+ return &ast.TypeName{
+ Names: &ast.List{Items: []ast.Node{&ast.String{Str: "text"}}},
+ Typemod: -1,
+ }
+}
+
+func wtQuoteIdent(name string) string {
+ return `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
+}
+
+func wtIsExpressionIndex(expr string) bool {
+ return strings.ContainsAny(expr, "( ") || strings.Contains(expr, "::")
+}
+
+func wtUnquoteIdent(s string) string {
+ if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
+ return strings.ReplaceAll(s[1:len(s)-1], `""`, `"`)
+ }
+ return s
+}
+
+func wtUnquoteColumns(cols []string) []string {
+ out := make([]string, len(cols))
+ for i, c := range cols {
+ out[i] = wtUnquoteIdent(c)
+ }
+ return out
+}
+
+func wtParseExpr(expr string) ast.Node {
+ stmts, err := omniparser.Parse("SELECT (" + expr + ")")
+ if err != nil || stmts == nil || len(stmts.Items) == 0 {
+ return nil
+ }
+ raw, ok := stmts.Items[0].(*ast.RawStmt)
+ if !ok {
+ return nil
+ }
+ sel, ok := raw.Stmt.(*ast.SelectStmt)
+ if !ok || sel.TargetList == nil || len(sel.TargetList.Items) == 0 {
+ return nil
+ }
+ rt, ok := sel.TargetList.Items[0].(*ast.ResTarget)
+ if !ok {
+ return nil
+ }
+ return rt.Val
+}
diff --git a/backend/plugin/schema/pg/walk_through_omni.go b/backend/plugin/schema/pg/walk_through_omni.go
index ab436a99d5..39843c045b 100644
--- a/backend/plugin/schema/pg/walk_through_omni.go
+++ b/backend/plugin/schema/pg/walk_through_omni.go
@@ -2,9 +2,6 @@ package pg
import (
"context"
- "fmt"
- "slices"
- "strings"
"github.com/bytebase/omni/pg/catalog"
@@ -15,62 +12,40 @@ import (
"github.com/bytebase/bytebase/backend/store/model"
)
-// WalkThroughOmni performs DDL simulation using omni catalog.Exec().
-// It replaces the ANTLR-based walkthrough with the following flow:
-// 1. GetDatabaseDefinition(metadata) → schemaDDL
-// 2. catalog.Exec(schemaDDL) → load existing schema state
-// 3. catalog.Exec(userSQL) → execute user DDL
-// 4. Map errors → *storepb.Advice
-// 5. Convert updated catalog → DatabaseMetadata (for downstream rules)
+// WalkThroughOmni performs DDL simulation using omni catalog.
+// 1. loadWalkThroughCatalog(metadata) → load existing schema into catalog
+// 2. catalog.Exec(userSQL) → execute user DDL, collect per-statement Changes
+// 3. Map exec errors → *storepb.Advice
+// 4. Merge Changes into original metadata → FinalMetadata for downstream rules
func WalkThroughOmni(ctx schema.WalkThroughContext, d *model.DatabaseMetadata, _ []base.AST) *storepb.Advice {
if ctx.RawSQL == "" {
return nil
}
- // Step 1: Generate DDL from current schema state.
- schemaDDL, err := schema.GetDatabaseDefinition(
- storepb.Engine_POSTGRES,
- schema.GetDefinitionContext{},
- d.GetProto(),
- )
- if err != nil {
+ // Step 1: Load existing schema into two catalogs — one as snapshot, one for exec.
+ // TODO(BYT-9215): Thread a real context.Context through the call chain instead of using context.Background().
+ // WalkThroughContext is not a context.Context and the caller does not pass one.
+ catBefore := catalog.New()
+ if ctx.SessionUser != "" {
+ catBefore.SetSessionUser(ctx.SessionUser)
+ }
+ if searchPath := getConfiguredSearchPath(d); len(searchPath) > 0 {
+ catBefore.SetSearchPath(searchPath)
+ }
+ if err := loadWalkThroughCatalog(context.Background(), catBefore, d.GetProto()); err != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.DDLSimulationFailed.Int32(),
- Title: "Failed to generate schema DDL",
+ Title: "Failed to load schema",
Content: err.Error(),
StartPosition: &storepb.Position{Line: 0},
}
}
- // Step 2: Create catalog and load existing schema.
- c := catalog.New()
- if ctx.SessionUser != "" {
- c.SetSessionUser(ctx.SessionUser)
- }
-
- // Set initial search path from metadata config.
- if searchPath := getConfiguredSearchPath(d); len(searchPath) > 0 {
- c.SetSearchPath(searchPath)
- }
+ catAfter := catBefore.Clone()
- if schemaDDL != "" {
- // Load existing schema. Use ContinueOnError so partial schemas
- // (e.g. columns without types in metadata) still load what they can.
- if _, err := c.Exec(schemaDDL, &catalog.ExecOptions{ContinueOnError: true}); err != nil {
- return &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: code.DDLSimulationFailed.Int32(),
- Title: "Failed to load schema DDL",
- Content: err.Error(),
- StartPosition: &storepb.Position{Line: 0},
- }
- }
- }
-
- // Step 3: Execute user SQL with ContinueOnError so that downstream
- // rules can check FinalMetadata even when some statements fail.
- results, execErr := c.Exec(ctx.RawSQL, &catalog.ExecOptions{ContinueOnError: true})
+ // Step 2: Execute user SQL on catAfter.
+ results, execErr := catAfter.Exec(ctx.RawSQL, &catalog.ExecOptions{ContinueOnError: true})
if execErr != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
@@ -81,7 +56,7 @@ func WalkThroughOmni(ctx schema.WalkThroughContext, d *model.DatabaseMetadata, _
}
}
- // Step 4: Report the first error from the simulation.
+ // Step 3: Report the first exec error.
for _, r := range results {
if r.Error == nil {
continue
@@ -98,11 +73,13 @@ func WalkThroughOmni(ctx schema.WalkThroughContext, d *model.DatabaseMetadata, _
}
}
- // Step 5: Convert catalog state back to DatabaseMetadata for downstream rules.
- newProto := catalogToProto(c)
- extractViewDependencies(newProto)
- newMetadata := model.NewDatabaseMetadata(newProto, nil, d.GetConfig(), storepb.Engine_POSTGRES, true)
- d.ReplaceFrom(newMetadata)
+ // Step 4: Diff before/after catalogs and apply to original metadata.
+ diff := catalog.Diff(catBefore, catAfter)
+ if diff != nil && !diff.IsEmpty() {
+ newProto := applyDiffToMetadata(d.GetProto(), catBefore, catAfter, diff)
+ newMetadata := model.NewDatabaseMetadata(newProto, nil, d.GetConfig(), storepb.Engine_POSTGRES, true)
+ d.ReplaceFrom(newMetadata)
+ }
return nil
}
@@ -186,65 +163,6 @@ func getConfiguredSearchPath(d *model.DatabaseMetadata) []string {
return searchPath
}
-// catalogToProto converts the omni catalog state to a storepb.DatabaseSchemaMetadata proto.
-func catalogToProto(c *catalog.Catalog) *storepb.DatabaseSchemaMetadata {
- dbMeta := &storepb.DatabaseSchemaMetadata{
- Name: "postgres",
- }
-
- for _, s := range c.UserSchemas() {
- schemaMeta := &storepb.SchemaMetadata{
- Name: s.Name,
- }
-
- // Convert tables, views, materialized views.
- // Sort relations for deterministic output.
- relNames := make([]string, 0, len(s.Relations))
- for name := range s.Relations {
- relNames = append(relNames, name)
- }
- slices.Sort(relNames)
-
- for _, relName := range relNames {
- rel := s.Relations[relName]
- switch rel.RelKind {
- case 'r', 'p', 'f': // table, partitioned table, foreign table
- schemaMeta.Tables = append(schemaMeta.Tables, relationToTableProto(c, rel))
- case 'v':
- schemaMeta.Views = append(schemaMeta.Views, relationToViewProto(c, rel))
- case 'm':
- schemaMeta.MaterializedViews = append(schemaMeta.MaterializedViews, relationToMatViewProto(c, rel))
- default:
- }
- }
-
- // Convert sequences.
- seqNames := make([]string, 0, len(s.Sequences))
- for name := range s.Sequences {
- seqNames = append(seqNames, name)
- }
- slices.Sort(seqNames)
-
- for _, seqName := range seqNames {
- seq := s.Sequences[seqName]
- schemaMeta.Sequences = append(schemaMeta.Sequences, &storepb.SequenceMetadata{
- Name: seq.Name,
- DataType: c.FormatType(seq.TypeOID, -1),
- Start: fmt.Sprintf("%d", seq.Start),
- MinValue: fmt.Sprintf("%d", seq.MinValue),
- MaxValue: fmt.Sprintf("%d", seq.MaxValue),
- Increment: fmt.Sprintf("%d", seq.Increment),
- Cycle: seq.Cycle,
- CacheSize: fmt.Sprintf("%d", seq.CacheValue),
- })
- }
-
- dbMeta.Schemas = append(dbMeta.Schemas, schemaMeta)
- }
-
- return dbMeta
-}
-
// relationToTableProto converts an omni Relation to a storepb.TableMetadata.
func relationToTableProto(c *catalog.Catalog, rel *catalog.Relation) *storepb.TableMetadata {
table := &storepb.TableMetadata{
@@ -356,9 +274,9 @@ func relationToTableProto(c *catalog.Catalog, rel *catalog.Relation) *storepb.Ta
}
}
// Actions.
- fk.OnUpdate = fkActionToString(con.FKUpdAction)
- fk.OnDelete = fkActionToString(con.FKDelAction)
- fk.MatchType = fkMatchToString(con.FKMatchType)
+ fk.OnUpdate = wtFKActionToString(con.FKUpdAction)
+ fk.OnDelete = wtFKActionToString(con.FKDelAction)
+ fk.MatchType = wtFKMatchToString(con.FKMatchType)
table.ForeignKeys = append(table.ForeignKeys, fk)
case 'c': // CHECK
table.CheckConstraints = append(table.CheckConstraints, &storepb.CheckConstraintMetadata{
@@ -440,85 +358,3 @@ func relationToMatViewProto(c *catalog.Catalog, rel *catalog.Relation) *storepb.
return mv
}
-
-func fkActionToString(action byte) string {
- switch action {
- case 'a':
- return "NO ACTION"
- case 'r':
- return "RESTRICT"
- case 'c':
- return "CASCADE"
- case 'n':
- return "SET NULL"
- case 'd':
- return "SET DEFAULT"
- default:
- return "NO ACTION"
- }
-}
-
-func fkMatchToString(match byte) string {
- switch match {
- case 'f':
- return "FULL"
- case 'p':
- return "PARTIAL"
- default:
- return "SIMPLE"
- }
-}
-
-func extractViewDependencies(schemaMetadata *storepb.DatabaseSchemaMetadata) {
- for _, s := range schemaMetadata.Schemas {
- for _, view := range s.Views {
- view.DependencyColumns = getViewDependencies(view.Definition, s.Name, schemaMetadata)
- }
- for _, mv := range s.MaterializedViews {
- mv.DependencyColumns = getViewDependencies(mv.Definition, s.Name, schemaMetadata)
- }
- }
-}
-
-func getViewDependencies(viewDef string, schemaName string, fullSchemaMetadata *storepb.DatabaseSchemaMetadata) []*storepb.DependencyColumn {
- queryStatement := strings.TrimSpace(viewDef)
- spans, err := base.GetQuerySpan(
- context.Background(),
- base.GetQuerySpanContext{
- GetDatabaseMetadataFunc: func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {
- dbMetadata := model.NewDatabaseMetadata(fullSchemaMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- return databaseName, dbMetadata, nil
- },
- ListDatabaseNamesFunc: func(_ context.Context, _ string) ([]string, error) {
- return []string{}, nil
- },
- },
- storepb.Engine_POSTGRES,
- []base.Statement{{Text: queryStatement}},
- "",
- schemaName,
- false,
- )
- if err != nil || len(spans) == 0 {
- return []*storepb.DependencyColumn{}
- }
- span := spans[0]
-
- dependencyMap := make(map[string]*storepb.DependencyColumn)
- for sourceColumn := range span.SourceColumns {
- key := fmt.Sprintf("%s.%s", sourceColumn.Schema, sourceColumn.Table)
- if _, exists := dependencyMap[key]; !exists {
- dependencyMap[key] = &storepb.DependencyColumn{
- Schema: sourceColumn.Schema,
- Table: sourceColumn.Table,
- Column: "*",
- }
- }
- }
-
- var dependencies []*storepb.DependencyColumn
- for _, dep := range dependencyMap {
- dependencies = append(dependencies, dep)
- }
- return dependencies
-}
diff --git a/go.mod b/go.mod
index 769a7c533d..2cac2341df 100644
--- a/go.mod
+++ b/go.mod
@@ -34,7 +34,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/bytebase/gomongo v0.0.0-20260403033636-f6c50d4ab888
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
- github.com/bytebase/omni v0.0.0-20260415084539-4a524e137c4a
+ github.com/bytebase/omni v0.0.0-20260417040020-724d3998950a
github.com/caarlos0/env/v11 v11.4.0
github.com/cenkalti/backoff/v5 v5.0.3
github.com/cockroachdb/cockroachdb-parser v0.25.2
diff --git a/go.sum b/go.sum
index 1b28ae71f4..b801d19bee 100644
--- a/go.sum
+++ b/go.sum
@@ -251,8 +251,8 @@ github.com/bytebase/gomongo v0.0.0-20260403033636-f6c50d4ab888 h1:E8tz8maxpih/vt
github.com/bytebase/gomongo v0.0.0-20260403033636-f6c50d4ab888/go.mod h1:IgLUOjyiHehdE0G3C92IjGYu9lJQgkPwf2mdNSvPWq8=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=
-github.com/bytebase/omni v0.0.0-20260415084539-4a524e137c4a h1:/13ZmyR9mpq1PjptpJmasQlJ3S5h6drkeL5yZFH5Zr8=
-github.com/bytebase/omni v0.0.0-20260415084539-4a524e137c4a/go.mod h1:AT+iLKCu7NyxTaYaOLzETRFBVurjRaqUU3YmFron9pw=
+github.com/bytebase/omni v0.0.0-20260417040020-724d3998950a h1:jSeJxacP5ZJzvIZwddyX5N0OP9hR87SOGnht3HdX9B4=
+github.com/bytebase/omni v0.0.0-20260417040020-724d3998950a/go.mod h1:AT+iLKCu7NyxTaYaOLzETRFBVurjRaqUU3YmFron9pw=
github.com/bytebase/parser v0.0.0-20260324092042-1d52e5412f50 h1:Kq5wlhZ586Rch39y/IQV9MnrEzHob4iiXBG6zrbvSMU=
github.com/bytebase/parser v0.0.0-20260324092042-1d52e5412f50/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=
github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=