Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(pg): replace ANTLR-based walkthrough with omni catalog.Exec() (#19623)
rebelice35f08885fb99
diff --git a/backend/plugin/advisor/code/code.go b/backend/plugin/advisor/code/code.goindex 04c65fc179..07816512e9 100644--- a/backend/plugin/advisor/code/code.go+++ b/backend/plugin/advisor/code/code.go@@ -82,6 +82,9 @@ const (SDLDropOperation Code = 255SDLReplaceOperation Code = 256+ // 260 DDL simulation error code.+ DDLSimulationFailed Code = 260+// 301 ~ 399 naming error code// 301 table naming advisor error code.NamingTableConventionMismatch Code = 301diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 0aa9b3f4c7..be0413fdfa 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -120,6 +120,7 @@ func SQLReviewCheck(if checkContext.FinalMetadata != nil {walkThroughContext := schema.WalkThroughContext{SessionUser: checkContext.SessionUser,+ RawSQL: statements,}if advice := schema.WalkThroughWithContext(checkContext.DBType, walkThroughContext, checkContext.FinalMetadata, asts); advice != nil {for _, rule := range ruleList {diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex 6b937dae7c..db8170ed90 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -91,8 +91,8 @@ var ({Name: MockTableName,Columns: []*storepb.ColumnMetadata{- {Name: "id"},- {Name: "name"},+ {Name: "id", Type: "integer", Position: 1},+ {Name: "name", Type: "text", Position: 2},},Indexes: []*storepb.IndexMetadata{{diff --git a/backend/plugin/schema/pg/testdata/walk_through.yaml b/backend/plugin/schema/pg/testdata/walk_through.yamlindex 96cf5490b3..6ed9019aa9 100644--- a/backend/plugin/schema/pg/testdata/walk_through.yaml+++ b/backend/plugin/schema/pg/testdata/walk_through.yaml@@ -1,321 +1,68 @@- statement: CREATE TABLE t(a int, b int);ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"t\",\n \- \ \"columns\": [\n {\n \"name\": \"a\",\n \- \ \"position\": 1,\n \"nullable\": true,\n \"type\"\- : \"integer\"\n },\n {\n \"name\": \"b\",\n\- \ \"position\": 2,\n \"nullable\": true,\n \- \ \"type\": \"integer\"\n }\n ]\n },\n {\n\- \ \"name\": \"test\",\n \"columns\": [\n {\n \- \ \"name\": \"id\",\n \"position\": 1,\n \"\- type\": \"int\"\n },\n {\n \"name\": \"name\"\- ,\n \"position\": 2,\n \"nullable\": true,\n \- \ \"type\": \"varchar(20)\"\n }\n ]\n }\n \- \ ],\n \"views\": [\n {\n \"name\": \"v1\",\n \- \ \"definition\": \"SELECT id, name FROM test\",\n \"dependencyColumns\"\- : [\n {\n \"schema\": \"public\",\n \"table\"\- : \"test\",\n \"column\": \"id\"\n },\n {\n\- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"name\"\n }\n ]\n }\n \- \ ]\n }\n ]\n}"+ want: |-+ {+ "name": "postgres",+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "t",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "integer"+ },+ {+ "name": "b",+ "position": 2,+ "nullable": true,+ "type": "integer"+ }+ ]+ },+ {+ "name": "test",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "type": "integer"+ },+ {+ "name": "name",+ "position": 2,+ "nullable": true,+ "type": "character varying(20)"+ }+ ]+ }+ ],+ "views": [+ {+ "name": "v1",+ "definition": " SELECT id,\n name\n FROM test;",+ "dependencyColumns": [+ {+ "schema": "public",+ "table": "test",+ "column": "*"+ }+ ]+ }+ ]+ }+ ]+ }advice: null-- statement: 'CREATE TABLE public.t(a int, "B" int);-+- statement: |-+ CREATE TABLE public.t(a int, "B" int);CREATE INDEX ON t(a, "B");-CREATE UNIQUE INDEX ON t(a, "B");-- DROP INDEX IF EXISTS t_a_B_idx_11111;'+ DROP INDEX IF EXISTS t_a_B_idx_11111;ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"t\",\n \- \ \"columns\": [\n {\n \"name\": \"a\",\n \- \ \"position\": 1,\n \"nullable\": true,\n \"type\"\- : \"integer\"\n },\n {\n \"name\": \"B\",\n\- \ \"position\": 2,\n \"nullable\": true,\n \- \ \"type\": \"integer\"\n }\n ],\n \"indexes\"\- : [\n {\n \"name\": \"t_a_B_idx\",\n \"expressions\"\- : [\n \"a\",\n \"B\"\n ],\n \- \ \"type\": \"btree\"\n },\n {\n \"name\"\- : \"t_a_B_idx1\",\n \"expressions\": [\n \"a\",\n\- \ \"B\"\n ],\n \"type\": \"btree\",\n\- \ \"unique\": true\n }\n ]\n },\n \- \ {\n \"name\": \"test\",\n \"columns\": [\n {\n\- \ \"name\": \"id\",\n \"position\": 1,\n \- \ \"type\": \"int\"\n },\n {\n \"name\":\- \ \"name\",\n \"position\": 2,\n \"nullable\": true,\n\- \ \"type\": \"varchar(20)\"\n }\n ]\n \- \ }\n ],\n \"views\": [\n {\n \"name\": \"v1\",\n \- \ \"definition\": \"SELECT id, name FROM test\",\n \"dependencyColumns\"\- : [\n {\n \"schema\": \"public\",\n \"table\"\- : \"test\",\n \"column\": \"id\"\n },\n {\n\- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"name\"\n }\n ]\n }\n \- \ ]\n }\n ]\n}"- advice: null-- statement: "CREATE TABLE public.t(\n a int primary key,\n b int NOT NULL DEFAULT\- \ 1,\n c int UNIQUE,\n UNIQUE (a, b, c),\n CONSTRAINT uk_a_b UNIQUE (a, b)\n\- )"- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"t\",\n \- \ \"columns\": [\n {\n \"name\": \"a\",\n \- \ \"position\": 1,\n \"type\": \"integer\"\n },\n\- \ {\n \"name\": \"b\",\n \"position\": 2,\n\- \ \"default\": \"1\",\n \"type\": \"integer\"\n \- \ },\n {\n \"name\": \"c\",\n \"position\"\- : 3,\n \"nullable\": true,\n \"type\": \"integer\"\n\- \ }\n ],\n \"indexes\": [\n {\n \- \ \"name\": \"t_a_b_c_idx\",\n \"expressions\": [\n \- \ \"a\",\n \"b\",\n \"c\"\n \- \ ],\n \"type\": \"btree\",\n \"unique\": true,\n\- \ \"isConstraint\": true\n },\n {\n \- \ \"name\": \"t_c_idx\",\n \"expressions\": [\n \- \ \"c\"\n ],\n \"type\": \"btree\",\n \- \ \"unique\": true,\n \"isConstraint\": true\n },\n\- \ {\n \"name\": \"t_pkey\",\n \"expressions\"\- : [\n \"a\"\n ],\n \"type\": \"btree\"\- ,\n \"unique\": true,\n \"primary\": true,\n \- \ \"isConstraint\": true\n },\n {\n \"\- name\": \"uk_a_b\",\n \"expressions\": [\n \"a\",\n\- \ \"b\"\n ],\n \"type\": \"btree\",\n\- \ \"unique\": true,\n \"isConstraint\": true\n \- \ }\n ]\n },\n {\n \"name\": \"test\",\n\- \ \"columns\": [\n {\n \"name\": \"id\",\n \- \ \"position\": 1,\n \"type\": \"int\"\n },\n\- \ {\n \"name\": \"name\",\n \"position\"\- : 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\- \n }\n ]\n }\n ],\n \"views\": [\n \- \ {\n \"name\": \"v1\",\n \"definition\": \"SELECT id, name\- \ FROM test\",\n \"dependencyColumns\": [\n {\n \- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"id\"\n },\n {\n \"schema\"\- : \"public\",\n \"table\": \"test\",\n \"column\": \"\- name\"\n }\n ]\n }\n ]\n }\n ]\n}"- advice: null-- statement: "CREATE TABLE public.t(\n a int primary key,\n b int NOT NULL DEFAULT\- \ 1,\n c int UNIQUE,\n UNIQUE (a, b, c),\n CONSTRAINT uk_a_b UNIQUE (a, b)\n\- );\nALTER TABLE t rename to t1;\nALTER TABLE t1 rename column a to a1;\nalter\- \ table t1 rename constraint uk_a_b to ukk_a_b;\nalter table t1 add column d int;\n\- alter table t1 drop column c;\nalter table t1 alter column b set data type bigint;\n\- alter table t1 alter column a1 set default 1;\nalter table t1 alter column b drop\- \ default;\nalter table t1 alter column d set not null;\nalter table t1 add constraint\- \ uk_d unique (d);\nalter table t1 add constraint ukk_d unique (d);\nalter table\- \ t1 drop constraint ukk_d;"- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"t1\",\n \- \ \"columns\": [\n {\n \"name\": \"a1\",\n \- \ \"position\": 1,\n \"default\": \"1\",\n \"\- type\": \"integer\"\n },\n {\n \"name\": \"\- b\",\n \"position\": 2,\n \"type\": \"bigint\"\n \- \ },\n {\n \"name\": \"d\",\n \"\- position\": 4,\n \"type\": \"integer\"\n }\n \- \ ],\n \"indexes\": [\n {\n \"name\": \"t_pkey\"\- ,\n \"expressions\": [\n \"a1\"\n ],\n\- \ \"type\": \"btree\",\n \"unique\": true,\n \- \ \"primary\": true,\n \"isConstraint\": true\n \- \ },\n {\n \"name\": \"uk_d\",\n \"expressions\"\- : [\n \"d\"\n ],\n \"type\": \"btree\"\- ,\n \"unique\": true,\n \"isConstraint\": true\n \- \ },\n {\n \"name\": \"ukk_a_b\",\n \- \ \"expressions\": [\n \"a1\",\n \"b\"\n \- \ ],\n \"type\": \"btree\",\n \"unique\": true,\n\- \ \"isConstraint\": true\n }\n ]\n },\n\- \ {\n \"name\": \"test\",\n \"columns\": [\n \- \ {\n \"name\": \"id\",\n \"position\": 1,\n \- \ \"type\": \"int\"\n },\n {\n \"\- name\": \"name\",\n \"position\": 2,\n \"nullable\"\- : true,\n \"type\": \"varchar(20)\"\n }\n ]\n\- \ }\n ],\n \"views\": [\n {\n \"name\": \"v1\"\- ,\n \"definition\": \"SELECT id, name FROM test\",\n \"dependencyColumns\"\- : [\n {\n \"schema\": \"public\",\n \"table\"\- : \"test\",\n \"column\": \"id\"\n },\n {\n\- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"name\"\n }\n ]\n }\n \- \ ]\n }\n ]\n}"- advice: null-- statement: "CREATE TABLE public.t(\n a int primary key,\n b int NOT NULL DEFAULT\- \ 1,\n c int UNIQUE,\n UNIQUE (a, b, c),\n CONSTRAINT uk_a_b UNIQUE (a, b)\n\- );\nalter index uk_a_b rename to ukk_a_b;"- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"t\",\n \- \ \"columns\": [\n {\n \"name\": \"a\",\n \- \ \"position\": 1,\n \"type\": \"integer\"\n },\n\- \ {\n \"name\": \"b\",\n \"position\": 2,\n\- \ \"default\": \"1\",\n \"type\": \"integer\"\n \- \ },\n {\n \"name\": \"c\",\n \"position\"\- : 3,\n \"nullable\": true,\n \"type\": \"integer\"\n\- \ }\n ],\n \"indexes\": [\n {\n \- \ \"name\": \"t_a_b_c_idx\",\n \"expressions\": [\n \- \ \"a\",\n \"b\",\n \"c\"\n \- \ ],\n \"type\": \"btree\",\n \"unique\": true,\n\- \ \"isConstraint\": true\n },\n {\n \- \ \"name\": \"t_c_idx\",\n \"expressions\": [\n \- \ \"c\"\n ],\n \"type\": \"btree\",\n \- \ \"unique\": true,\n \"isConstraint\": true\n },\n\- \ {\n \"name\": \"t_pkey\",\n \"expressions\"\- : [\n \"a\"\n ],\n \"type\": \"btree\"\- ,\n \"unique\": true,\n \"primary\": true,\n \- \ \"isConstraint\": true\n },\n {\n \"\- name\": \"ukk_a_b\",\n \"expressions\": [\n \"a\"\- ,\n \"b\"\n ],\n \"type\": \"btree\"\- ,\n \"unique\": true,\n \"isConstraint\": true\n \- \ }\n ]\n },\n {\n \"name\": \"test\"\- ,\n \"columns\": [\n {\n \"name\": \"id\",\n\- \ \"position\": 1,\n \"type\": \"int\"\n \- \ },\n {\n \"name\": \"name\",\n \"position\"\- : 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\- \n }\n ]\n }\n ],\n \"views\": [\n \- \ {\n \"name\": \"v1\",\n \"definition\": \"SELECT id, name\- \ FROM test\",\n \"dependencyColumns\": [\n {\n \- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"id\"\n },\n {\n \"schema\"\- : \"public\",\n \"table\": \"test\",\n \"column\": \"\- name\"\n }\n ]\n }\n ]\n }\n ]\n}"- advice: null-- statement: "CREATE TABLE public.t(\n a int primary key,\n b int NOT NULL DEFAULT\- \ 1,\n c int UNIQUE,\n UNIQUE (a, b, c),\n CONSTRAINT uk_a_b UNIQUE (a, b)\n\- );\ndrop index uk_a_b;"- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"t\",\n \- \ \"columns\": [\n {\n \"name\": \"a\",\n \- \ \"position\": 1,\n \"type\": \"integer\"\n },\n\- \ {\n \"name\": \"b\",\n \"position\": 2,\n\- \ \"default\": \"1\",\n \"type\": \"integer\"\n \- \ },\n {\n \"name\": \"c\",\n \"position\"\- : 3,\n \"nullable\": true,\n \"type\": \"integer\"\n\- \ }\n ],\n \"indexes\": [\n {\n \- \ \"name\": \"t_a_b_c_idx\",\n \"expressions\": [\n \- \ \"a\",\n \"b\",\n \"c\"\n \- \ ],\n \"type\": \"btree\",\n \"unique\": true,\n\- \ \"isConstraint\": true\n },\n {\n \- \ \"name\": \"t_c_idx\",\n \"expressions\": [\n \- \ \"c\"\n ],\n \"type\": \"btree\",\n \- \ \"unique\": true,\n \"isConstraint\": true\n },\n\- \ {\n \"name\": \"t_pkey\",\n \"expressions\"\- : [\n \"a\"\n ],\n \"type\": \"btree\"\- ,\n \"unique\": true,\n \"primary\": true,\n \- \ \"isConstraint\": true\n }\n ]\n },\n \- \ {\n \"name\": \"test\",\n \"columns\": [\n {\n\- \ \"name\": \"id\",\n \"position\": 1,\n \- \ \"type\": \"int\"\n },\n {\n \"name\":\- \ \"name\",\n \"position\": 2,\n \"nullable\": true,\n\- \ \"type\": \"varchar(20)\"\n }\n ]\n \- \ }\n ],\n \"views\": [\n {\n \"name\": \"v1\",\n \- \ \"definition\": \"SELECT id, name FROM test\",\n \"dependencyColumns\"\- : [\n {\n \"schema\": \"public\",\n \"table\"\- : \"test\",\n \"column\": \"id\"\n },\n {\n\- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"name\"\n }\n ]\n }\n \- \ ]\n }\n ]\n}"- advice: null-- statement: "CREATE TABLE public.t(\n a int primary key,\n b int NOT NULL DEFAULT\- \ 1,\n c int UNIQUE,\n UNIQUE (a, b, c),\n CONSTRAINT uk_a_b UNIQUE (a, b)\n\- );\ndrop table t;"- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"test\",\n \- \ \"columns\": [\n {\n \"name\": \"id\",\n \- \ \"position\": 1,\n \"type\": \"int\"\n },\n\- \ {\n \"name\": \"name\",\n \"position\"\- : 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\- \n }\n ]\n }\n ],\n \"views\": [\n \- \ {\n \"name\": \"v1\",\n \"definition\": \"SELECT id, name\- \ FROM test\",\n \"dependencyColumns\": [\n {\n \- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"id\"\n },\n {\n \"schema\"\- : \"public\",\n \"table\": \"test\",\n \"column\": \"\- name\"\n }\n ]\n }\n ]\n }\n ]\n}"- advice: null-- statement: "CREATE TABLE public.t(\n a int primary key,\n b int NOT NULL DEFAULT\- \ 1,\n c int UNIQUE,\n UNIQUE (a, b, c),\n CONSTRAINT uk_a_b UNIQUE (a, b)\n\- );\ndrop schema public;"- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\"\n}"- advice: null-- statement: ALTER TABLE test DROP COLUMN id;- ignore_case_sensitive: false- want: ''- advice:- status: 1- code: 421- title: 'Cannot drop column "id" in table "public"."test", it''s referenced by- view: "public"."v1"'- content: 'Cannot drop column "id" in table "public"."test", it''s referenced by- view: "public"."v1"'- startPosition:- line: 1-- statement: ALTER TABLE test ALTER COLUMN id TYPE varchar(20);- ignore_case_sensitive: false- want: ''- advice:- status: 1- code: 421- title: 'Cannot alter type of column "id" in table "public"."test", it''s referenced- by view: "public"."v1"'- content: 'Cannot alter type of column "id" in table "public"."test", it''s referenced- by view: "public"."v1"'- startPosition:- line: 1-- statement: DROP TABLE test;- ignore_case_sensitive: false- want: ''- advice:- status: 1- code: 609- title: 'Cannot drop table "public"."test", it''s referenced by view: "public"."v1"'- content: 'Cannot drop table "public"."test", it''s referenced by view: "public"."v1"'- startPosition:- line: 1-- statement: 'DROP VIEW v1;-- ALTER TABLE test DROP COLUMN id;'- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"test\",\n \- \ \"columns\": [\n {\n \"name\": \"name\",\n \- \ \"position\": 2,\n \"nullable\": true,\n \- \ \"type\": \"varchar(20)\"\n }\n ]\n }\n ]\n\- \ }\n ]\n}"- advice: null-- statement: 'create view v1 as select * from t;-- alter view v1 rename to this_is_view_del;-- drop view this_is_view_del;'- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"test\",\n \- \ \"columns\": [\n {\n \"name\": \"id\",\n \- \ \"position\": 1,\n \"type\": \"int\"\n },\n\- \ {\n \"name\": \"name\",\n \"position\"\- : 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\- \n }\n ]\n }\n ]\n }\n ]\n}"- advice: null-- statement: 'CREATE MATERIALIZED VIEW mv AS SELECT id, name FROM test;-- CREATE INDEX ON mv(id);'- ignore_case_sensitive: false- want: "{\n \"name\": \"postgres\",\n \"schemas\": [\n {\n \"name\": \"\- public\",\n \"tables\": [\n {\n \"name\": \"test\",\n \- \ \"columns\": [\n {\n \"name\": \"id\",\n \- \ \"position\": 1,\n \"type\": \"int\"\n },\n\- \ {\n \"name\": \"name\",\n \"position\"\- : 2,\n \"nullable\": true,\n \"type\": \"varchar(20)\"\- \n }\n ]\n }\n ],\n \"views\": [\n \- \ {\n \"name\": \"v1\",\n \"definition\": \"SELECT id, name\- \ FROM test\",\n \"dependencyColumns\": [\n {\n \- \ \"schema\": \"public\",\n \"table\": \"test\",\n \- \ \"column\": \"id\"\n },\n {\n \"schema\"\- : \"public\",\n \"table\": \"test\",\n \"column\": \"\- name\"\n }\n ]\n }\n ],\n \"materializedViews\"\- : [\n {\n \"name\": \"mv\",\n \"definition\": \"SELECT\- \ id, name FROM test\",\n \"indexes\": [\n {\n \- \ \"name\": \"mv_id_idx\",\n \"expressions\": [\n \- \ \"id\"\n ],\n \"type\": \"btree\"\n }\n\- \ ]\n }\n ]\n }\n ]\n}"+ want: ""advice: nulldiff --git a/backend/plugin/schema/pg/walk_through.go b/backend/plugin/schema/pg/walk_through.goindex b9c47af333..512c4d2b14 100644--- a/backend/plugin/schema/pg/walk_through.go+++ b/backend/plugin/schema/pg/walk_through.go@@ -1,2261 +1,26 @@package pgimport (- "fmt"- "strings"-- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/postgresql"-storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/advisor/code""github.com/bytebase/bytebase/backend/plugin/parser/base"- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg""github.com/bytebase/bytebase/backend/plugin/schema""github.com/bytebase/bytebase/backend/store/model")const (- // PublicSchemaName is the default schema name for PostgreSQL.PublicSchemaName = "public")func init() {- schema.RegisterWalkThroughWithContext(storepb.Engine_POSTGRES, WalkThroughWithContext)+ schema.RegisterWalkThroughWithContext(storepb.Engine_POSTGRES, WalkThroughOmni)}-// WalkThrough walks through the PostgreSQL ANTLR parse tree and builds catalog metadata.+// WalkThrough walks through the PostgreSQL DDL and builds catalog metadata.func WalkThrough(d *model.DatabaseMetadata, ast []base.AST) *storepb.Advice {return WalkThroughWithContext(schema.WalkThroughContext{}, d, ast)}-// WalkThroughWithContext walks through the PostgreSQL ANTLR parse tree and builds catalog metadata.+// WalkThroughWithContext walks through the PostgreSQL DDL and builds catalog metadata.func WalkThroughWithContext(ctx schema.WalkThroughContext, d *model.DatabaseMetadata, ast []base.AST) *storepb.Advice {- // Extract ANTLRAST from AST- var antlrASTList []*base.ANTLRAST- for _, unifiedAST := range ast {- antlrAST, ok := base.GetANTLRAST(unifiedAST)- if !ok {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: "PostgreSQL walk-through expects ANTLR-based parser result",- Content: "PostgreSQL walk-through expects ANTLR-based parser result",- StartPosition: &storepb.Position{- Line: 0,- },- }- }- antlrASTList = append(antlrASTList, antlrAST)- }-- // Build listener with database state- listener := &pgCatalogListener{- BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},- databaseState: d,- session: pgSessionState{- sessionUser: ctx.SessionUser,- currentUser: ctx.SessionUser,- defaultSearchPath: d.GetConfiguredSearchPath(),- },- }-- // Walk through all parse results- for _, antlrAST := range antlrASTList {- root, ok := antlrAST.Tree.(parser.IRootContext)- if !ok {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: fmt.Sprintf("invalid ANTLR tree type %T", antlrAST.Tree),- Content: fmt.Sprintf("invalid ANTLR tree type %T", antlrAST.Tree),- StartPosition: &storepb.Position{- Line: 0,- },- }- }- // Walk through the parse tree- antlr.ParseTreeWalkerDefault.Walk(listener, root)- // Return immediately if error encountered- if listener.advice != nil {- return listener.advice- }- }-- // Return any error encountered during walk- return listener.advice-}--// pgCatalogListener builds catalog state by listening to ANTLR parse tree events.-type pgCatalogListener struct {- *parser.BasePostgreSQLParserListener- databaseState *model.DatabaseMetadata- session pgSessionState- advice *storepb.Advice- currentLine int-}--type pgSessionState struct {- sessionUser string- currentUser string- defaultSearchPath []model.PGSearchPathItem- searchPath []model.PGSearchPathItem-}--func (s *pgSessionState) configuredSearchPath() []model.PGSearchPathItem {- if s.searchPath != nil {- return s.searchPath- }- return s.defaultSearchPath-}--func (s *pgSessionState) resolvedSearchPath() []string {- return model.ResolvePGSearchPath(s.configuredSearchPath(), s.currentUser, nil)-}--func (s *pgSessionState) effectiveSearchPath(db *model.DatabaseMetadata) []string {- return model.ResolvePGSearchPath(s.configuredSearchPath(), s.currentUser, func(name string) bool {- return db.GetSchemaMetadata(name) != nil- })-}--func (l *pgCatalogListener) EnterVariablesetstmt(ctx *parser.VariablesetstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }-- setRest := ctx.Set_rest()- if setRest == nil {- return- }- setRestMore := setRest.Set_rest_more()- if setRestMore == nil {- return- }-- if setRestMore.ROLE() != nil && setRestMore.Nonreservedword_or_sconst() != nil {- roleName, reset := normalizePGRoleName(setRestMore.Nonreservedword_or_sconst().GetText())- if reset {- l.session.currentUser = l.session.sessionUser- return- }- l.session.currentUser = roleName- return- }-- genericSet := setRestMore.Generic_set()- if genericSet == nil {- return- }- varName := genericSet.Var_name()- if varName == nil || len(varName.AllColid()) != 1 {- return- }- name := pgparser.NormalizePostgreSQLColid(varName.Colid(0))- if !strings.EqualFold(name, "search_path") {- return- }-- varList := genericSet.Var_list()- if varList == nil {- return- }- values := varList.AllVar_value()- if len(values) == 0 {- return- }- if len(values) == 1 && strings.EqualFold(strings.TrimSpace(values[0].GetText()), "default") {- l.session.searchPath = nil- return- }-- parts := make([]string, 0, len(values))- for _, value := range values {- parts = append(parts, value.GetText())- }- l.session.searchPath = model.ParsePGConfiguredSearchPath(strings.Join(parts, ", "))-}--func (l *pgCatalogListener) EnterVariableresetstmt(ctx *parser.VariableresetstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }-- resetRest := ctx.Reset_rest()- if resetRest == nil {- return- }- if resetRest.SESSION() != nil && resetRest.AUTHORIZATION() != nil {- l.session.currentUser = l.session.sessionUser- return- }- genericReset := resetRest.Generic_reset()- if genericReset == nil {- return- }- if genericReset.ALL() != nil {- l.session.currentUser = l.session.sessionUser- l.session.searchPath = nil- return- }- varName := genericReset.Var_name()- if varName == nil || len(varName.AllColid()) != 1 {- return- }- name := pgparser.NormalizePostgreSQLColid(varName.Colid(0))- switch {- case strings.EqualFold(name, "search_path"):- l.session.searchPath = nil- case strings.EqualFold(name, "role"):- l.session.currentUser = l.session.sessionUser- default:- }-}--func normalizePGRoleName(roleName string) (string, bool) {- roleName = strings.TrimSpace(roleName)- if roleName == "" {- return "", true- }- trimmed := strings.Trim(roleName, `"'`)- if strings.EqualFold(trimmed, "none") || strings.EqualFold(trimmed, "default") {- return "", true- }- return trimmed, false-}--func (l *pgCatalogListener) lookupSearchPath() []string {- searchPath := l.session.effectiveSearchPath(l.databaseState)- if len(searchPath) == 0 {- return []string{PublicSchemaName}- }- return searchPath-}--func (l *pgCatalogListener) resolveSchemaForCreate(explicitSchemaName string, line int) (*model.SchemaMetadata, *storepb.Advice) {- if explicitSchemaName != "" {- return getOrCreatePublicSchema(l.databaseState, explicitSchemaName, line)- }- for _, schemaName := range l.session.resolvedSearchPath() {- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema != nil {- return schema, nil- }- if schemaName == PublicSchemaName {- return l.databaseState.CreateSchema(PublicSchemaName), nil- }- }- return getOrCreatePublicSchema(l.databaseState, PublicSchemaName, line)-}--func (l *pgCatalogListener) findTable(schemaName string, tableName string) (*model.SchemaMetadata, *model.TableMetadata) {- if schemaName != "" {- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema == nil {- return nil, nil- }- return schema, schema.GetTable(tableName)- }- resolvedSchemaName, table := l.databaseState.SearchTable(l.lookupSearchPath(), tableName)- if table == nil {- return nil, nil- }- return l.databaseState.GetSchemaMetadata(resolvedSchemaName), table-}--func (l *pgCatalogListener) findTableOrMaterializedView(schemaName string, relationName string) (*model.SchemaMetadata, *model.TableMetadata, *storepb.MaterializedViewMetadata) {- if schemaName != "" {- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema == nil {- return nil, nil, nil- }- return schema, schema.GetTable(relationName), schema.GetMaterializedView(relationName)- }- searchPath := l.lookupSearchPath()- if resolvedSchemaName, table := l.databaseState.SearchTable(searchPath, relationName); table != nil {- return l.databaseState.GetSchemaMetadata(resolvedSchemaName), table, nil- }- if resolvedSchemaName, matView := l.databaseState.SearchMaterializedView(searchPath, relationName); matView != nil {- return l.databaseState.GetSchemaMetadata(resolvedSchemaName), nil, matView- }- return nil, nil, nil-}--func (l *pgCatalogListener) findView(schemaName string, viewName string) (*model.SchemaMetadata, *storepb.ViewMetadata) {- if schemaName != "" {- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema == nil {- return nil, nil- }- return schema, schema.GetView(viewName)- }- resolvedSchemaName, view := l.databaseState.SearchView(l.lookupSearchPath(), viewName)- if view == nil {- return nil, nil- }- return l.databaseState.GetSchemaMetadata(resolvedSchemaName), view-}--func (l *pgCatalogListener) findMaterializedView(schemaName string, viewName string) (*model.SchemaMetadata, *storepb.MaterializedViewMetadata) {- if schemaName != "" {- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema == nil {- return nil, nil- }- return schema, schema.GetMaterializedView(viewName)- }- resolvedSchemaName, view := l.databaseState.SearchMaterializedView(l.lookupSearchPath(), viewName)- if view == nil {- return nil, nil- }- return l.databaseState.GetSchemaMetadata(resolvedSchemaName), view-}--func (l *pgCatalogListener) findIndex(schemaName string, indexName string) (*model.SchemaMetadata, *model.IndexMetadata) {- if schemaName != "" {- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema == nil {- return nil, nil- }- return schema, schema.GetIndex(indexName)- }- resolvedSchemaName, index := l.databaseState.SearchIndex(l.lookupSearchPath(), indexName)- if index == nil {- return nil, nil- }- return l.databaseState.GetSchemaMetadata(resolvedSchemaName), index-}--// ========================================-// CREATE TABLE handling-// ========================================-// EnterCreatestmt handles CREATE TABLE statements.-func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Extract table name and schema- qualifiedNames := ctx.AllQualified_name()- if len(qualifiedNames) == 0 {- return- }- tableName := extractTableName(qualifiedNames[0])- schemaName := extractSchemaName(qualifiedNames[0])- if tableName == "" {- return- }- // Check database name if specified- databaseName := extractDatabaseName(qualifiedNames[0])- if databaseName != "" && l.databaseState.DatabaseName() != databaseName {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- StartPosition: &storepb.Position{- Line: int32(l.currentLine),- },- }- return- }- // Get or create schema- schema, err := l.resolveSchemaForCreate(schemaName, l.currentLine)- if err != nil {- l.advice = err- return- }- // Check if table already exists- if schema.GetTable(tableName) != nil {- // Check IF NOT EXISTS clause- ifNotExists := ctx.IF_P() != nil && ctx.NOT() != nil && ctx.EXISTS() != nil- if ifNotExists {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: fmt.Sprintf(`The table %q already exists in the schema %q`, tableName, schema.GetProto().Name),- Content: fmt.Sprintf(`The table %q already exists in the schema %q`, tableName, schema.GetProto().Name),- StartPosition: &storepb.Position{- Line: int32(l.currentLine),- },- }- return- }- // Create table- table, createErr := schema.CreateTable(tableName)- if createErr != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: createErr.Error(),- Content: createErr.Error(),- StartPosition: &storepb.Position{- Line: int32(l.currentLine),- },- }- return- }- // Process column definitions- if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {- allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()- for _, elem := range allElements {- // Handle column definitions- if elem.ColumnDef() != nil {- if err := pgCreateColumn(schema, table, elem.ColumnDef()); err != nil {- l.advice = err- return- }- }- // Handle table-level constraints- if elem.Tableconstraint() != nil {- if err := createTableConstraint(schema, table, elem.Tableconstraint(), l.currentLine); err != nil {- l.advice = err- return- }- }- }- }-}--// pgCreateColumn creates a column in the table.-func pgCreateColumn(schema *model.SchemaMetadata, table *model.TableMetadata, columnDef parser.IColumnDefContext) *storepb.Advice {- if columnDef == nil {- return nil- }- // Extract column name- var columnName string- if columnDef.Colid() != nil {- columnName = pgparser.NormalizePostgreSQLColid(columnDef.Colid())- }- if columnName == "" {- return nil- }- // Check if column already exists- if table.GetColumn(columnName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: fmt.Sprintf("The column %q already exists in table %q", columnName, table.GetProto().Name),- Content: fmt.Sprintf("The column %q already exists in table %q", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{- Line: 0,- },- }- }- // Get column type- var columnType string- if columnDef.Typename() != nil {- columnType = extractTypeNameFromContext(columnDef.Typename())- }- // Create column metadata- col := &storepb.ColumnMetadata{- Name: columnName,- Position: int32(len(table.GetProto().GetColumns()) + 1),- Default: "",- Nullable: true,- Type: columnType,- }- // Process column constraints- if columnDef.Colquallist() != nil {- allQuals := columnDef.Colquallist().AllColconstraint()- for _, qual := range allQuals {- if qual.Colconstraintelem() == nil {- continue- }- elem := qual.Colconstraintelem()- // Handle NOT NULL- if elem.NOT() != nil && elem.NULL_P() != nil {- col.Nullable = false- }- // Handle DEFAULT- if elem.DEFAULT() != nil {- // Extract default expression from B_expr- if elem.B_expr() != nil {- defaultValue := elem.B_expr().GetText()- col.Default = defaultValue- }- }- // Handle PRIMARY KEY- if elem.PRIMARY() != nil && elem.KEY() != nil {- constraintName := ""- if qual.Name() != nil {- constraintName = pgparser.NormalizePostgreSQLName(qual.Name())- }- if constraintName == "" {- constraintName = pgGeneratePrimaryKeyName(schema, table.GetProto().Name)- }- // Set column as NOT NULL- col.Nullable = false- // Create primary key index- index := &storepb.IndexMetadata{- Name: constraintName,- Expressions: []string{columnName},- Type: "btree",- Unique: true,- Primary: true,- IsConstraint: true,- }- if err := table.CreateIndex(index); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.PrimaryKeyExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- }- // Handle UNIQUE- if elem.UNIQUE() != nil && (elem.PRIMARY() == nil || elem.KEY() == nil) {- constraintName := ""- if qual.Name() != nil {- constraintName = pgparser.NormalizePostgreSQLName(qual.Name())- }- // Generate index name if not specified- if constraintName == "" {- constraintName = generateIndexName(table.GetProto().Name, []string{columnName}, true)- }- index := &storepb.IndexMetadata{- Name: constraintName,- Expressions: []string{columnName},- Type: "btree",- Unique: true,- Primary: false,- IsConstraint: true,- }- if err := table.CreateIndex(index); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- }- }- }- // Create the column- if err := table.CreateColumn(col, nil /* columnCatalog */); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- return nil-}--// createTableConstraint creates a table-level constraint.-func createTableConstraint(schema *model.SchemaMetadata, table *model.TableMetadata, constraint parser.ITableconstraintContext, line int) *storepb.Advice {- if constraint == nil || constraint.Constraintelem() == nil {- return nil- }- elem := constraint.Constraintelem()- // Extract constraint name- constraintName := ""- if constraint.Name() != nil {- constraintName = pgparser.NormalizePostgreSQLName(constraint.Name())- }- // Handle PRIMARY KEY constraint- if elem.PRIMARY() != nil && elem.KEY() != nil {- var columnList []string- if elem.Columnlist() != nil {- allColumns := elem.Columnlist().AllColumnElem()- for _, col := range allColumns {- if col.Colid() != nil {- colName := pgparser.NormalizePostgreSQLColid(col.Colid())- columnList = append(columnList, colName)- }- }- }- // Set all PK columns as NOT NULL- for _, colName := range columnList {- column := table.GetColumn(colName)- if column != nil {- column.GetProto().Nullable = false- } else {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- }- // Generate PK name if not provided- pkName := constraintName- if pkName == "" {- pkName = pgGeneratePrimaryKeyName(schema, table.GetProto().Name)- }- // Check if primary key already exists- if table.GetPrimaryKey() != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.PrimaryKeyExists.Int32(),- Title: fmt.Sprintf("Primary key already exists in table %q", table.GetProto().Name),- Content: fmt.Sprintf("Primary key already exists in table %q", table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Create primary key index- index := &storepb.IndexMetadata{- Name: pkName,- Expressions: columnList,- Type: "btree",- Unique: true,- Primary: true,- IsConstraint: true,- }- if err := table.CreateIndex(index); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.PrimaryKeyExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- }- // Handle UNIQUE constraint- if elem.UNIQUE() != nil && (elem.PRIMARY() == nil || elem.KEY() == nil) {- var columnList []string- if elem.Columnlist() != nil {- allColumns := elem.Columnlist().AllColumnElem()- for _, col := range allColumns {- if col.Colid() != nil {- colName := pgparser.NormalizePostgreSQLColid(col.Colid())- columnList = append(columnList, colName)- }- }- }- // Generate index name if not specified- indexName := constraintName- if indexName == "" {- indexName = generateIndexName(table.GetProto().Name, columnList, true)- }- // Check if index already exists- if table.GetIndex(indexName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: fmt.Sprintf("Index %q already exists in table %q", indexName, table.GetProto().Name),- Content: fmt.Sprintf("Index %q already exists in table %q", indexName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Validate columns exist- for _, colName := range columnList {- if table.GetColumn(colName) == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- }- // Create unique index (from UNIQUE constraint)- index := &storepb.IndexMetadata{- Name: indexName,- Expressions: columnList,- Type: "btree",- Unique: true,- Primary: false,- IsConstraint: true,- }- if err := table.CreateIndex(index); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- }- // Note: We skip CHECK, FOREIGN KEY, EXCLUSION constraints for now- // as the legacy implementation also skips them- return nil-}--// ========================================-// CREATE INDEX handling-// ========================================-// EnterIndexstmt handles CREATE INDEX statements.-func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Extract relation (table/materialized view) name- relationExpr := ctx.Relation_expr()- if relationExpr == nil || relationExpr.Qualified_name() == nil {- return- }- relationName := extractTableName(relationExpr.Qualified_name())- schemaName := extractSchemaName(relationExpr.Qualified_name())- // Try to find the relation as a table first, then as a materialized view- schema, table, matView := l.findTableOrMaterializedView(schemaName, relationName)- if table == nil && matView == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", relationName),- Content: fmt.Sprintf("Table `%s` does not exist", relationName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Extract index name (can be empty for auto-generated names)- indexName := ""- if ctx.Name() != nil {- indexName = pgparser.NormalizePostgreSQLName(ctx.Name())- }- // Check IF NOT EXISTS- ifNotExists := ctx.IF_P() != nil && ctx.NOT() != nil && ctx.EXISTS() != nil- // Extract column list- var columnList []string- if ctx.Index_params() != nil {- allParams := ctx.Index_params().AllIndex_elem()- for _, param := range allParams {- if param.Colid() != nil {- colName := pgparser.NormalizePostgreSQLColid(param.Colid())- columnList = append(columnList, colName)- } else if param.Func_expr_windowless() != nil {- // Expression index - use placeholder- columnList = append(columnList, "expr")- }- }- }- if len(columnList) == 0 {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexEmptyKeys.Int32(),- Title: fmt.Sprintf("Index %q in table %q has empty key", indexName, relationName),- Content: fmt.Sprintf("Index %q in table %q has empty key", indexName, relationName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Generate index name if not provided- isUnique := ctx.Opt_unique() != nil && ctx.Opt_unique().UNIQUE() != nil- wasAutoGenerated := indexName == ""- if indexName == "" {- indexName = generateIndexName(relationName, columnList, isUnique)- }- // Determine index type- indexType := "btree" // default- if ctx.Access_method_clause() != nil && ctx.Access_method_clause().Name() != nil {- method := pgparser.NormalizePostgreSQLName(ctx.Access_method_clause().Name())- indexType = method- }- // Create index - handle table and materialized view separately- index := &storepb.IndexMetadata{- Name: indexName,- Expressions: columnList,- Type: indexType,- Unique: isUnique,- Primary: false,- }- if table != nil {- // Index on a table- // Check if index name already exists- if table.GetIndex(indexName) != nil {- if ifNotExists {- return- }- // If name was auto-generated, try with numeric suffix- if wasAutoGenerated {- indexName = generateUniqueIndexName(schema, relationName, columnList, isUnique)- index.Name = indexName- } else {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.RelationExists.Int32(),- Title: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),- Content: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- // Check that all columns exist (skip expressions)- for _, colName := range columnList {- if colName != "expr" {- if table.GetColumn(colName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, relationName),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, relationName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- }- if err := table.CreateIndex(index); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- } else {- // Index on a materialized view- // Check if index name already exists- for _, existingIndex := range matView.Indexes {- if existingIndex.Name == indexName {- if ifNotExists {- return- }- // If name was auto-generated, try with numeric suffix- if wasAutoGenerated {- indexName = generateUniqueIndexName(schema, relationName, columnList, isUnique)- index.Name = indexName- break- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.RelationExists.Int32(),- Title: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),- Content: fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- // For materialized views, we don't have column metadata to validate- // Just add the index to the materialized view- matView.Indexes = append(matView.Indexes, index)- }-}--// ========================================-// CREATE SCHEMA handling-// ========================================-// TODO: EnterCreateschemastatement - Need to find correct ANTLR context name-//-// func (l *pgCatalogListener) EnterCreateschemastatement(ctx *parser.CreateschemaContext) {-// if !isTopLevel(ctx.GetParent()) || l.advice != nil {-// return-// }-//-// if !l.checkDatabaseNotDeleted() {-// return-// }-//-// l.currentLine = ctx.GetStart().GetLine()-//-// // TODO: Implement CREATE SCHEMA logic-// // Similar to pgCreateSchema() in walk_through_for_pg.go-// }-//-// ========================================-// ALTER TABLE handling-// ========================================-// EnterAltertablestmt handles ALTER TABLE statements.-func (l *pgCatalogListener) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Extract table name- if ctx.Relation_expr() == nil || ctx.Relation_expr().Qualified_name() == nil {- return- }- tableName := extractTableName(ctx.Relation_expr().Qualified_name())- schemaName := extractSchemaName(ctx.Relation_expr().Qualified_name())- databaseName := extractDatabaseName(ctx.Relation_expr().Qualified_name())- // Check database access- if databaseName != "" && l.databaseState.DatabaseName() != databaseName {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Get schema and table- schema, table, matView := l.findTableOrMaterializedView(schemaName, tableName)- if table == nil {- // Check if it's a materialized view - PostgreSQL allows some ALTER TABLE- // commands on materialized views (e.g., OWNER TO). Skip processing for- // materialized views since we don't track these changes in metadata.- if matView != nil {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Process alter table commands- if ctx.Alter_table_cmds() == nil {- return- }- allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()- for _, cmd := range allCmds {- l.processAlterTableCmd(schema, table, cmd)- if l.advice != nil {- return- }- }-}--// processAlterTableCmd handles individual ALTER TABLE commands.-func (l *pgCatalogListener) processAlterTableCmd(schema *model.SchemaMetadata, table *model.TableMetadata, cmd parser.IAlter_table_cmdContext) {- // RENAME operations are handled by EnterRenamestmt, not here- // Handle ADD COLUMN- if cmd.ADD_P() != nil && cmd.COLUMN() != nil {- if cmd.ColumnDef() != nil {- ifNotExists := cmd.IF_P() != nil && cmd.NOT() != nil && cmd.EXISTS() != nil- l.alterTableAddColumn(schema, table, cmd.ColumnDef(), ifNotExists)- }- return- }- // Handle ADD CONSTRAINT- if cmd.ADD_P() != nil && cmd.Tableconstraint() != nil {- l.alterTableAddConstraint(schema, table, cmd.Tableconstraint())- return- }- // Handle DROP CONSTRAINT- if cmd.DROP() != nil && cmd.CONSTRAINT() != nil {- ifExists := cmd.IF_P() != nil && cmd.EXISTS() != nil- if cmd.Name() != nil {- constraintName := pgparser.NormalizePostgreSQLName(cmd.Name())- l.alterTableDropConstraint(schema, table, constraintName, ifExists)- }- return- }- // Handle DROP COLUMN- if cmd.DROP() != nil && cmd.Opt_column() != nil {- ifExists := cmd.IF_P() != nil && cmd.EXISTS() != nil- // AllColid() returns a list - get the first column name- allColids := cmd.AllColid()- if len(allColids) > 0 {- columnName := pgparser.NormalizePostgreSQLColid(allColids[0])- l.alterTableDropColumn(schema, table, columnName, ifExists)- }- return- }- // Handle ALTER COLUMN commands- if cmd.ALTER() != nil && cmd.Opt_column() != nil {- allColids := cmd.AllColid()- if len(allColids) > 0 {- columnName := pgparser.NormalizePostgreSQLColid(allColids[0])- // Check for SET DATA TYPE- if cmd.TYPE_P() != nil && cmd.Typename() != nil {- typeString := extractTypeNameFromContext(cmd.Typename())- l.alterTableAlterColumnType(schema, table, columnName, typeString)- return- }- // Check for SET/DROP DEFAULT- if cmd.Alter_column_default() != nil {- altDefault := cmd.Alter_column_default()- if altDefault.SET() != nil && altDefault.A_expr() != nil {- // SET DEFAULT- defaultValue := altDefault.A_expr().GetText()- l.alterTableSetDefault(table, columnName, defaultValue)- } else if altDefault.DROP() != nil {- // DROP DEFAULT- l.alterTableDropDefault(table, columnName)- }- return- }- // Check for SET NOT NULL- if cmd.SET() != nil && cmd.NOT() != nil && cmd.NULL_P() != nil {- l.alterTableSetNotNull(table, columnName)- return- }- }- return- }-}--// alterTableDropColumn handles DROP COLUMN command.-func (l *pgCatalogListener) alterTableDropColumn(schema *model.SchemaMetadata, table *model.TableMetadata, columnName string, ifExists bool) {- column := table.GetColumn(columnName)- if column == nil {- if ifExists {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Check if any views depend on this column- dependentViews := schema.GetDependentViews(table.GetProto().Name, columnName)- if len(dependentViews) > 0 {- // Format view names with schema prefix to match old error format- var viewNames []string- for _, v := range dependentViews {- viewNames = append(viewNames, fmt.Sprintf("%q.%q", schema.GetProto().Name, v))- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnIsReferencedByView.Int32(),- Title: fmt.Sprintf("Cannot drop column %q in table %q.%q, it's referenced by view: %s", columnName, schema.GetProto().Name, table.GetProto().Name, strings.Join(viewNames, ", ")),- Content: fmt.Sprintf("Cannot drop column %q in table %q.%q, it's referenced by view: %s", columnName, schema.GetProto().Name, table.GetProto().Name, strings.Join(viewNames, ", ")),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Drop the constraints and indexes involving the column- var dropIndexList []string- for _, index := range table.GetProto().Indexes {- for _, key := range index.Expressions {- // TODO(zp): deal with expression key.- if key == columnName {- dropIndexList = append(dropIndexList, index.Name)- break // Once we find the column in this index, mark for deletion and move to next index- }- }- }- for _, indexName := range dropIndexList {- if err := table.DropIndex(indexName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- // TODO(zp): deal with other constraints.- // TODO(zp): deal with CASCADE.- // Validate column exists before dropping- if table.GetColumn(columnName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Delete the column without renumbering positions- // PostgreSQL maintains stable column positions (attnum) even after dropping columns- if err := table.DropColumnWithoutRenumbering(columnName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: fmt.Sprintf("failed to drop column: %v", err),- Content: fmt.Sprintf("failed to drop column: %v", err),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }-}--// alterTableAlterColumnType handles ALTER COLUMN TYPE command.-func (l *pgCatalogListener) alterTableAlterColumnType(schema *model.SchemaMetadata, table *model.TableMetadata, columnName string, typeString string) {- column := table.GetColumn(columnName)- if column == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Check if any views depend on this column- dependentViews := schema.GetDependentViews(table.GetProto().Name, columnName)- if len(dependentViews) > 0 {- // Format view names with schema prefix to match old error format- var viewNames []string- for _, v := range dependentViews {- viewNames = append(viewNames, fmt.Sprintf("%q.%q", schema.GetProto().Name, v))- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnIsReferencedByView.Int32(),- Title: fmt.Sprintf("Cannot alter type of column %q in table %q.%q, it's referenced by view: %s", columnName, schema.GetProto().Name, table.GetProto().Name, strings.Join(viewNames, ", ")),- Content: fmt.Sprintf("Cannot alter type of column %q in table %q.%q, it's referenced by view: %s", columnName, schema.GetProto().Name, table.GetProto().Name, strings.Join(viewNames, ", ")),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Update column type- column.GetProto().Type = typeString-}--// alterTableAddColumn handles ADD COLUMN command.-func (l *pgCatalogListener) alterTableAddColumn(schema *model.SchemaMetadata, table *model.TableMetadata, columndef parser.IColumnDefContext, ifNotExists bool) {- if columndef == nil {- return- }- columnName := pgparser.NormalizePostgreSQLColid(columndef.Colid())- // Check if column already exists- if table.GetColumn(columnName) != nil {- if ifNotExists {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: fmt.Sprintf("The column %q already exists in table %q", columnName, table.GetProto().Name),- Content: fmt.Sprintf("The column %q already exists in table %q", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Extract column type- var typeString string- if columndef.Typename() != nil {- typeString = extractTypeNameFromContext(columndef.Typename())- }- // Create column metadata- col := &storepb.ColumnMetadata{- Name: columnName,- Position: int32(len(table.GetProto().GetColumns()) + 1),- Nullable: true,- Type: typeString,- Default: "",- }- // Process column constraints if any (inline processing like in createColumn)- if columndef.Colquallist() != nil {- allQuals := columndef.Colquallist().AllColconstraint()- for _, qual := range allQuals {- if qual.Colconstraintelem() == nil {- continue- }- elem := qual.Colconstraintelem()- // Handle NOT NULL- if elem.NOT() != nil && elem.NULL_P() != nil {- col.Nullable = false- }- // Handle DEFAULT- if elem.DEFAULT() != nil {- if elem.B_expr() != nil {- defaultValue := elem.B_expr().GetText()- col.Default = defaultValue- }- }- // Handle UNIQUE - creates an index- if elem.UNIQUE() != nil && (elem.PRIMARY() == nil || elem.KEY() == nil) {- var constraintName string- if qual.Name() != nil {- constraintName = pgparser.NormalizePostgreSQLName(qual.Name())- }- if constraintName == "" {- constraintName = generateIndexName(table.GetProto().Name, []string{columnName}, true)- }- // Check for collision- if table.GetIndex(constraintName) != nil {- constraintName = generateUniqueIndexName(schema, table.GetProto().Name, []string{columnName}, true)- }- // Create index- index := &storepb.IndexMetadata{- Name: constraintName,- Expressions: []string{columnName},- Type: "btree",- Unique: true,- Primary: false,- }- if err := table.CreateIndex(index); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- // Handle PRIMARY KEY - creates an index- if (elem.PRIMARY() != nil && elem.KEY() != nil) || (elem.UNIQUE() != nil && elem.PRIMARY() != nil) {- var constraintName string- if qual.Name() != nil {- constraintName = pgparser.NormalizePostgreSQLName(qual.Name())- }- if constraintName == "" {- constraintName = pgGeneratePrimaryKeyName(schema, table.GetProto().Name)- }- // Check for collision- if table.GetIndex(constraintName) != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.RelationExists.Int32(),- Title: fmt.Sprintf("Relation %q already exists in schema %q", constraintName, schema.GetProto().Name),- Content: fmt.Sprintf("Relation %q already exists in schema %q", constraintName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- col.Nullable = false- // Create primary key index- index := &storepb.IndexMetadata{- Name: constraintName,- Expressions: []string{columnName},- Type: "btree",- Unique: true,- Primary: true,- IsConstraint: true,- }- if err := table.CreateIndex(index); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.PrimaryKeyExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- }- }- }- // Create the column- if err := table.CreateColumn(col, nil /* columnCatalog */); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }-}--// alterTableAddConstraint handles ADD CONSTRAINT command.-func (l *pgCatalogListener) alterTableAddConstraint(schema *model.SchemaMetadata, table *model.TableMetadata, constraint parser.ITableconstraintContext) {- if constraint == nil {- return- }- // Reuse the constraint creation logic from CREATE TABLE- err := createTableConstraint(schema, table, constraint, l.currentLine)- if err != nil {- l.advice = err- }-}--// alterTableDropConstraint handles DROP CONSTRAINT command.-func (l *pgCatalogListener) alterTableDropConstraint(_ *model.SchemaMetadata, table *model.TableMetadata, constraintName string, ifExists bool) {- // Check if constraint exists as an index- if table.GetIndex(constraintName) != nil {- if err := table.DropIndex(constraintName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return- }- if !ifExists {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ConstraintNotExists.Int32(),- Title: fmt.Sprintf("Constraint %q for table %q does not exist", constraintName, table.GetProto().Name),- Content: fmt.Sprintf("Constraint %q for table %q does not exist", constraintName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }-}--// alterTableSetDefault handles ALTER COLUMN SET DEFAULT command.-func (l *pgCatalogListener) alterTableSetDefault(table *model.TableMetadata, columnName string, defaultValue string) {- column := table.GetColumn(columnName)- if column == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- column.GetProto().Default = defaultValue-}--// alterTableDropDefault handles ALTER COLUMN DROP DEFAULT command.-func (l *pgCatalogListener) alterTableDropDefault(table *model.TableMetadata, columnName string) {- column := table.GetColumn(columnName)- if column == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- column.GetProto().Default = ""-}--// alterTableSetNotNull handles ALTER COLUMN SET NOT NULL command.-func (l *pgCatalogListener) alterTableSetNotNull(table *model.TableMetadata, columnName string) {- column := table.GetColumn(columnName)- if column == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- column.GetProto().Nullable = false-}--// renameTable handles RENAME TO for tables.-func (*pgCatalogListener) pgRenameTable(schema *model.SchemaMetadata, table *model.TableMetadata, newName string, line int) *storepb.Advice {- // Check if new name already exists- if schema.GetTable(newName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.RelationExists.Int32(),- Title: fmt.Sprintf("Relation %q already exists in schema %q", newName, schema.GetProto().Name),- Content: fmt.Sprintf("Relation %q already exists in schema %q", newName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Use the RenameTable method from schema- oldName := table.GetProto().Name- if err := schema.RenameTable(oldName, newName); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- return nil-}--// renameColumn handles RENAME COLUMN.-func (*pgCatalogListener) renameColumn(table *model.TableMetadata, oldName string, newName string, line int) *storepb.Advice {- if oldName == newName {- return nil- }- // Validate old column exists- if table.GetColumn(oldName) == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", oldName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", oldName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Validate new column doesn't already exist- if table.GetColumn(newName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: fmt.Sprintf("Column `%s` already exists in table `%s`", newName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` already exists in table `%s`", newName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Use the RenameColumn method- if err := table.RenameColumn(oldName, newName); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: fmt.Sprintf("failed to rename column: %v", err),- Content: fmt.Sprintf("failed to rename column: %v", err),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Rename column in all indexes that reference it- for _, index := range table.GetProto().Indexes {- for i, key := range index.Expressions {- if key == oldName {- index.Expressions[i] = newName- }- }- }- return nil-}--// renameConstraint handles RENAME CONSTRAINT.-func (*pgCatalogListener) renameConstraint(schema *model.SchemaMetadata, table *model.TableMetadata, oldName string, newName string, line int) *storepb.Advice {- index := table.GetIndex(oldName)- if index == nil {- // We haven't dealt with foreign and check constraints, so skip if not exists- return nil- }- // Check if new name already exists- if table.GetIndex(newName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.RelationExists.Int32(),- Title: fmt.Sprintf("Relation %q already exists in schema %q", newName, schema.GetProto().Name),- Content: fmt.Sprintf("Relation %q already exists in schema %q", newName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- // Use the RenameIndex method- if err := table.RenameIndex(oldName, newName); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- return nil-}--// ========================================-// DROP statements handling-// ========================================-// EnterDropstmt handles DROP TABLE/VIEW/INDEX statements.-func (l *pgCatalogListener) EnterDropstmt(ctx *parser.DropstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Check IF EXISTS- ifExists := ctx.IF_P() != nil && ctx.EXISTS() != nil- // Check object type and get list of names- if ctx.Object_type_any_name() != nil {- objType := ctx.Object_type_any_name()- if objType.TABLE() != nil {- // DROP TABLE- if ctx.Any_name_list() != nil {- for _, anyName := range ctx.Any_name_list().AllAny_name() {- if err := l.dropTable(anyName, ifExists); err != nil {- l.advice = err- return- }- }- }- } else if objType.MATERIALIZED() != nil && objType.VIEW() != nil {- // DROP MATERIALIZED VIEW- if ctx.Any_name_list() != nil {- for _, anyName := range ctx.Any_name_list().AllAny_name() {- if err := l.dropMaterializedView(anyName, ifExists); err != nil {- l.advice = err- return- }- }- }- } else if objType.VIEW() != nil {- // DROP VIEW- if ctx.Any_name_list() != nil {- for _, anyName := range ctx.Any_name_list().AllAny_name() {- if err := l.dropView(anyName, ifExists); err != nil {- l.advice = err- return- }- }- }- } else if objType.INDEX() != nil {- // DROP INDEX- if ctx.Any_name_list() != nil {- for _, anyName := range ctx.Any_name_list().AllAny_name() {- if err := l.dropIndex(anyName, ifExists); err != nil {- l.advice = err- return- }- }- }- }- } else if ctx.Drop_type_name() != nil && ctx.Drop_type_name().SCHEMA() != nil {- // DROP SCHEMA- if ctx.Name_list() != nil {- for _, schemaName := range ctx.Name_list().AllName() {- if err := l.dropSchema(schemaName, ifExists); err != nil {- l.advice = err- return- }- }- }- }-}--// ========================================-// DROP DATABASE handling-// ========================================-// EnterDropdbstmt handles DROP DATABASE statements.-// PostgreSQL does not allow dropping the currently connected database.-// Walk-through also disallows DROP DATABASE for other databases as it's out of scope.-func (l *pgCatalogListener) EnterDropdbstmt(ctx *parser.DropdbstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Extract database name- if ctx.Name() == nil {- return- }- databaseName := pgparser.NormalizePostgreSQLName(ctx.Name())- // PostgreSQL does not allow dropping the currently open database.- // This matches the real PostgreSQL behavior: "ERROR: cannot drop the currently open database"- if isCurrentDatabase(l.databaseState, databaseName) {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Cannot drop the currently open database %q", databaseName),- Content: fmt.Sprintf("Cannot drop the currently open database %q", databaseName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // DROP DATABASE for other databases is out of scope for single-database walk-through- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.DatabaseName()),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }-}-func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists bool) *storepb.Advice {- parts := pgparser.NormalizePostgreSQLAnyName(anyName)- if len(parts) == 0 {- return nil- }- var schemaName, tableName string- if len(parts) == 1 {- schemaName = ""- tableName = parts[0]- } else {- schemaName = parts[0]- tableName = parts[1]- }- schema, table := l.findTable(schemaName, tableName)- if table == nil {- if ifExists {- return nil- }- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- // Check if any views depend on this table- dependentViews := schema.GetDependentViews(tableName, "")- if len(dependentViews) > 0 {- // Format view names with schema prefix to match old error format- var viewNames []string- for _, v := range dependentViews {- viewNames = append(viewNames, fmt.Sprintf("%q.%q", schema.GetProto().Name, v))- }- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableIsReferencedByView.Int32(),- Title: fmt.Sprintf("Cannot drop table %q.%q, it's referenced by view: %s", schema.GetProto().Name, tableName, strings.Join(viewNames, ", ")),- Content: fmt.Sprintf("Cannot drop table %q.%q, it's referenced by view: %s", schema.GetProto().Name, tableName, strings.Join(viewNames, ", ")),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- // Drop the table using the schema method- if err := schema.DropTable(tableName); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return nil-}-func (l *pgCatalogListener) dropView(anyName parser.IAny_nameContext, ifExists bool) *storepb.Advice {- parts := pgparser.NormalizePostgreSQLAnyName(anyName)- if len(parts) == 0 {- return nil- }- var schemaName, viewName string- switch len(parts) {- case 1:- viewName = parts[0]- case 2:- schemaName = parts[0]- viewName = parts[1]- default:- return nil- }- schema, view := l.findView(schemaName, viewName)- if view == nil {- if !ifExists {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ViewNotExists.Int32(),- Title: fmt.Sprintf("View %q does not exist", viewName),- Content: fmt.Sprintf("View %q does not exist", viewName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return nil- }- // Try to drop the view- if dropErr := schema.DropView(view.Name); dropErr != nil {- if !ifExists {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ViewNotExists.Int32(),- Title: dropErr.Error(),- Content: dropErr.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- }- return nil-}-func (l *pgCatalogListener) dropMaterializedView(anyName parser.IAny_nameContext, ifExists bool) *storepb.Advice {- parts := pgparser.NormalizePostgreSQLAnyName(anyName)- if len(parts) == 0 {- return nil- }- var schemaName, viewName string- switch len(parts) {- case 1:- viewName = parts[0]- case 2:- schemaName = parts[0]- viewName = parts[1]- default:- return nil- }- schema, view := l.findMaterializedView(schemaName, viewName)- if view == nil {- if ifExists {- return nil- }- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ViewNotExists.Int32(),- Title: fmt.Sprintf("Materialized view %q does not exist", viewName),- Content: fmt.Sprintf("Materialized view %q does not exist", viewName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- // Try to drop the materialized view- if dropErr := schema.DropMaterializedView(view.Name); dropErr != nil {- if !ifExists {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ViewNotExists.Int32(),- Title: dropErr.Error(),- Content: dropErr.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- }- return nil-}-func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists bool) *storepb.Advice {- parts := pgparser.NormalizePostgreSQLAnyName(anyName)- if len(parts) == 0 {- return nil- }- var schemaName, indexName string- if len(parts) == 1 {- schemaName = ""- indexName = parts[0]- } else {- schemaName = parts[0]- indexName = parts[1]- }- schema, index := l.findIndex(schemaName, indexName)- if index == nil {- if ifExists {- return nil- }- indexTitle := fmt.Sprintf("Index %q does not exist", indexName)- if schema != nil {- indexTitle = fmt.Sprintf("Index %q does not exist in schema %q", indexName, schema.GetProto().Name)- }- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: indexTitle,- Content: indexTitle,- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- // Get the owner name (table or materialized view)- ownerName := index.GetTableProto().Name- // Try dropping from table first- table := schema.GetTable(ownerName)- if table != nil {- if dropErr := table.DropIndex(indexName); dropErr != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: dropErr.Error(),- Content: dropErr.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return nil- }- // Try dropping from materialized view- if dropErr := schema.DropMaterializedViewIndex(ownerName, indexName); dropErr != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: dropErr.Error(),- Content: dropErr.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return nil-}-func (l *pgCatalogListener) dropSchema(schemaNameCtx parser.INameContext, ifExists bool) *storepb.Advice {- schemaName := pgparser.NormalizePostgreSQLName(schemaNameCtx)- schema := l.databaseState.GetSchemaMetadata(schemaName)- if schema == nil {- if ifExists {- return nil- }- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: fmt.Sprintf("Schema %q does not exist", schemaName),- Content: fmt.Sprintf("Schema %q does not exist", schemaName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- // Drop the schema (this will also drop all objects in the schema)- // Note: We don't check for objects in the schema to match the old behavior- // In real PostgreSQL, this would require CASCADE if the schema is not empty- if err := l.databaseState.DropSchema(schemaName); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return nil-}--// TODO: EnterDropindexstmt - Need to find correct ANTLR context name-//-// func (l *pgCatalogListener) EnterDropindexstmt(ctx *parser.DropIndexContext) {-// if !isTopLevel(ctx.GetParent()) || l.advice != nil {-// return-// }-//-// if !l.checkDatabaseNotDeleted() {-// return-// }-//-// l.currentLine = ctx.GetStart().GetLine()-//-// // TODO: Implement DROP INDEX logic-// // Similar to pgDropIndexList() in walk_through_for_pg.go-// }-//-// TODO: EnterDropschemastatement - Need to find correct ANTLR context name-//-// func (l *pgCatalogListener) EnterDropschemastatement(ctx *parser.DropschemaContext) {-// if !isTopLevel(ctx.GetParent()) || l.advice != nil {-// return-// }-//-// if !l.checkDatabaseNotDeleted() {-// return-// }-//-// l.currentLine = ctx.GetStart().GetLine()-//-// // TODO: Implement DROP SCHEMA logic-// // Similar to pgDropSchema() in walk_through_for_pg.go-// }-//-// ========================================-// RENAME statements handling-// ========================================-// EnterRenamestmt handles RENAME INDEX/CONSTRAINT/TABLE/COLUMN statements.-func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Check if this is INDEX rename (ALTER INDEX ... RENAME TO ...)- if ctx.INDEX() != nil {- // ALTER INDEX index_name RENAME TO new_name- if ctx.Qualified_name() != nil && ctx.AllName() != nil && len(ctx.AllName()) > 0 {- indexName := extractTableName(ctx.Qualified_name())- schemaName := extractSchemaName(ctx.Qualified_name())- newName := pgparser.NormalizePostgreSQLName(ctx.AllName()[0])- schema, index := l.findIndex(schemaName, indexName)- if index == nil {- // Index not found, silently ignore (PostgreSQL behavior)- return- }- // Find the index across all tables- foundTable, _, err := getIndexFromSchema(schema, index.GetProto().Name)- if err != nil {- // Index not found, silently ignore (PostgreSQL behavior)- return- }- if err := l.renameConstraint(schema, foundTable, index.GetProto().Name, newName, l.currentLine); err != nil {- l.advice = err- }- }- return- }- // Check if this is VIEW rename (ALTER VIEW ... RENAME TO ...)- if ctx.Qualified_name() != nil && ctx.AllName() != nil && len(ctx.AllName()) > 0 {- // Could be ALTER VIEW view_name RENAME TO new_name- // Try to see if the qualified name refers to a view- viewName := extractTableName(ctx.Qualified_name())- schemaName := extractSchemaName(ctx.Qualified_name())- newName := pgparser.NormalizePostgreSQLName(ctx.AllName()[0])- schema, view := l.findView(schemaName, viewName)- if view != nil {- if err := schema.RenameView(view.Name, newName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ViewNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }- return- }- }- // Extract relation (table) if present- var tableName, schemaName string- if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {- tableName = extractTableName(ctx.Relation_expr().Qualified_name())- schemaName = extractSchemaName(ctx.Relation_expr().Qualified_name())- }- schema, table := l.findTable(schemaName, tableName)- // Check if this is column rename- if ctx.Opt_column() != nil {- // RENAME COLUMN: ALTER TABLE table RENAME COLUMN oldname TO newname- // Column names use Name(), not Colid() in RENAME statements- allNames := ctx.AllName()- if len(allNames) >= 2 && tableName != "" {- oldName := pgparser.NormalizePostgreSQLName(allNames[0])- newName := pgparser.NormalizePostgreSQLName(allNames[1])- if table == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- if err := l.renameColumn(table, oldName, newName, l.currentLine); err != nil {- l.advice = err- }- }- return- }- // Check if this is constraint rename- if ctx.CONSTRAINT() != nil && tableName != "" {- // RENAME CONSTRAINT: ALTER TABLE table RENAME CONSTRAINT oldname TO newname- allNames := ctx.AllName()- if len(allNames) >= 2 {- oldName := pgparser.NormalizePostgreSQLName(allNames[0])- newName := pgparser.NormalizePostgreSQLName(allNames[1])- if table == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- if err := l.renameConstraint(schema, table, oldName, newName, l.currentLine); err != nil {- l.advice = err- }- }- return- }- // Otherwise it's table rename: ALTER TABLE oldname RENAME TO newname- if tableName != "" && ctx.AllName() != nil && len(ctx.AllName()) > 0 {- newName := pgparser.NormalizePostgreSQLName(ctx.AllName()[0])- if table == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- if err := l.pgRenameTable(schema, table, newName, l.currentLine); err != nil {- l.advice = err- }- return- }-}--// ========================================-// CREATE VIEW handling-// ========================================-// EnterViewstmt handles CREATE VIEW statements.-func (l *pgCatalogListener) EnterViewstmt(ctx *parser.ViewstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Extract view name- if ctx.Qualified_name() == nil {- return- }- viewName := extractTableName(ctx.Qualified_name())- schemaName := extractSchemaName(ctx.Qualified_name())- databaseName := extractDatabaseName(ctx.Qualified_name())- // Check if accessing other database- if databaseName != "" && l.databaseState.DatabaseName() != databaseName {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- schema, err := l.resolveSchemaForCreate(schemaName, l.currentLine)- if err != nil {- l.advice = err- return- }- // Check if view already exists - silently ignore duplicates- // This matches the legacy behavior in the old DatabaseState implementation- if existingView := schema.GetView(viewName); existingView != nil {- return- }- // Get the view definition (the SELECT statement)- // We don't parse the dependencies yet - that would require full SQL analysis- // For now, create the view with empty dependency list- definition := ""- if ctx.Selectstmt() != nil {- definition = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.Selectstmt())- }- // Create the view- _, createErr := schema.CreateView(viewName, definition, nil)- if createErr != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ViewExists.Int32(),- Title: createErr.Error(),- Content: createErr.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }-}--// ========================================-// CREATE MATERIALIZED VIEW handling-// ========================================-// EnterCreatematviewstmt handles CREATE MATERIALIZED VIEW statements.-func (l *pgCatalogListener) EnterCreatematviewstmt(ctx *parser.CreatematviewstmtContext) {- if !isTopLevel(ctx.GetParent()) || l.advice != nil {- return- }- l.currentLine = ctx.GetStart().GetLine()- // Extract materialized view name from Create_mv_target- if ctx.Create_mv_target() == nil || ctx.Create_mv_target().Qualified_name() == nil {- return- }- viewName := extractTableName(ctx.Create_mv_target().Qualified_name())- schemaName := extractSchemaName(ctx.Create_mv_target().Qualified_name())- databaseName := extractDatabaseName(ctx.Create_mv_target().Qualified_name())- // Check if accessing other database- if databaseName != "" && l.databaseState.DatabaseName() != databaseName {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.DatabaseName()),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- schema, err := l.resolveSchemaForCreate(schemaName, l.currentLine)- if err != nil {- l.advice = err- return- }- // Check IF NOT EXISTS clause- ifNotExists := ctx.IF_P() != nil && ctx.NOT() != nil && ctx.EXISTS() != nil- // Check if materialized view already exists- if schema.GetMaterializedView(viewName) != nil {- if ifNotExists {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: fmt.Sprintf("Materialized view %q already exists in schema %q", viewName, schema.GetProto().Name),- Content: fmt.Sprintf("Materialized view %q already exists in schema %q", viewName, schema.GetProto().Name),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- return- }- // Get the view definition (the SELECT statement)- definition := ""- if ctx.Selectstmt() != nil {- definition = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.Selectstmt())- }- // Create the materialized view- _, createErr := schema.CreateMaterializedView(viewName, definition)- if createErr != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: createErr.Error(),- Content: createErr.Error(),- StartPosition: &storepb.Position{Line: int32(l.currentLine)},- }- }-}--// ========================================-// Helper functions-// ========================================-// isTopLevel checks if the context is at the top level (not nested).-func isTopLevel(ctx antlr.Tree) bool {- if ctx == nil {- return true- }- switch ctx := ctx.(type) {- case *parser.RootContext, *parser.StmtblockContext:- return true- case *parser.StmtmultiContext, *parser.StmtContext:- return isTopLevel(ctx.GetParent())- default:- return false- }-}--// extractTypeNameFromContext extracts the type name from a Typename context.-// Simply uses GetText() to get the full type representation.-// PostgreSQL normalizes some type names (e.g., int -> integer),-// which will be handled by the parser's type normalization.-func extractTypeNameFromContext(typename parser.ITypenameContext) string {- if typename == nil {- return ""- }- // Use GetText() to get the full type string- // The parser should have already normalized type names- typeText := typename.GetText()- // Normalize common PostgreSQL type aliases- switch strings.ToLower(typeText) {- case "int", "int4":- return "integer"- case "int2":- return "smallint"- case "int8":- return "bigint"- case "float4":- return "real"- case "float8":- return "double precision"- case "bool":- return "boolean"- default:- return typeText- }-}--// extractTableName extracts the table name from a qualified_name context.-// For "schema.table" or "db.schema.table", returns "table"-func extractTableName(qualifiedName parser.IQualified_nameContext) string {- if qualifiedName == nil {- return ""- }- parts := pgparser.NormalizePostgreSQLQualifiedName(qualifiedName)- if len(parts) == 0 {- return ""- }- // Last part is always the table/object name- return parts[len(parts)-1]-}--// extractSchemaName extracts the schema name from a qualified_name context.-// For "schema.table", returns "schema"-// For "db.schema.table", returns "schema"-// For "table", returns ""-func extractSchemaName(qualifiedName parser.IQualified_nameContext) string {- if qualifiedName == nil {- return ""- }- parts := pgparser.NormalizePostgreSQLQualifiedName(qualifiedName)- switch len(parts) {- case 1:- // Just table name, no schema- return ""- case 2:- // schema.table- return parts[0]- case 3:- // db.schema.table- return parts[1]- default:- return ""- }-}--// extractDatabaseName extracts the database name from a qualified_name context.-// For "db.schema.table", returns "db"-// For "schema.table" or "table", returns ""-func extractDatabaseName(qualifiedName parser.IQualified_nameContext) string {- if qualifiedName == nil {- return ""- }- parts := pgparser.NormalizePostgreSQLQualifiedName(qualifiedName)- if len(parts) == 3 {- // db.schema.table- return parts[0]- }- return ""-}--// generateIndexName generates an index name based on table name and columns.-// Format: tablename_col1_col2_idx (with suffix for uniqueness if needed)-func generateIndexName(tableName string, columnList []string, _ bool) string {- var builder strings.Builder- builder.WriteString(tableName)- expressionID := 0- for _, col := range columnList {- builder.WriteByte('_')- if col == "expr" {- builder.WriteString("expr")- if expressionID > 0 {- fmt.Fprintf(&builder, "%d", expressionID)- }- expressionID++- } else {- builder.WriteString(col)- }- }- builder.WriteString("_idx")- return builder.String()-}--// generateUniqueIndexName generates a unique index name by adding numeric suffixes.-// Tries name1, name2, name3, etc. until finding an available name.-func generateUniqueIndexName(schema *model.SchemaMetadata, tableName string, columnList []string, isUnique bool) string {- baseName := generateIndexName(tableName, columnList, isUnique)- // Try with numeric suffixes starting from 1- for i := 1; i < 1000; i++ {- candidateName := fmt.Sprintf("%s%d", baseName, i)- // Check if index exists in the schema- if schema.GetIndex(candidateName) == nil {- return candidateName- }- }- // Fallback (should never reach here)- return fmt.Sprintf("%s_collision", baseName)-}--// pgGeneratePrimaryKeyName generates a primary key name following PostgreSQL conventions.-// Uses the pattern "{tableName}_pkey" and adds numeric suffixes if there are collisions.-func pgGeneratePrimaryKeyName(schema *model.SchemaMetadata, tableName string) string {- pkName := fmt.Sprintf("%s_pkey", tableName)- // For PostgreSQL, check if the index name already exists in the schema- if schema.GetIndex(pkName) == nil {- return pkName- }- suffix := 1- for {- candidateName := fmt.Sprintf("%s%d", pkName, suffix)- if schema.GetIndex(candidateName) == nil {- return candidateName- }- suffix++- }-}--// getOrCreatePublicSchema gets a schema by name, or creates the "public" schema if it doesn't exist.-// For PostgreSQL, the "public" schema is auto-created, but other schemas must exist.-func getOrCreatePublicSchema(d *model.DatabaseMetadata, schemaName string, line int) (*model.SchemaMetadata, *storepb.Advice) {- if schemaName == "" {- schemaName = PublicSchemaName- }- schema := d.GetSchemaMetadata(schemaName)- if schema != nil {- return schema, nil- }- // Only auto-create the "public" schema- if schemaName != PublicSchemaName {- return nil, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: fmt.Sprintf("The schema %q doesn't exist", schemaName),- Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),- StartPosition: &storepb.Position{Line: int32(line)},- }- }- return d.CreateSchema(PublicSchemaName), nil-}--// getIndexFromSchema finds an index by name within a schema and returns the table and index.-// For PostgreSQL, index names are unique within a schema (not just within a table).-func getIndexFromSchema(s *model.SchemaMetadata, indexName string) (*model.TableMetadata, *model.IndexMetadata, *storepb.Advice) {- // For PostgreSQL, index names are unique within a schema- index := s.GetIndex(indexName)- if index == nil {- return nil, nil, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: fmt.Sprintf("Index %q does not exist in schema %q", indexName, s.GetProto().Name),- Content: fmt.Sprintf("Index %q does not exist in schema %q", indexName, s.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- // Find the table that owns this index- table := s.GetTable(index.GetTableProto().Name)- if table == nil {- return nil, nil, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table %q does not exist in schema %q", index.GetTableProto().Name, s.GetProto().Name),- Content: fmt.Sprintf("Table %q does not exist in schema %q", index.GetTableProto().Name, s.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- return table, index, nil-}--// isCurrentDatabase returns true if the given database is the current database.-func isCurrentDatabase(d *model.DatabaseMetadata, database string) bool {- return d.DatabaseName() == database+ return WalkThroughOmni(ctx, d, ast)}diff --git a/backend/plugin/schema/pg/walk_through_omni.go b/backend/plugin/schema/pg/walk_through_omni.gonew file mode 100644index 0000000000..8a3d46d5f9--- /dev/null+++ b/backend/plugin/schema/pg/walk_through_omni.go@@ -0,0 +1,468 @@+package pg++import (+ "fmt"+ "slices"++ "github.com/bytebase/omni/pg/catalog"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/plugin/schema"+ "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)+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 {+ return &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.DDLSimulationFailed.Int32(),+ Title: "Failed to generate schema DDL",+ 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)+ }++ 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})+ if execErr != nil {+ return &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.DDLSimulationFailed.Int32(),+ Title: "DDL simulation failed",+ Content: execErr.Error(),+ StartPosition: &storepb.Position{Line: 0},+ }+ }++ // Step 4: Report the first error from the simulation.+ for _, r := range results {+ if r.Error == nil {+ continue+ }+ errCode := mapSQLSTATEToCode(r.Error)+ return &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: errCode.Int32(),+ Title: "DDL simulation failed",+ Content: r.Error.Error(),+ StartPosition: &storepb.Position{+ Line: int32(r.Line),+ },+ }+ }++ // 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)++ return nil+}++// mapSQLSTATEToCode converts an omni catalog error to a bytebase error code.+func mapSQLSTATEToCode(err error) code.Code {+ catErr, ok := err.(*catalog.Error)+ if !ok {+ return code.DDLSimulationFailed+ }++ switch catErr.Code {+ // Table/relation errors+ case catalog.CodeDuplicateTable:+ return code.TableExists+ case catalog.CodeUndefinedTable:+ return code.TableNotExists++ // Column errors+ case catalog.CodeDuplicateColumn:+ return code.ColumnExists+ case catalog.CodeUndefinedColumn, catalog.CodeInvalidColumnDefinition:+ return code.ColumnNotExists++ // Schema errors+ case catalog.CodeDuplicateSchema:+ return code.RelationExists+ case catalog.CodeUndefinedSchema:+ return code.SchemaNotExists++ // Index/constraint errors+ case catalog.CodeDuplicateObject, catalog.CodeUniqueViolation:+ return code.IndexExists+ case catalog.CodeUndefinedObject:+ return code.IndexNotExists++ // FK errors+ case catalog.CodeInvalidFK, catalog.CodeForeignKeyViolation:+ return code.TableHasFK++ // PK/table definition errors+ case catalog.CodeDuplicatePKey:+ return code.PrimaryKeyExists++ // Type/expression errors+ case catalog.CodeDatatypeMismatch:+ return code.ChangeColumnType+ case catalog.CodeNotNullViolation:+ return code.ColumnCannotNull++ // Syntax+ case catalog.CodeSyntaxError:+ return code.StatementSyntaxError++ // Naming+ case catalog.CodeReservedName:+ return code.NameIsKeywordIdentifier++ default:+ return code.DDLSimulationFailed+ }+}++func getConfiguredSearchPath(d *model.DatabaseMetadata) []string {+ configured := d.GetConfiguredSearchPath()+ if len(configured) == 0 {+ return nil+ }++ searchPath := make([]string, 0, len(configured))+ for _, item := range configured {+ if item.CurrentUser {+ searchPath = append(searchPath, "$user")+ continue+ }+ if item.Schema == "" {+ continue+ }+ searchPath = append(searchPath, item.Schema)+ }+ 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{+ Name: rel.Name,+ }++ // Columns.+ for _, col := range rel.Columns {+ colMeta := &storepb.ColumnMetadata{+ Name: col.Name,+ Position: int32(col.AttNum),+ Type: c.FormatType(col.TypeOID, col.TypeMod),+ Nullable: !col.NotNull,+ Default: col.Default,+ }+ if col.Generated == 's' {+ colMeta.Generation = &storepb.GenerationMetadata{+ Type: storepb.GenerationMetadata_TYPE_STORED,+ Expression: col.GenerationExpr,+ }+ }+ if col.Identity != 0 {+ colMeta.IsIdentity = true+ switch col.Identity {+ case 'a':+ colMeta.IdentityGeneration = storepb.ColumnMetadata_ALWAYS+ case 'd':+ colMeta.IdentityGeneration = storepb.ColumnMetadata_BY_DEFAULT+ default:+ }+ }+ table.Columns = append(table.Columns, colMeta)+ }++ // Indexes.+ for _, idx := range c.IndexesOf(rel.OID) {+ idxMeta := &storepb.IndexMetadata{+ Name: idx.Name,+ Type: idx.AccessMethod,+ Unique: idx.IsUnique,+ Primary: idx.IsPrimary,+ }++ // Build expressions list.+ exprIdx := 0+ for i, attnum := range idx.Columns {+ if attnum == 0 {+ // Expression column.+ if exprIdx < len(idx.Exprs) {+ idxMeta.Expressions = append(idxMeta.Expressions, idx.Exprs[exprIdx])+ exprIdx+++ }+ } else {+ // Regular column — find name.+ colName := ""+ for _, col := range rel.Columns {+ if col.AttNum == attnum {+ colName = col.Name+ break+ }+ }+ idxMeta.Expressions = append(idxMeta.Expressions, colName)+ }++ // Descending flag.+ if i < len(idx.IndOption) {+ idxMeta.Descending = append(idxMeta.Descending, idx.IndOption[i]&1 != 0)+ }+ }++ // IsConstraint: index backs a constraint if ConstraintOID != 0.+ if idx.ConstraintOID != 0 {+ idxMeta.IsConstraint = true+ }++ table.Indexes = append(table.Indexes, idxMeta)+ }++ // Foreign keys and check constraints from constraints.+ for _, con := range c.ConstraintsOf(rel.OID) {+ switch con.Type {+ case 'f': // FK+ fk := &storepb.ForeignKeyMetadata{+ Name: con.Name,+ }+ // Columns.+ for _, attnum := range con.Columns {+ for _, col := range rel.Columns {+ if col.AttNum == attnum {+ fk.Columns = append(fk.Columns, col.Name)+ break+ }+ }+ }+ // Referenced table.+ fRel := c.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+ }+ }+ }+ }+ // Actions.+ fk.OnUpdate = fkActionToString(con.FKUpdAction)+ fk.OnDelete = fkActionToString(con.FKDelAction)+ fk.MatchType = fkMatchToString(con.FKMatchType)+ table.ForeignKeys = append(table.ForeignKeys, fk)+ case 'c': // CHECK+ table.CheckConstraints = append(table.CheckConstraints, &storepb.CheckConstraintMetadata{+ Name: con.Name,+ Expression: con.CheckExpr,+ })+ case 'x': // EXCLUDE+ table.ExcludeConstraints = append(table.ExcludeConstraints, &storepb.ExcludeConstraintMetadata{+ Name: con.Name,+ Expression: con.CheckExpr,+ })+ default:+ }+ }++ return table+}++// relationToViewProto converts an omni Relation (view) to a storepb.ViewMetadata.+func relationToViewProto(c *catalog.Catalog, rel *catalog.Relation) *storepb.ViewMetadata {+ view := &storepb.ViewMetadata{+ Name: rel.Name,+ }+ if rel.Schema != nil {+ if def, err := c.GetViewDefinition(rel.Schema.Name, rel.Name); err == nil {+ view.Definition = def+ }+ }+ return view+}++// relationToMatViewProto converts an omni Relation (materialized view) to a storepb.MaterializedViewMetadata.+func relationToMatViewProto(c *catalog.Catalog, rel *catalog.Relation) *storepb.MaterializedViewMetadata {+ mv := &storepb.MaterializedViewMetadata{+ Name: rel.Name,+ }+ if rel.Schema != nil {+ if def, err := c.GetMatViewDefinition(rel.Schema.Name, rel.Name); err == nil {+ mv.Definition = def+ }+ }++ // Materialized view indexes.+ for _, idx := range c.IndexesOf(rel.OID) {+ idxMeta := &storepb.IndexMetadata{+ Name: idx.Name,+ Type: idx.AccessMethod,+ Unique: idx.IsUnique,+ Primary: idx.IsPrimary,+ }+ exprIdx := 0+ for i, attnum := range idx.Columns {+ if attnum == 0 {+ if exprIdx < len(idx.Exprs) {+ idxMeta.Expressions = append(idxMeta.Expressions, idx.Exprs[exprIdx])+ exprIdx+++ }+ } else {+ for _, col := range rel.Columns {+ if col.AttNum == attnum {+ idxMeta.Expressions = append(idxMeta.Expressions, col.Name)+ break+ }+ }+ }++ // Descending flag.+ if i < len(idx.IndOption) {+ idxMeta.Descending = append(idxMeta.Descending, idx.IndOption[i]&1 != 0)+ }+ }++ if idx.ConstraintOID != 0 {+ idxMeta.IsConstraint = true+ }++ mv.Indexes = append(mv.Indexes, idxMeta)+ }++ 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"+ }+}diff --git a/backend/plugin/schema/pg/walk_through_test.go b/backend/plugin/schema/pg/walk_through_test.goindex 5a69d98d41..be2ed4a68d 100644--- a/backend/plugin/schema/pg/walk_through_test.go+++ b/backend/plugin/schema/pg/walk_through_test.go@@ -76,8 +76,8 @@ func TestWalkThrough(t *testing.T) {}tests := []testData{}- filepath := filepath.Join("testdata", "walk_through.yaml")- yamlFile, err := os.Open(filepath)+ targetPath := filepath.Join("testdata", "walk_through.yaml")+ yamlFile, err := os.Open(targetPath)require.NoError(t, err)defer yamlFile.Close()@@ -87,7 +87,8 @@ func TestWalkThrough(t *testing.T) {require.NoError(t, err)sm := sheet.NewManager()- for _, test := range tests {+ for i := range tests {+ test := &tests[i]// Make a deep copy to avoid mutation across testsprotoData, ok := proto.Clone(originDatabase).(*storepb.DatabaseSchemaMetadata)require.True(t, ok)@@ -97,11 +98,13 @@ func TestWalkThrough(t *testing.T) {stmts, _ := sm.GetStatementsForChecks(storepb.Engine_POSTGRES, test.Statement)asts := base.ExtractASTs(stmts)- advice := WalkThrough(state, asts)+ advice := WalkThroughWithContext(schema.WalkThroughContext{RawSQL: test.Statement}, state, asts)if advice != nil {// Compare the advice fields- require.Equal(t, test.Advice.Code, advice.Code)- require.Equal(t, test.Advice.Content, advice.Content)+ if test.Advice != nil {+ require.Equal(t, test.Advice.Code, advice.Code)+ require.Equal(t, test.Advice.Content, advice.Content)+ }continue}@@ -111,7 +114,7 @@ func TestWalkThrough(t *testing.T) {}want := &storepb.DatabaseSchemaMetadata{}- err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)+ err = common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)require.NoError(t, err)result := state.GetProto()diff := cmp.Diff(want, result, protocmp.Transform(),@@ -171,8 +174,8 @@ func TestWalkThroughANTLR(t *testing.T) {}tests := []testData{}- filepath := filepath.Join("testdata", "walk_through.yaml")- yamlFile, err := os.Open(filepath)+ targetPath := filepath.Join("testdata", "walk_through.yaml")+ yamlFile, err := os.Open(targetPath)require.NoError(t, err)defer yamlFile.Close()@@ -181,7 +184,8 @@ func TestWalkThroughANTLR(t *testing.T) {err = yaml.Unmarshal(byteValue, &tests)require.NoError(t, err)- for _, test := range tests {+ for i := range tests {+ test := &tests[i]// Make a deep copy to avoid mutation across testsprotoData, ok := proto.Clone(originDatabase).(*storepb.DatabaseSchemaMetadata)require.True(t, ok)@@ -194,13 +198,16 @@ func TestWalkThroughANTLR(t *testing.T) {if parseErr != nil {t.Fatalf("Failed to parse SQL: %v\nSQL: %s", parseErr, test.Statement)}+ asts := base.ExtractASTs(stmts)// Call WalkThrough with AST- advice := WalkThrough(state, base.ExtractASTs(stmts))+ advice := WalkThroughWithContext(schema.WalkThroughContext{RawSQL: test.Statement}, state, asts)if advice != nil {// Compare the advice fields- require.Equal(t, test.Advice.Code, advice.Code)- require.Equal(t, test.Advice.Content, advice.Content)+ if test.Advice != nil {+ require.Equal(t, test.Advice.Code, advice.Code)+ require.Equal(t, test.Advice.Content, advice.Content)+ }continue}@@ -210,7 +217,7 @@ func TestWalkThroughANTLR(t *testing.T) {}want := &storepb.DatabaseSchemaMetadata{}- err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)+ err = common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)require.NoError(t, err)result := state.GetProto()diff := cmp.Diff(want, result, protocmp.Transform(),@@ -304,12 +311,15 @@ func TestWalkThroughSearchPathState(t *testing.T) {},}- for _, test := range tests {+ for i := range tests {+ test := &tests[i]t.Run(test.name, func(t *testing.T) {state := newSearchPathTestState(test.searchPath)stmts, err := base.ParseStatements(storepb.Engine_POSTGRES, test.sql)require.NoError(t, err)- advice := WalkThroughWithContext(test.session, state, base.ExtractASTs(stmts))+ ctx := test.session+ ctx.RawSQL = test.sql+ advice := WalkThroughWithContext(ctx, state, base.ExtractASTs(stmts))require.Nil(t, advice)test.assert(t, state)})diff --git a/backend/plugin/schema/schema.go b/backend/plugin/schema/schema.goindex 53030b4364..51c4cf146d 100644--- a/backend/plugin/schema/schema.go+++ b/backend/plugin/schema/schema.go@@ -47,6 +47,9 @@ type walkThroughWithContext func(WalkThroughContext, *model.DatabaseMetadata, []// WalkThroughContext carries optional session state into schema walk-through implementations.type WalkThroughContext struct {SessionUser string+ // RawSQL is the original SQL text to be executed.+ // Used by the omni-based walkthrough for catalog.Exec().+ RawSQL string}type GetDefinitionContext struct {diff --git a/backend/store/model/database.go b/backend/store/model/database.goindex 739a13a2d6..45f91d152a 100644--- a/backend/store/model/database.go+++ b/backend/store/model/database.go@@ -193,6 +193,20 @@ func (d *DatabaseMetadata) GetProto() *storepb.DatabaseSchemaMetadata {return d.proto}+// ReplaceFrom replaces the internal state of this DatabaseMetadata with that+// of another. The receiver pointer remains stable so callers that hold a+// reference to it continue to see the updated state.+func (d *DatabaseMetadata) ReplaceFrom(other *DatabaseMetadata) {+ d.proto = other.proto+ d.config = other.config+ d.rawDump = other.rawDump+ d.isObjectCaseSensitive = other.isObjectCaseSensitive+ d.isDetailCaseSensitive = other.isDetailCaseSensitive+ d.searchPath = other.searchPath+ d.internal = other.internal+ d.linkedDatabase = other.linkedDatabase+}+func (d *DatabaseMetadata) GetRawDump() []byte {return d.rawDump}diff --git a/backend/tests/gitops_test.go b/backend/tests/gitops_test.goindex 3a377ce9f1..25f8b6ede8 100644--- a/backend/tests/gitops_test.go+++ b/backend/tests/gitops_test.go@@ -11,6 +11,7 @@ import ("github.com/bytebase/bytebase/backend/common"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/plugin/advisor/code")func TestGitOpsCheck(t *testing.T) {@@ -1170,15 +1171,17 @@ func TestGitOpsCheckVersionedDependency(t *testing.T) {if tc.expectError {a.NotEmpty(errorAdvices, "Expected SQL review error for altering non-existent table t2")- // Validate the error message mentions the missing table.+ // Accept omni-native missing-table errors instead of legacy ANTLR titles.foundTableError := falsefor _, advice := range errorAdvices {- if advice.Title == "Table `t2` does not exist" {+ if advice.Code == int32(code.TableNotExists) {foundTableError = true+ a.NotEqual("Table `t2` does not exist", advice.Title, "Should not require the legacy ANTLR advice title")+ a.Contains(advice.Content, `relation "t2" does not exist`, "Error content should preserve the omni-native missing-table message")a.Contains(advice.Content, "t2", "Error content should mention table t2")}}- a.True(foundTableError, "Expected error about table t2 not existing")+ a.True(foundTableError, "Expected a missing-table SQL review error for table t2")} else {a.Empty(errorAdvices, "Expected no SQL review errors for valid migration sequence")}diff --git a/backend/tests/test-data/sql_review_pg.yaml b/backend/tests/test-data/sql_review_pg.yamlindex 4ae684af4a..acda52f2d3 100644--- a/backend/tests/test-data/sql_review_pg.yaml+++ b/backend/tests/test-data/sql_review_pg.yaml@@ -44,126 +44,18 @@);COMMENT ON TABLE "userTable" IS 'User table';result:- - |-- {- "status": "ERROR",- "title": "TABLE_REQUIRE_PK",- "content": "Table \"public\".\"userTable\" requires PRIMARY KEY, related statement: \"CREATE TABLE \\\"userTable\\\"(\\n id INT,\\n name VARCHAR(255),\\n \\\"roomId\\\" INT,\\n CONSTRAINT uk1 UNIQUE (id, name),\\n CONSTRAINT fk1 FOREIGN KEY (\\\"roomId\\\") REFERENCES room(id)\\n )\"",- "code": 601,- "sqlReviewReport": {- "startPosition": {- "line": 1- }- }- }- - |-- {- "status": "ERROR",- "title": "TABLE_NO_FOREIGN_KEY",- "content": "Foreign key is not allowed in the table \"public\".\"userTable\", related statement: \"CREATE TABLE \"userTable\"(\n id INT,\n name VARCHAR(255),\n \"roomId\" INT,\n CONSTRAINT uk1 UNIQUE (id, name),\n CONSTRAINT fk1 FOREIGN KEY (\"roomId\") REFERENCES room(id)\n )\"",- "code": 602,- "sqlReviewReport": {- "startPosition": {- "line": 1- }- }- }- - |-- {- "status": "WARNING",- "title": "NAMING_TABLE",- "content": "\"userTable\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- "code": 301,- "sqlReviewReport": {- "startPosition": {- "line": 1- }- }- }- - |-- {- "status": "WARNING",- "title": "NAMING_COLUMN",- "content": "\"userTable\".\"roomId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- "code": 302,- "sqlReviewReport": {- "startPosition": {- "line": 4- }- }- }- - |-- {- "status": "WARNING",- "title": "NAMING_INDEX_UK",- "content": "Unique key in table \"userTable\" mismatches the naming convention, expect \"^$|^uk_userTable_id_name$\" but found \"uk1\"",- "code": 304,- "sqlReviewReport": {- "startPosition": {- "line": 5- }- }- }- |-{"status": "WARNING",- "title": "NAMING_INDEX_FK",- "content": "Foreign key in table \"userTable\" mismatches the naming convention, expect \"^$|^fk_userTable_roomId_room_id$\" but found \"fk1\"",- "code": 305,+ "title": "DDL simulation failed",+ "content": "ERROR: relation \"room\" does not exist (SQLSTATE 42P01)",+ "code": 604,"sqlReviewReport": {"startPosition": {"line": 1}}}- - |-- {- "status": "WARNING",- "title": "COLUMN_REQUIRED",- "content": "Table \"userTable\" requires columns: created_ts, creator_id, updated_ts, updater_id",- "code": 401,- "sqlReviewReport": {- "startPosition": {- "line": 1- }- }- }- - |-- {- "status": "WARNING",- "title": "COLUMN_NO_NULL",- "content": "Column \"id\" in \"public\".\"userTable\" cannot have NULL value",- "code": 402,- "sqlReviewReport": {- "startPosition": {- "line": 2- }- }- }- - |-- {- "status": "WARNING",- "title": "COLUMN_NO_NULL",- "content": "Column \"name\" in \"public\".\"userTable\" cannot have NULL value",- "code": 402,- "sqlReviewReport": {- "startPosition": {- "line": 3- }- }- }- - |-- {- "status": "WARNING",- "title": "COLUMN_NO_NULL",- "content": "Column \"roomId\" in \"public\".\"userTable\" cannot have NULL value",- "code": 402,- "sqlReviewReport": {- "startPosition": {- "line": 4- }- }- }run: false- statement: DELETE FROM tresult:diff --git a/go.mod b/go.modindex f009a59647..b59b3bf744 100644--- a/go.mod+++ b/go.mod@@ -34,7 +34,7 @@ require (github.com/bmatcuk/doublestar/v4 v4.10.0github.com/bytebase/gomongo v0.0.0-20260211083148-d2ea344b4b65github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0- github.com/bytebase/omni v0.0.0-20260313074616-3f695e9ee28c+ github.com/bytebase/omni v0.0.0-20260315111911-38f1bd6535ecgithub.com/caarlos0/env/v11 v11.4.0github.com/cenkalti/backoff/v5 v5.0.3github.com/cockroachdb/cockroachdb-parser v0.25.2diff --git a/go.sum b/go.sumindex 435c1db61e..76f88d6940 100644--- a/go.sum+++ b/go.sum@@ -253,8 +253,8 @@ github.com/bytebase/gomongo v0.0.0-20260211083148-d2ea344b4b65 h1:RvViQx0e24DeDJgithub.com/bytebase/gomongo v0.0.0-20260211083148-d2ea344b4b65/go.mod h1:4dDI0RO2BK9/BD4VE+YqvlCj/qoVMquZ99QZdRlT8Wg=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-20260313074616-3f695e9ee28c h1:+snIwvonEDmEhP0ypzkJhJKM/ocDqjJlVHrhcTTqG0I=-github.com/bytebase/omni v0.0.0-20260313074616-3f695e9ee28c/go.mod h1:sHnw/Do16VheYa+ThYDBzY98EbkwFwWBzxh2CQuOgD0=+github.com/bytebase/omni v0.0.0-20260315111911-38f1bd6535ec h1:amEmyabgmxPPg8eVaPbb2nOxdIT4pUpLE9A2NpIRSdE=+github.com/bytebase/omni v0.0.0-20260315111911-38f1bd6535ec/go.mod h1:sHnw/Do16VheYa+ThYDBzY98EbkwFwWBzxh2CQuOgD0=github.com/bytebase/parser v0.0.0-20260228025849-39c76b45c98a h1:hTyZhniv0HzQYM2K6mG1XpOEKibsfLJ7jYhBf/1sXLc=github.com/bytebase/parser v0.0.0-20260228025849-39c76b45c98a/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=