Skip to content

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

feat: enable PostgreSQL walk-through (#4295)

rebelice5549a6c7cc01
diff --git a/plugin/advisor/catalog/state.go b/plugin/advisor/catalog/state.go
index e283238b8a..7af7e0f6ad 100644
--- a/plugin/advisor/catalog/state.go
+++ b/plugin/advisor/catalog/state.go
@@ -19,6 +19,7 @@ func newDatabaseState(d *storepb.DatabaseMetadata, context *FinderContext) *Data
collation: d.Collation,
dbType: context.EngineType,
schemaSet: make(schemaStateMap),
+ usable: true,
}
for _, schema := range d.Schemas {
@@ -111,6 +112,9 @@ func newIndexState(i *storepb.IndexMetadata) *IndexState {
visible: newBoolPointer(i.Visible),
comment: newStringPointer(i.Comment),
expressionList: copyStringSlice(i.Expressions),
+ // We rudely think that pk and uk are constraints here.
+ // But in fact, we can create uk by CREATE UNIQUE INDEX statements.
+ isConstraint: *newBoolPointer(i.Primary || i.Unique),
}
return index
}
@@ -124,6 +128,7 @@ type DatabaseState struct {
dbType db.Type
schemaSet schemaStateMap
deleted bool
+ usable bool
}
// HasNoTable returns true if the current database has no table.
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index 08377e9723..620a6f9f55 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -199,7 +199,16 @@ func (d *DatabaseState) WalkThrough(stmt string) error {
case db.MySQL, db.TiDB:
return d.mysqlWalkThrough(stmt)
case db.Postgres:
- return d.pgWalkThrough(stmt)
+ if err := d.pgWalkThrough(stmt); err != nil {
+ if d.ctx.CheckIntegrity {
+ return err
+ }
+ // For PostgreSQL, we only support to walk-through with CheckIntegrity == true.
+ // If CheckIntegrity == false, we'll try to walk-through with empty public schema.
+ // We use `usable` to check if walk-through successfully.
+ d.usable = false
+ }
+ return nil
default:
return &WalkThroughError{
Type: ErrorTypeUnsupported,
diff --git a/plugin/advisor/catalog/walk_through_for_pg.go b/plugin/advisor/catalog/walk_through_for_pg.go
index cc446604b7..27fbd9a342 100644
--- a/plugin/advisor/catalog/walk_through_for_pg.go
+++ b/plugin/advisor/catalog/walk_through_for_pg.go
@@ -15,7 +15,7 @@ const (
func (d *DatabaseState) pgWalkThrough(stmt string) error {
nodeList, err := pgParse(stmt)
if err != nil {
- return err
+ return NewParseError(err.Error())
}
for _, node := range nodeList {
@@ -907,10 +907,19 @@ func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, *WalkThrough
}
schema, exists := d.schemaSet[schemaName]
if !exists {
- return nil, &WalkThroughError{
- Type: ErrorTypeSchemaNotExists,
- Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),
+ if schemaName != publicSchemaName {
+ return nil, &WalkThroughError{
+ Type: ErrorTypeSchemaNotExists,
+ Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),
+ }
+ }
+ schema = &SchemaState{
+ name: publicSchemaName,
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
+ identifierMap: make(identifierMap),
}
+ d.schemaSet[publicSchemaName] = schema
}
return schema, nil
}
diff --git a/plugin/advisor/pg/test/column_no_null.yaml b/plugin/advisor/pg/test/column_no_null.yaml
index 7c72fa8fbf..26e34922fd 100644
--- a/plugin/advisor/pg/test/column_no_null.yaml
+++ b/plugin/advisor/pg/test/column_no_null.yaml
@@ -42,33 +42,36 @@
title: OK
content: ""
line: 0
-- statement: ALTER TABLE book ADD COLUMN id int
+- statement: ALTER TABLE tech_book ADD COLUMN reader int
want:
- status: WARN
code: 402
title: column.no-null
- content: Column "id" in "public"."book" cannot have NULL value
+ content: Column "reader" in "public"."tech_book" cannot have NULL value
line: 1
-- statement: ALTER TABLE book ADD COLUMN id int PRIMARY KEY, ADD COLUMN name varchar(255) NOT NULL
+- statement: |-
+ ALTER TABLE tech_book DROP COLUMN id;
+ ALTER TABLE tech_book DROP COLUMN name;
+ ALTER TABLE tech_book ADD COLUMN id int PRIMARY KEY, ADD COLUMN name varchar(255) NOT NULL;
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: ALTER TABLE book ALTER COLUMN id SET NOT NULL
+- statement: ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: ALTER TABLE book ALTER COLUMN id DROP NOT NULL
+- statement: ALTER TABLE tech_book ALTER COLUMN name DROP NOT NULL
want:
- status: WARN
code: 402
title: column.no-null
- content: Column "id" in "public"."book" cannot have NULL value
+ content: Column "name" in "public"."tech_book" cannot have NULL value
line: 1
- statement: /* this is a comment */
want:
diff --git a/plugin/advisor/pg/test/column_required.yaml b/plugin/advisor/pg/test/column_required.yaml
index 489637ea8d..18b47d743c 100644
--- a/plugin/advisor/pg/test/column_required.yaml
+++ b/plugin/advisor/pg/test/column_required.yaml
@@ -18,17 +18,21 @@
title: OK
content: ""
line: 0
-- statement: ALTER TABLE book RENAME COLUMN creator_id TO creator;
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN creator_id int;
+ ALTER TABLE tech_book RENAME COLUMN creator_id TO creator;
want:
- status: WARN
code: 401
title: column.required
- content: 'Table "book" requires columns: creator_id'
- line: 1
-- statement: ALTER TABLE book DROP COLUMN creator_id;
+ content: 'Table "tech_book" requires columns: creator_id'
+ line: 2
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN creator_id int;
+ ALTER TABLE tech_book DROP COLUMN creator_id;
want:
- status: WARN
code: 401
title: column.required
- content: 'Table "book" requires columns: creator_id'
- line: 1
+ content: 'Table "tech_book" requires columns: creator_id'
+ line: 2
diff --git a/plugin/advisor/pg/test/index_key_number_limit.yaml b/plugin/advisor/pg/test/index_key_number_limit.yaml
index 0f5f61305f..b150c4f700 100644
--- a/plugin/advisor/pg/test/index_key_number_limit.yaml
+++ b/plugin/advisor/pg/test/index_key_number_limit.yaml
@@ -33,24 +33,30 @@
title: index.key-number-limit
content: The number of index `t_id_name` in table `t` should be not greater than 5
line: 1
-- statement: CREATE INDEX idx_address_phone ON address(id, phone, c1, c2, c3, c4);
+- statement: |-
+ CREATE TABLE address(id int, phone varchar(225), c1 int, c2 int, c3 int, c4 int);
+ CREATE INDEX idx_address_phone ON address(id, phone, c1, c2, c3, c4);
want:
- status: WARN
code: 802
title: index.key-number-limit
content: The number of index `idx_address_phone` in table `address` should be not greater than 5
- line: 1
-- statement: CREATE UNIQUE INDEX idx_address_phone ON address(id, phone, c2, c1, c3, c4);
+ line: 2
+- statement: |-
+ CREATE TABLE address(id int, phone varchar(225), c1 int, c2 int, c3 int, c4 int);
+ CREATE UNIQUE INDEX idx_address_phone ON address(id, phone, c2, c1, c3, c4);
want:
- status: WARN
code: 802
title: index.key-number-limit
content: The number of index `idx_address_phone` in table `address` should be not greater than 5
- line: 1
-- statement: ALTER TABLE t ADD CONSTRAINT t_id_name UNIQUE (id, name, c1, c2, c3, c4);
+ line: 2
+- statement: |-
+ CREATE TABLE address(id int, phone varchar(225), c1 int, c2 int, c3 int, c4 int);
+ ALTER TABLE address ADD CONSTRAINT address_id_name UNIQUE (id, phone, c1, c2, c3, c4);
want:
- status: WARN
code: 802
title: index.key-number-limit
- content: The number of index `t_id_name` in table `t` should be not greater than 5
- line: 1
+ content: The number of index `address_id_name` in table `address` should be not greater than 5
+ line: 2
diff --git a/plugin/advisor/pg/test/naming_index_pk.yaml b/plugin/advisor/pg/test/naming_index_pk.yaml
index f25a7a7691..905fec17c0 100644
--- a/plugin/advisor/pg/test/naming_index_pk.yaml
+++ b/plugin/advisor/pg/test/naming_index_pk.yaml
@@ -19,7 +19,7 @@
title: naming.index.pk
content: Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_id_name$" but found "udmhjtnsaablcmjhqcznfiwtnevcehcvw"
line: 1
-- statement: CREATE TABLE tech_book(id INT, name VARCHAR(20), CONSTRAINT pk_tech_book_name PRIMARY KEY (name))
+- statement: CREATE TABLE book(id INT, name VARCHAR(20), CONSTRAINT pk_book_name PRIMARY KEY (name))
want:
- status: SUCCESS
code: 0
@@ -28,7 +28,7 @@
line: 0
- statement: |-
-- this is the first line.
- CREATE TABLE tech_book(
+ CREATE TABLE book(
id INT,
name VARCHAR(20),
CONSTRAINT tech_book_name PRIMARY KEY (name)
@@ -37,16 +37,16 @@
- status: WARN
code: 306
title: naming.index.pk
- content: Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_name$" but found "tech_book_name"
+ content: Primary key in table "book" mismatches the naming convention, expect "^$|^pk_book_name$" but found "tech_book_name"
line: 5
-- statement: CREATE TABLE tech_book(id INT, name VARCHAR(20), PRIMARY KEY (name))
+- statement: CREATE TABLE book(id INT, name VARCHAR(20), PRIMARY KEY (name))
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: CREATE TABLE tech_book(id INT, name VARCHAR(20) PRIMARY KEY)
+- statement: CREATE TABLE book(id INT, name VARCHAR(20) PRIMARY KEY)
want:
- status: SUCCESS
code: 0
diff --git a/plugin/advisor/pg/test/naming_index_uk.yaml b/plugin/advisor/pg/test/naming_index_uk.yaml
index 4a5ecb3f9c..8f2bc3641d 100644
--- a/plugin/advisor/pg/test/naming_index_uk.yaml
+++ b/plugin/advisor/pg/test/naming_index_uk.yaml
@@ -33,7 +33,7 @@
title: naming.index.uk
content: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "tech_book_id_name"
line: 1
-- statement: CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), CONSTRAINT uk_tech_book_name UNIQUE (name))
+- statement: CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(20), CONSTRAINT uk_book_name UNIQUE (name))
want:
- status: SUCCESS
code: 0
@@ -42,25 +42,25 @@
line: 0
- statement: |-
-- this is the first line.
- CREATE TABLE tech_book(
+ CREATE TABLE book(
id INT PRIMARY KEY,
name VARCHAR(20),
- CONSTRAINT tech_book_name UNIQUE (name)
+ CONSTRAINT book_name UNIQUE (name)
)
want:
- status: WARN
code: 304
title: naming.index.uk
- content: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_name$" but found "tech_book_name"
+ content: Unique key in table "book" mismatches the naming convention, expect "^$|^uk_book_name$" but found "book_name"
line: 5
-- statement: CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE (name))
+- statement: CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE (name))
want:
- status: SUCCESS
code: 0
title: OK
content: ""
line: 0
-- statement: CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20) UNIQUE)
+- statement: CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(20) UNIQUE)
want:
- status: SUCCESS
code: 0
diff --git a/plugin/advisor/pg/test/naming_table.yaml b/plugin/advisor/pg/test/naming_table.yaml
index dec2826971..b9a79812a2 100644
--- a/plugin/advisor/pg/test/naming_table.yaml
+++ b/plugin/advisor/pg/test/naming_table.yaml
@@ -26,7 +26,7 @@
title: OK
content: ""
line: 0
-- statement: CREATE TABLE tech_book(id int, name varchar(255))
+- statement: CREATE TABLE book(id int, name varchar(255))
want:
- status: SUCCESS
code: 0
@@ -40,7 +40,9 @@
title: naming.table
content: '"TechBook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'
line: 1
-- statement: ALTER TABLE techBook RENAME TO tech_book
+- statement: |-
+ create table techBook(id int);
+ ALTER TABLE techBook RENAME TO tech_book_tmp;
want:
- status: SUCCESS
code: 0
diff --git a/plugin/advisor/pg/test/schema_backward_compatibility.yaml b/plugin/advisor/pg/test/schema_backward_compatibility.yaml
index b211555c3d..10ba031797 100644
--- a/plugin/advisor/pg/test/schema_backward_compatibility.yaml
+++ b/plugin/advisor/pg/test/schema_backward_compatibility.yaml
@@ -1,7 +1,7 @@
-- statement: ALTER TABLE t1 ALTER COLUMN f1 TYPE TEXT
+- statement: ALTER TABLE tech_book ALTER COLUMN name TYPE TEXT
want:
- status: WARN
code: 111
title: schema.backward-compatibility
- content: '"ALTER TABLE t1 ALTER COLUMN f1 TYPE TEXT" may cause incompatibility with the existing data and code'
+ content: '"ALTER TABLE tech_book ALTER COLUMN name TYPE TEXT" may cause incompatibility with the existing data and code'
line: 1
diff --git a/plugin/advisor/pg/test/system_collation_allowlist.yaml b/plugin/advisor/pg/test/system_collation_allowlist.yaml
index 9dd31f1066..40704800be 100644
--- a/plugin/advisor/pg/test/system_collation_allowlist.yaml
+++ b/plugin/advisor/pg/test/system_collation_allowlist.yaml
@@ -12,12 +12,12 @@
title: system.collation.allowlist
content: Use disabled collation "unknown", related statement "CREATE TABLE t(a text COLLATE "unknown")"
line: 1
-- statement: ALTER TABLE t ADD COLUMN c1 text COLLATE "unknown"
+- statement: ALTER TABLE tech_book ADD COLUMN c1 text COLLATE "unknown"
want:
- status: WARN
code: 1201
title: system.collation.allowlist
- content: Use disabled collation "unknown", related statement "ALTER TABLE t ADD COLUMN c1 text COLLATE "unknown""
+ content: Use disabled collation "unknown", related statement "ALTER TABLE tech_book ADD COLUMN c1 text COLLATE "unknown""
line: 1
- statement: ALTER TABLE tech_book ALTER COLUMN id TYPE text COLLATE "unknown"
want:
diff --git a/plugin/advisor/pg/test/table_require_pk.yaml b/plugin/advisor/pg/test/table_require_pk.yaml
index f9b7f08eae..49ff47050f 100644
--- a/plugin/advisor/pg/test/table_require_pk.yaml
+++ b/plugin/advisor/pg/test/table_require_pk.yaml
@@ -47,7 +47,9 @@
title: table.require-pk
content: 'Table "public"."tech_book" requires PRIMARY KEY, related statement: "ALTER TABLE \"tech_book\" DROP COLUMN id"'
line: 1
-- statement: ALTER TABLE "tech_book" DROP COLUMN column_not_in_pk
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN column_not_in_pk int;
+ ALTER TABLE "tech_book" DROP COLUMN column_not_in_pk;
want:
- status: SUCCESS
code: 0
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index e2ee105cee..c94dc7f601 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -431,7 +431,7 @@ func SQLReviewCheck(statements string, ruleList []*SQLReviewRule, checkContext S
finder := checkContext.Catalog.GetFinder()
switch checkContext.DbType {
- case db.TiDB, db.MySQL:
+ case db.TiDB, db.MySQL, db.Postgres:
if err := finder.WalkThrough(statements); err != nil {
return convertWalkThroughErrorToAdvice(err)
}
@@ -626,6 +626,14 @@ func convertWalkThroughErrorToAdvice(err error) ([]Advice, error) {
Content: walkThroughError.Content,
Line: walkThroughError.Line,
})
+ default:
+ res = append(res, Advice{
+ Status: Error,
+ Code: Internal,
+ Title: "Failed to walk-through",
+ Content: walkThroughError.Content,
+ Line: walkThroughError.Line,
+ })
}
return res, nil
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index 656679ab2f..e91bb6f957 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -187,7 +187,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType db.Type,
if record {
tests[i].Want = adviceList
} else {
- require.Equal(t, tc.Want, adviceList, tc.Statement)
+ require.Equalf(t, tc.Want, adviceList, "rule: %s, statements: %s", rule, tc.Statement)
}
}
diff --git a/tests/sql_review_test.go b/tests/sql_review_test.go
index 10de059228..a65ff8ad19 100644
--- a/tests/sql_review_test.go
+++ b/tests/sql_review_test.go
@@ -469,7 +469,7 @@ func createIssueAndReturnSQLReviewResult(a *require.Assertions, ctl *controller,
if wait {
a.Equal(1, len(result))
- a.Equal(common.Ok.Int(), result[0].Code)
+ a.Equal(common.Ok.Int(), result[0].Code, result[0])
status, err := ctl.waitIssuePipeline(issue.ID)
a.NoError(err)
a.Equal(api.TaskDone, status)