Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: refactor the test framework for plugin/advisor/pg (#3879)
rebelice8d08f9d488f6
diff --git a/plugin/advisor/pg/advisor_column_no_null_test.go b/plugin/advisor/pg/advisor_column_no_null_test.godeleted file mode 100644index b5e5eca7b4..0000000000--- a/plugin/advisor/pg/advisor_column_no_null_test.go+++ /dev/null@@ -1,145 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestColumnNoNULL(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `- CREATE TABLE book (- id int,- name varchar(255)- )`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "id" in "public"."book" cannot have NULL value`,- Line: 3,- },- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "name" in "public"."book" cannot have NULL value`,- Line: 4,- },- },- },- {- Statement: "CREATE TABLE book(id int, name varchar(255), PRIMARY KEY (id))",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "name" in "public"."book" cannot have NULL value`,- Line: 1,- },- },- },- {- Statement: "CREATE TABLE book(id int PRIMARY KEY, name varchar(255))",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "name" in "public"."book" cannot have NULL value`,- Line: 1,- },- },- },- {- Statement: "CREATE TABLE book(id int NOT NULL, name varchar(255))",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "name" in "public"."book" cannot have NULL value`,- Line: 1,- },- },- },- {- Statement: "CREATE TABLE book(id int PRIMARY KEY, name varchar(255) NOT NULL)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE book ADD COLUMN id int",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "id" in "public"."book" cannot have NULL value`,- Line: 1,- },- },- },- {- Statement: "ALTER TABLE book ADD COLUMN id int PRIMARY KEY, ADD COLUMN name varchar(255) NOT NULL",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE book ALTER COLUMN id SET NOT NULL",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE book ALTER COLUMN id DROP NOT NULL",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.ColumnCannotNull,- Title: "column.no-null",- Content: `Column "id" in "public"."book" cannot have NULL value`,- Line: 1,- },- },- },- {- Statement: "/* this is a comment */",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &ColumnNoNullAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleColumnNotNull,- Level: advisor.SchemaRuleLevelWarning,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_column_required_test.go b/plugin/advisor/pg/advisor_column_required_test.godeleted file mode 100644index 78883a823e..0000000000--- a/plugin/advisor/pg/advisor_column_required_test.go+++ /dev/null@@ -1,82 +0,0 @@-package pg--import (- "encoding/json"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestColumnRequirement(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "CREATE TABLE book(id int)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NoRequiredColumn,- Title: "column.required",- Content: "Table \"book\" requires columns: created_ts, creator_id, updated_ts, updater_id",- Line: 1,- },- },- },- {- Statement: `CREATE TABLE book(- id int,- creator_id int,- created_ts timestamp,- updater_id int,- updated_ts timestamp)`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `ALTER TABLE book RENAME COLUMN creator_id TO creator;`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NoRequiredColumn,- Title: "column.required",- Content: "Table \"book\" requires columns: creator_id",- Line: 1,- },- },- },- {- Statement: `ALTER TABLE book DROP COLUMN creator_id;`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NoRequiredColumn,- Title: "column.required",- Content: "Table \"book\" requires columns: creator_id",- Line: 1,- },- },- },- }- payload, err := json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{- "id",- "created_ts",- "updated_ts",- "creator_id",- "updater_id",- },- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &ColumnRequirementAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleRequiredColumn,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_column_type_disallow_list_test.go b/plugin/advisor/pg/advisor_column_type_disallow_list_test.godeleted file mode 100644index df616508b4..0000000000--- a/plugin/advisor/pg/advisor_column_type_disallow_list_test.go+++ /dev/null@@ -1,76 +0,0 @@-package pg--import (- "encoding/json"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestColumnTypeDisallowList(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `CREATE TABLE t(a char(5));`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE t(a int, b bigint, c real);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DisabledColumnType,- Title: "column.type-disallow-list",- Content: "Disallow column type INT but column \"t\".\"a\" is",- Line: 1,- },- {- Status: advisor.Warn,- Code: advisor.DisabledColumnType,- Title: "column.type-disallow-list",- Content: "Disallow column type BIGINT but column \"t\".\"b\" is",- Line: 1,- },- {- Status: advisor.Warn,- Code: advisor.DisabledColumnType,- Title: "column.type-disallow-list",- Content: "Disallow column type REAL but column \"t\".\"c\" is",- Line: 1,- },- },- },- {- Statement: `- CREATE TABLE t(d char(5));- ALTER TABLE t add COLUMN a int;`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DisabledColumnType,- Title: "column.type-disallow-list",- Content: "Disallow column type INT but column \"t\".\"a\" is",- Line: 3,- },- },- },- }-- payload, err := json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{"int", "float", "bigint", "real"},- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &ColumnTypeDisallowListAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleColumnTypeDisallowList,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_comment_convention_test.go b/plugin/advisor/pg/advisor_comment_convention_test.godeleted file mode 100644index c163fdfc09..0000000000--- a/plugin/advisor/pg/advisor_comment_convention_test.go+++ /dev/null@@ -1,74 +0,0 @@-package pg--// Framework code is generated by the generator.--import (- "encoding/json"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestCommentConvention(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `comment on column public.name is 'is a column name'`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Line: 0,- },- },- },- {- Statement: `comment on column public.name is 'is a to long column name'`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CommentTooLong,- Title: "comment.length",- Content: "The length of comment should be within 20 characters",- Line: 1,- },- },- },- {- Statement: `comment on table public is 'is a table name'`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Line: 0,- },- },- },- {- Statement: `comment on table public is 'is a to long table name'`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CommentTooLong,- Title: "comment.length",- Content: "The length of comment should be within 20 characters",- Line: 1,- },- },- },- }-- payload, err := json.Marshal(advisor.CommentConventionRulePayload{- Required: true,- MaxLength: 20,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &CommentConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleCommentLength,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_encoding_allowlist_test.go b/plugin/advisor/pg/advisor_encoding_allowlist_test.godeleted file mode 100644index c8eab498a7..0000000000--- a/plugin/advisor/pg/advisor_encoding_allowlist_test.go+++ /dev/null@@ -1,69 +0,0 @@-package pg--import (- "encoding/json"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestCharsetAllowlist(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `CREATE DATABASE test WITH ENCODING 'UTF8'`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE DATABASE test`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE DATABASE test WITH ENCODING 'LATIN1'`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DisabledCharset,- Title: "system.charset.allowlist",- Content: "\"\" used disabled encoding 'latin1'",- },- },- },- {- Statement: "/* this is a comment */",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- }-- payload, err := json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{"utf8"},- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &EncodingAllowlistAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleCharsetAllowlist,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_index_key_number_limit_test.go b/plugin/advisor/pg/advisor_index_key_number_limit_test.godeleted file mode 100644index 5d03640e23..0000000000--- a/plugin/advisor/pg/advisor_index_key_number_limit_test.go+++ /dev/null@@ -1,118 +0,0 @@-package pg--import (- "encoding/json"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestIndexKeyNumberLimitAdvisor(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `CREATE TABLE t(name char(20));`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE t(name varchar(225));`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE t(name char(225), PRIMARY KEY (name));`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE t(id int, name char(225), CONSTRAINT t_id_name PRIMARY KEY (id, name));`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.IndexKeyNumberExceedsLimit,- Title: "index.key-number-limit",- Content: "The number of index `t_id_name` in table `t` should be not greater than 1",- Line: 1,- },- },- },- {- Statement: `CREATE TABLE t(id int, name char(225), CONSTRAINT t_id_name UNIQUE (id, name));`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.IndexKeyNumberExceedsLimit,- Title: "index.key-number-limit",- Content: "The number of index `t_id_name` in table `t` should be not greater than 1",- Line: 1,- },- },- },- {- Statement: `CREATE INDEX idx_address_phone ON address(id, phone);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.IndexKeyNumberExceedsLimit,- Title: "index.key-number-limit",- Content: "The number of index `idx_address_phone` in table `address` should be not greater than 1",- Line: 1,- },- },- },- {- Statement: `CREATE UNIQUE INDEX idx_address_phone ON address(id, phone);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.IndexKeyNumberExceedsLimit,- Title: "index.key-number-limit",- Content: "The number of index `idx_address_phone` in table `address` should be not greater than 1",- Line: 1,- },- },- },- {- Statement: `ALTER TABLE t ADD CONSTRAINT t_id_name UNIQUE (id, name);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.IndexKeyNumberExceedsLimit,- Title: "index.key-number-limit",- Content: "The number of index `t_id_name` in table `t` should be not greater than 1",- Line: 1,- },- },- },- }-- payload, err := json.Marshal(advisor.NumberTypeRulePayload{- Number: 1,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &IndexKeyNumberLimitAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleIndexKeyNumberLimit,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_index_no_duplicate_column_test.go b/plugin/advisor/pg/advisor_index_no_duplicate_column_test.godeleted file mode 100644index c54c7f2ad0..0000000000--- a/plugin/advisor/pg/advisor_index_no_duplicate_column_test.go+++ /dev/null@@ -1,99 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestIndexNoDuplicateColumn(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `CREATE TABLE t (a INT, PRIMARY KEY (a));`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE t (- a int,- PRIMARY KEY (a, a));`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DuplicateColumnInIndex,- Title: "index.no-duplicate-column",- Content: "PRIMARY KEY \"\" has duplicate column \"t\".\"a\"",- Line: 3,- },- },- },- {- Statement: `- CREATE TABLE t(a int);- CREATE INDEX idx_a on t(a, a);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DuplicateColumnInIndex,- Title: "index.no-duplicate-column",- Content: "INDEX \"idx_a\" has duplicate column \"t\".\"a\"",- Line: 3,- },- },- },- {- Statement: `- CREATE TABLE t(a int);- ALTER TABLE t ADD CONSTRAINT uk_a UNIQUE (a, a);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DuplicateColumnInIndex,- Title: "index.no-duplicate-column",- Content: "UNIQUE KEY \"uk_a\" has duplicate column \"t\".\"a\"",- Line: 3,- },- },- },- {- Statement: `- CREATE TABLE t(a int);- ALTER TABLE t ADD CONSTRAINT pk_a PRIMARY KEY (a, a);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DuplicateColumnInIndex,- Title: "index.no-duplicate-column",- Content: "PRIMARY KEY \"pk_a\" has duplicate column \"t\".\"a\"",- Line: 3,- },- },- },- {- Statement: `- CREATE TABLE t(a int);- ALTER TABLE t ADD CONSTRAINT fk_a FOREIGN KEY (a, a) REFERENCES t1(a, b);`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.DuplicateColumnInIndex,- Title: "index.no-duplicate-column",- Content: "FOREIGN KEY \"fk_a\" has duplicate column \"t\".\"a\"",- Line: 3,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &IndexNoDuplicateColumnAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleIndexNoDuplicateColumn,- Level: advisor.SchemaRuleLevelWarning,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_insert_row_limit_test.go b/plugin/advisor/pg/advisor_insert_row_limit_test.godeleted file mode 100644index 05e6b162a6..0000000000--- a/plugin/advisor/pg/advisor_insert_row_limit_test.go+++ /dev/null@@ -1,49 +0,0 @@-package pg--// Framework code is generated by the generator.--import (- "encoding/json"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestInsertRowLimit(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: `INSERT INTO tech_book values(1, 'a')`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `INSERT INTO tech_book values(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.InsertTooManyRows,- Title: "statement.insert.row-limit",- Content: "The value rows in \"INSERT INTO tech_book values(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')\" should be no more than 5, but found 6",- Line: 1,- },- },- },- }- payload, err := json.Marshal(advisor.NumberTypeRulePayload{- Number: 5,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &InsertRowLimitAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleStatementInsertRowLimit,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_migration_compatibility_test.go b/plugin/advisor/pg/advisor_migration_compatibility_test.godeleted file mode 100644index 31217a93b4..0000000000--- a/plugin/advisor/pg/advisor_migration_compatibility_test.go+++ /dev/null@@ -1,287 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestBasic(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "DROP DATABASE d1",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityDropDatabase,- Title: "schema.backward-compatibility",- Content: "\"DROP DATABASE d1\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "DROP TABLE t1",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityDropTable,- Title: "schema.backward-compatibility",- Content: "\"DROP TABLE t1\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 RENAME to t2",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityRenameTable,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 RENAME to t2\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "DROP VIEW v1",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityDropTable,- Title: "schema.backward-compatibility",- Content: "\"DROP VIEW v1\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "CREATE UNIQUE INDEX idx1 ON t1 (f1)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAddUniqueKey,- Title: "schema.backward-compatibility",- Content: "\"CREATE UNIQUE INDEX idx1 ON t1 (f1)\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "DROP TABLE t1;DROP TABLE t2;",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityDropTable,- Title: "schema.backward-compatibility",- Content: "\"DROP TABLE t1;\" may cause incompatibility with the existing data and code",- Line: 1,- },- {- Status: advisor.Warn,- Code: advisor.CompatibilityDropTable,- Title: "schema.backward-compatibility",- Content: "\"DROP TABLE t2;\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: `- CREATE TABLE t(a int);- CREATE UNIQUE INDEX uk_t_a on t(a);- `,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `- CREATE TABLE t(a int);- ALTER TABLE t ADD PRIMARY KEY (a);- `,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleSchemaBackwardCompatibility,- Level: advisor.SchemaRuleLevelWarning,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}--func TestAlterTable(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "ALTER TABLE t1 ADD f1 TEXT",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE t1 RENAME COLUMN f1 to f2",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityRenameColumn,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 RENAME COLUMN f1 to f2\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 DROP COLUMN f1",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityDropColumn,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 DROP COLUMN f1\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 ADD PRIMARY KEY (f1)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAddPrimaryKey,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 ADD PRIMARY KEY (f1)\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 ADD UNIQUE (f1)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAddUniqueKey,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 ADD UNIQUE (f1)\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAddForeignKey,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2)\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 ADD CHECK (f1 > 0)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAddCheck,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 ADD CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 ADD CHECK (f1 > 0) NOT VALID",- Want: []advisor.Advice{- {- Status: advisor.Success,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE t1 ADD CONSTRAINT check_f1_bigger_0 CHECK (f1 > 0)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAddCheck,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 ADD CONSTRAINT check_f1_bigger_0 CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- {- Statement: "ALTER TABLE t1 ADD CONSTRAINT check_f1_bigger_0 CHECK (f1 > 0) NOT VALID",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE t1 RENAME TO t2",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityRenameTable,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 RENAME TO t2\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleSchemaBackwardCompatibility,- Level: advisor.SchemaRuleLevelWarning,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}--func TestAlterTableChangeColumnType(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "ALTER TABLE t1 ALTER COLUMN f1 TYPE TEXT",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.CompatibilityAlterColumn,- Title: "schema.backward-compatibility",- Content: "\"ALTER TABLE t1 ALTER COLUMN f1 TYPE TEXT\" may cause incompatibility with the existing data and code",- Line: 1,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleSchemaBackwardCompatibility,- Level: advisor.SchemaRuleLevelWarning,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_naming_column_test.go b/plugin/advisor/pg/advisor_naming_column_test.godeleted file mode 100644index 57f81444e0..0000000000--- a/plugin/advisor/pg/advisor_naming_column_test.go+++ /dev/null@@ -1,125 +0,0 @@-package pg--import (- "encoding/json"- "fmt"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNamingColumnConvention(t *testing.T) {- invalidColumnName := advisor.RandomString(33)-- tests := []advisor.TestCase{- {- Statement: "CREATE TABLE book(id int, \"creatorId\" int)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NamingColumnConventionMismatch,- Title: "naming.column",- Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("CREATE TABLE book(id int, %s int)", invalidColumnName),- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NamingColumnConventionMismatch,- Title: "naming.column",- Content: fmt.Sprintf("\"book\".\"%s\" mismatches column naming convention, its length should be within 32 characters", invalidColumnName),- Line: 1,- },- },- },- {- // PostgreSQL uses lowercase by default unless using double quotes.- Statement: "CREATE TABLE book(id int, creator_Id int)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE book(id int, creator_id int)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE book(id int, creator_id int);- ALTER TABLE book ADD COLUMN "creatorId" int`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NamingColumnConventionMismatch,- Title: "naming.column",- Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 2,- },- },- },- {- Statement: `CREATE TABLE book(id int, creator_id int);- ALTER TABLE book ADD COLUMN "creator" int`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE book(id int, creator_id int);- ALTER TABLE book RENAME COLUMN creator_id TO "creatorId"`,- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.NamingColumnConventionMismatch,- Title: "naming.column",- Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 2,- },- },- },- {- Statement: `CREATE TABLE book(id int, creator_id int);- ALTER TABLE book RENAME COLUMN creator_id TO "creator"`,- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- }- payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^[a-z]+(_[a-z]+)*$",- MaxLength: 32,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &NamingColumnConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleColumnNaming,- Level: advisor.SchemaRuleLevelWarning,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.go b/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.godeleted file mode 100644index a0e4004205..0000000000--- a/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.go+++ /dev/null@@ -1,145 +0,0 @@-package pg--import (- "encoding/json"- "fmt"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNamingFKConvention(t *testing.T) {- invalidFKName := advisor.RandomString(42)- maxLength := 32-- tests := []advisor.TestCase{- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT fk_author_id FOREIGN KEY (author_id) REFERENCES author (id)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingFKConventionMismatch,- Title: "naming.index.fk",- Content: "Foreign key in table \"tech_book\" mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found \"fk_author_id\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE tech_book ADD CONSTRAINT %s FOREIGN KEY (author_id) REFERENCES author (id)", invalidFKName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingFKConventionMismatch,- Title: "naming.index.fk",- Content: fmt.Sprintf("Foreign key in table \"tech_book\" mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found \"%s\"", invalidFKName),- Line: 1,- },- {- Status: advisor.Error,- Code: advisor.NamingFKConventionMismatch,- Title: "naming.index.fk",- Content: fmt.Sprintf("Foreign key \"%s\" in table \"tech_book\" mismatches the naming convention, its length should be within %d characters", invalidFKName, maxLength),- Line: 1,- },- },- },- {- Statement: "ALTER TABLE tech_book ADD COLUMN author_id INT CONSTRAINT fk_tech_book_author_id_author_id REFERENCES author (id)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE tech_book ADD COLUMN author_id INT CONSTRAINT fk_author_id REFERENCES author (id)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingFKConventionMismatch,- Title: "naming.index.fk",- Content: "Foreign key in table \"tech_book\" mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found \"fk_author_id\"",- Line: 1,- },- },- },- {- Statement: "CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id FOREIGN KEY (author_id) REFERENCES author (id))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingFKConventionMismatch,- Title: "naming.index.fk",- Content: "Foreign key in table \"book\" mismatches the naming convention, expect \"^fk_book_author_id_author_id$\" but found \"fk_book_author_id\"",- Line: 1,- },- },- },- {- Statement: "CREATE TABLE book(id INT, author_id INT CONSTRAINT fk_book_author_id_author_id REFERENCES author (id))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `-- this is the first line.- CREATE TABLE book(- id INT,- author_id INT CONSTRAINT fk_book_author_id REFERENCES author (id)- )`,- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingFKConventionMismatch,- Title: "naming.index.fk",- Content: "Foreign key in table \"book\" mismatches the naming convention, expect \"^fk_book_author_id_author_id$\" but found \"fk_book_author_id\"",- Line: 4,- },- },- },- }-- payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",- MaxLength: maxLength,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &NamingFKConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleFKNaming,- Level: advisor.SchemaRuleLevelError,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_naming_index_convention_test.go b/plugin/advisor/pg/advisor_naming_index_convention_test.godeleted file mode 100644index 6d787d18e0..0000000000--- a/plugin/advisor/pg/advisor_naming_index_convention_test.go+++ /dev/null@@ -1,102 +0,0 @@-package pg--import (- "encoding/json"- "fmt"- "strings"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNamingIndexConvention(t *testing.T) {- invalidIndexName := advisor.RandomString(33)-- tests := []advisor.TestCase{- {- Statement: "CREATE INDEX idx_tech_book_id_name ON tech_book(id, name)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE INDEX tech_book_id_name ON tech_book(id, name)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingIndexConventionMismatch,- Title: "naming.index.idx",- Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("CREATE INDEX %s ON tech_book(id, name)", invalidIndexName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingIndexConventionMismatch,- Title: "naming.index.idx",- Content: fmt.Sprintf("Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"%s\"", invalidIndexName),- Line: 1,- },- {- Status: advisor.Error,- Code: advisor.NamingIndexConventionMismatch,- Title: "naming.index.idx",- Content: fmt.Sprintf("Index \"%s\" in table \"tech_book\" mismatches the naming convention, its length should be within 32 characters", invalidIndexName),- Line: 1,- },- },- },- {- Statement: fmt.Sprintf(- "ALTER INDEX %s RENAME TO idx_tech_book_%s",- advisor.MockOldIndexName,- strings.Join(advisor.MockIndexColumnList, "_"),- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER INDEX %s RENAME TO idx_tech_book",- advisor.MockOldIndexName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingIndexConventionMismatch,- Title: "naming.index.idx",- Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"idx_tech_book\"",- Line: 1,- },- },- },- }-- payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^$|^idx_{{table}}_{{column_list}}$",- MaxLength: 32,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &NamingIndexConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleIDXNaming,- Level: advisor.SchemaRuleLevelError,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go b/plugin/advisor/pg/advisor_naming_primary_key_convention_test.godeleted file mode 100644index 790203ae35..0000000000--- a/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go+++ /dev/null@@ -1,213 +0,0 @@-package pg--import (- "encoding/json"- "fmt"- "strings"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNamingPKConvention(t *testing.T) {- maxLength := 32- invalidPKName := advisor.RandomString(33)-- tests := []advisor.TestCase{- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id_name PRIMARY KEY (id, name)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT tech_book_id_name PRIMARY KEY (id, name)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"tech_book_id_name\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE tech_book ADD CONSTRAINT %s PRIMARY KEY (id, name)", invalidPKName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: fmt.Sprintf("Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"%s\"", invalidPKName),- Line: 1,- },- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: fmt.Sprintf(`Primary key "%s" in table "tech_book" mismatches the naming convention, its length should be within %d characters`, invalidPKName, maxLength),- Line: 1,- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), CONSTRAINT pk_tech_book_name PRIMARY KEY (name))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `-- this is the first line.- CREATE TABLE tech_book(- id INT,- name VARCHAR(20),- CONSTRAINT tech_book_name PRIMARY KEY (name)- )`,- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- 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\"",- Line: 5,- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), PRIMARY KEY (name))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20) PRIMARY KEY)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_%s PRIMARY KEY USING INDEX %s",- strings.Join(advisor.MockIndexColumnList, "_"),- advisor.MockOldIndexName,- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book PRIMARY KEY USING INDEX %s",- advisor.MockOldIndexName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book RENAME CONSTRAINT %s TO pk_tech_book_%s",- advisor.MockOldPostgreSQLPKName,- strings.Join(advisor.MockIndexColumnList, "_"),- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book RENAME CONSTRAINT %s TO pk_tech_book",- advisor.MockOldPostgreSQLPKName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf(- "ALTER INDEX %s RENAME TO pk_tech_book_%s",- advisor.MockOldPostgreSQLPKName,- strings.Join(advisor.MockIndexColumnList, "_"),- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER INDEX %s RENAME TO pk_tech_book",- advisor.MockOldPostgreSQLPKName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",- Line: 1,- },- },- },- }-- payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^$|^pk_{{table}}_{{column_list}}$",- MaxLength: maxLength,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &NamingPKConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRulePKNaming,- Level: advisor.SchemaRuleLevelError,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_naming_table_test.go b/plugin/advisor/pg/advisor_naming_table_test.godeleted file mode 100644index 674f6e186d..0000000000--- a/plugin/advisor/pg/advisor_naming_table_test.go+++ /dev/null@@ -1,130 +0,0 @@-package pg--import (- "encoding/json"- "fmt"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestPostgreSQLNamingTableConvention(t *testing.T) {- invalidTableName := advisor.RandomString(33)-- tests := []advisor.TestCase{- {- Statement: "CREATE TABLE \"techBook\"(id int, name varchar(255))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingTableConventionMismatch,- Title: "naming.table",- Content: "\"techBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("CREATE TABLE \"%s\"(id int, name varchar(255))", invalidTableName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingTableConventionMismatch,- Title: "naming.table",- Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within 32 characters", invalidTableName),- Line: 1,- },- },- },- {- Statement: "CREATE TABLE _techBook(id int, name varchar(255))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingTableConventionMismatch,- Title: "naming.table",- Content: "\"_techbook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 1,- },- },- },- {- // PostgreSQL uses lowercase by default unless using double quotes.- Statement: "CREATE TABLE techBook(id int, name varchar(255))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE tech_book(id int, name varchar(255))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE tech_book RENAME TO \"TechBook\"",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingTableConventionMismatch,- 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",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `CREATE TABLE _techBook(id int, name varchar(255));- ALTER TABLE tech_book RENAME TO "TechBook";`,- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingTableConventionMismatch,- Title: "naming.table",- Content: "\"_techbook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 1,- },- {- Status: advisor.Error,- Code: advisor.NamingTableConventionMismatch,- Title: "naming.table",- Content: "\"TechBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",- Line: 2,- },- },- },- }- payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^[a-z]+(_[a-z]+)*$",- MaxLength: 32,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &NamingTableConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleTableNaming,- Level: advisor.SchemaRuleLevelError,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go b/plugin/advisor/pg/advisor_naming_unique_key_convention_test.godeleted file mode 100644index 020e29e79d..0000000000--- a/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go+++ /dev/null@@ -1,236 +0,0 @@-package pg--import (- "encoding/json"- "fmt"- "strings"- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNamingUKConvention(t *testing.T) {- invalidUKName := advisor.RandomString(42)- maxLength := 32-- tests := []advisor.TestCase{- {- Statement: "CREATE UNIQUE INDEX uk_tech_book_id_name ON tech_book(id, name)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE UNIQUE INDEX tech_book_id_name ON tech_book(id, name)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- 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: fmt.Sprintf("CREATE UNIQUE INDEX %s ON tech_book(id, name)", invalidUKName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: fmt.Sprintf("Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"%s\"", invalidUKName),- Line: 1,- },- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: fmt.Sprintf("Unique key \"%s\" in table \"tech_book\" mismatches the naming convention, its length should be within %d characters", invalidUKName, maxLength),- Line: 1,- },- },- },- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id_name UNIQUE (id, name)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT tech_book_id_name UNIQUE (id, name)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- 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))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: `-- this is the first line.- CREATE TABLE tech_book(- id INT PRIMARY KEY,- name VARCHAR(20),- CONSTRAINT tech_book_name UNIQUE (name)- )`,- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- 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\"",- Line: 5,- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE (name))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20) UNIQUE)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_%s UNIQUE USING INDEX %s",- strings.Join(advisor.MockIndexColumnList, "_"),- advisor.MockOldIndexName,- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book UNIQUE USING INDEX %s",- advisor.MockOldIndexName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book RENAME CONSTRAINT %s TO uk_tech_book_%s",- advisor.MockOldUKName,- strings.Join(advisor.MockIndexColumnList, "_"),- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER TABLE tech_book RENAME CONSTRAINT %s TO uk_tech_book",- advisor.MockOldUKName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf(- "ALTER INDEX %s RENAME TO uk_tech_book_%s",- advisor.MockOldUKName,- strings.Join(advisor.MockIndexColumnList, "_"),- ),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf(- "ALTER INDEX %s RENAME TO uk_tech_book",- advisor.MockOldUKName,- ),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",- Line: 1,- },- },- },- }-- payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^$|^uk_{{table}}_{{column_list}}$",- MaxLength: maxLength,- })- require.NoError(t, err)- advisor.RunSQLReviewRuleTests(t, tests, &NamingUKConventionAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleUKNaming,- Level: advisor.SchemaRuleLevelError,- Payload: string(payload),- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.godeleted file mode 100644index 1d84bcc06c..0000000000--- a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go+++ /dev/null@@ -1,77 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNoLeadingWildcardLike(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "SELECT * FROM t WHERE a LIKE 'abc%'",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "SELECT * FROM t WHERE a LIKE '%abc'",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementLeadingWildcardLike,- Title: "statement.where.no-leading-wildcard-like",- Content: "\"SELECT * FROM t WHERE a LIKE '%abc'\" uses leading wildcard LIKE",- Line: 1,- },- },- },- {- Statement: "SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementLeadingWildcardLike,- Title: "statement.where.no-leading-wildcard-like",- Content: "\"SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'\" uses leading wildcard LIKE",- Line: 1,- },- },- },- {- Statement: "SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementLeadingWildcardLike,- Title: "statement.where.no-leading-wildcard-like",- Content: "\"SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'\" uses leading wildcard LIKE",- Line: 1,- },- },- },- {- Statement: "SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementLeadingWildcardLike,- Title: "statement.where.no-leading-wildcard-like",- Content: "\"SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1\" uses leading wildcard LIKE",- Line: 1,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &NoLeadingWildcardLikeAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleStatementNoLeadingWildcardLike,- Level: advisor.SchemaRuleLevelError,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_stmt_no_select_all_test.go b/plugin/advisor/pg/advisor_stmt_no_select_all_test.godeleted file mode 100644index 4f5b3c4d14..0000000000--- a/plugin/advisor/pg/advisor_stmt_no_select_all_test.go+++ /dev/null@@ -1,65 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestNoSelectAll(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "SELECT * FROM t",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementSelectAll,- Title: "statement.select.no-select-all",- Content: "\"SELECT * FROM t\" uses SELECT all",- Line: 1,- },- },- },- {- Statement: "SELECT a, b FROM t",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "SELECT a, b FROM (SELECT * from t1, t2) t",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementSelectAll,- Title: "statement.select.no-select-all",- Content: "\"SELECT a, b FROM (SELECT * from t1, t2) t\" uses SELECT all",- Line: 1,- },- },- },- {- Statement: "INSERT INTO t SELECT * FROM t1",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.StatementSelectAll,- Title: "statement.select.no-select-all",- Content: "\"INSERT INTO t SELECT * FROM t1\" uses SELECT all",- Line: 1,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &NoSelectAllAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleStatementNoSelectAll,- Level: advisor.SchemaRuleLevelError,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_stmt_where_required_test.go b/plugin/advisor/pg/advisor_stmt_where_required_test.godeleted file mode 100644index b6ab8607d1..0000000000--- a/plugin/advisor/pg/advisor_stmt_where_required_test.go+++ /dev/null@@ -1,110 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestWhereRequirement(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "INSERT INTO t VALUES(1)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "DELETE FROM t1",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.StatementNoWhere,- Title: "statement.where.require",- Content: "\"DELETE FROM t1\" requires WHERE clause",- Line: 1,- },- },- },- {- Statement: "UPDATE t1 SET a = 1",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.StatementNoWhere,- Title: "statement.where.require",- Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",- Line: 1,- },- },- },- {- Statement: "DELETE FROM t1 WHERE a > 0",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "UPDATE t1 SET a = 1 WHERE a > 10",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "SELECT a FROM t",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.StatementNoWhere,- Title: "statement.where.require",- Content: "\"SELECT a FROM t\" requires WHERE clause",- Line: 1,- },- },- },- {- Statement: "SELECT a FROM t WHERE a > 0",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "SELECT a FROM t WHERE a > (SELECT max(id) FROM user)",- Want: []advisor.Advice{- {- Status: advisor.Warn,- Code: advisor.StatementNoWhere,- Title: "statement.where.require",- Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",- Line: 1,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &WhereRequirementAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleStatementRequireWhere,- Level: advisor.SchemaRuleLevelWarning,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_table_no_fk_test.go b/plugin/advisor/pg/advisor_table_no_fk_test.godeleted file mode 100644index d870662f66..0000000000--- a/plugin/advisor/pg/advisor_table_no_fk_test.go+++ /dev/null@@ -1,42 +0,0 @@-package pg--import (- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestTableNoFK(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.TableHasFK,- Title: "table.no-foreign-key",- Content: "Foreign key is not allowed in the table \"public\".\"tech_book\", related statement: \"ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)\"",- Line: 1,- },- },- },- {- Statement: "CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.TableHasFK,- Title: "table.no-foreign-key",- Content: "Foreign key is not allowed in the table \"public\".\"book\", related statement: \"CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))\"",- Line: 1,- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &TableNoFKAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleTableNoFK,- Level: advisor.SchemaRuleLevelError,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/advisor_table_require_pk_test.go b/plugin/advisor/pg/advisor_table_require_pk_test.godeleted file mode 100644index 4be90b434c..0000000000--- a/plugin/advisor/pg/advisor_table_require_pk_test.go+++ /dev/null@@ -1,110 +0,0 @@-package pg--import (- "fmt"- "testing"-- "github.com/bytebase/bytebase/plugin/advisor"-)--func TestRequirePK(t *testing.T) {- tests := []advisor.TestCase{- {- Statement: "CREATE TABLE t(id INT PRIMARY KEY)",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE t(id INT, PRIMARY KEY (id))",- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: "CREATE TABLE t(id INT)",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.TableNoPK,- Title: "table.require-pk",- Content: "Table \"public\".\"t\" requires PRIMARY KEY, related statement: \"CREATE TABLE t(id INT)\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE %q DROP CONSTRAINT %q", advisor.MockTableName, advisor.MockOldPostgreSQLPKName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.TableNoPK,- Title: "table.require-pk",- Content: "Table \"public\".\"tech_book\" requires PRIMARY KEY, related statement: \"ALTER TABLE \\\"tech_book\\\" DROP CONSTRAINT \\\"old_pk\\\"\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE %q DROP CONSTRAINT %q", advisor.MockTableName, advisor.MockOldIndexName),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE %q DROP CONSTRAINT constraint_not_in_catalog", advisor.MockTableName),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE %q DROP COLUMN id", advisor.MockTableName),- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.TableNoPK,- Title: "table.require-pk",- Content: "Table \"public\".\"tech_book\" requires PRIMARY KEY, related statement: \"ALTER TABLE \\\"tech_book\\\" DROP COLUMN id\"",- Line: 1,- },- },- },- {- Statement: fmt.Sprintf("ALTER TABLE %q DROP COLUMN column_not_in_pk", advisor.MockTableName),- Want: []advisor.Advice{- {- Status: advisor.Success,- Code: advisor.Ok,- Title: "OK",- Content: "",- },- },- },- }-- advisor.RunSQLReviewRuleTests(t, tests, &TableRequirePKAdvisor{}, &advisor.SQLReviewRule{- Type: advisor.SchemaRuleTableRequirePK,- Level: advisor.SchemaRuleLevelError,- Payload: "",- }, advisor.MockPostgreSQLDatabase)-}diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.gonew file mode 100644index 0000000000..a1eee73000--- /dev/null+++ b/plugin/advisor/pg/pg_rules_test.go@@ -0,0 +1,37 @@+package pg++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/db"+)++func TestPostgreSQLRules(t *testing.T) {+ pgRules := []advisor.SQLReviewRuleType{+ advisor.SchemaRuleColumnNotNull,+ advisor.SchemaRuleRequiredColumn,+ advisor.SchemaRuleColumnTypeDisallowList,+ advisor.SchemaRuleCommentLength,+ advisor.SchemaRuleIndexKeyNumberLimit,+ advisor.SchemaRuleIndexNoDuplicateColumn,+ advisor.SchemaRuleColumnNaming,+ advisor.SchemaRuleFKNaming,+ advisor.SchemaRuleIDXNaming,+ advisor.SchemaRulePKNaming,+ advisor.SchemaRuleUKNaming,+ advisor.SchemaRuleTableNaming,+ advisor.SchemaRuleSchemaBackwardCompatibility,+ advisor.SchemaRuleStatementInsertRowLimit,+ advisor.SchemaRuleStatementNoSelectAll,+ advisor.SchemaRuleStatementNoLeadingWildcardLike,+ advisor.SchemaRuleStatementRequireWhere,+ advisor.SchemaRuleCharsetAllowlist,+ advisor.SchemaRuleTableNoFK,+ advisor.SchemaRuleTableRequirePK,+ }++ for _, rule := range pgRules {+ advisor.RunSQLReviewRuleTest(t, rule, db.Postgres, false /* record */)+ }+}diff --git a/plugin/advisor/pg/test/column_no_null.yaml b/plugin/advisor/pg/test/column_no_null.yamlnew file mode 100644index 0000000000..7c72fa8fbf--- /dev/null+++ b/plugin/advisor/pg/test/column_no_null.yaml@@ -0,0 +1,79 @@+- statement: |-+ CREATE TABLE book (+ id int,+ name varchar(255)+ )+ want:+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "id" in "public"."book" cannot have NULL value+ line: 2+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "name" in "public"."book" cannot have NULL value+ line: 3+- statement: CREATE TABLE book(id int, name varchar(255), PRIMARY KEY (id))+ want:+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "name" in "public"."book" cannot have NULL value+ line: 1+- statement: CREATE TABLE book(id int PRIMARY KEY, name varchar(255))+ want:+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "name" in "public"."book" cannot have NULL value+ line: 1+- statement: CREATE TABLE book(id int NOT NULL, name varchar(255))+ want:+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "name" in "public"."book" cannot have NULL value+ line: 1+- statement: CREATE TABLE book(id int PRIMARY KEY, name varchar(255) NOT NULL)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE book ADD COLUMN id int+ want:+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "id" in "public"."book" cannot have NULL value+ line: 1+- statement: ALTER TABLE 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+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE book ALTER COLUMN id DROP NOT NULL+ want:+ - status: WARN+ code: 402+ title: column.no-null+ content: Column "id" in "public"."book" cannot have NULL value+ line: 1+- statement: /* this is a comment */+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0diff --git a/plugin/advisor/pg/test/column_required.yaml b/plugin/advisor/pg/test/column_required.yamlnew file mode 100644index 0000000000..489637ea8d--- /dev/null+++ b/plugin/advisor/pg/test/column_required.yaml@@ -0,0 +1,34 @@+- statement: CREATE TABLE book(id int)+ want:+ - status: WARN+ code: 401+ title: column.required+ content: 'Table "book" requires columns: created_ts, creator_id, updated_ts, updater_id'+ line: 1+- statement: |-+ CREATE TABLE book(+ id int,+ creator_id int,+ created_ts timestamp,+ updater_id int,+ updated_ts timestamp)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE 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;+ want:+ - status: WARN+ code: 401+ title: column.required+ content: 'Table "book" requires columns: creator_id'+ line: 1diff --git a/plugin/advisor/pg/test/column_type_disallow_list.yaml b/plugin/advisor/pg/test/column_type_disallow_list.yamlnew file mode 100644index 0000000000..d07eada0c6--- /dev/null+++ b/plugin/advisor/pg/test/column_type_disallow_list.yaml@@ -0,0 +1,23 @@+- statement: CREATE TABLE t(a char(5));+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE t(a int, b JSON);+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type JSON but column "t"."b" is+ line: 1+- statement: |-+ CREATE TABLE t(d char(5));+ ALTER TABLE t add COLUMN a JSON;+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type JSON but column "t"."a" is+ line: 2diff --git a/plugin/advisor/pg/test/comment_length.yaml b/plugin/advisor/pg/test/comment_length.yamlnew file mode 100644index 0000000000..c03ea16727--- /dev/null+++ b/plugin/advisor/pg/test/comment_length.yaml@@ -0,0 +1,28 @@+- statement: comment on column public.name is 'is a column name'+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: comment on column public.name is 'is a to long column name'+ want:+ - status: WARN+ code: 1301+ title: comment.length+ content: The length of comment should be within 20 characters+ line: 1+- statement: comment on table public is 'is a table name'+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: comment on table public is 'is a to long table name long long long long long long long'+ want:+ - status: WARN+ code: 1301+ title: comment.length+ content: The length of comment should be within 20 characters+ line: 1diff --git a/plugin/advisor/pg/test/index_key_number_limit.yaml b/plugin/advisor/pg/test/index_key_number_limit.yamlnew file mode 100644index 0000000000..0f5f61305f--- /dev/null+++ b/plugin/advisor/pg/test/index_key_number_limit.yaml@@ -0,0 +1,56 @@+- statement: CREATE TABLE t(name char(20));+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE t(name varchar(225));+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE t(name char(225), PRIMARY KEY (name));+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE t(id int, name char(225), c1 int, c2 int, c3 int, c4 int, CONSTRAINT t_id_name PRIMARY KEY (id, name, 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+- statement: CREATE TABLE t(id int, name char(225), c1 int, c2 int, c3 int, c4 int, CONSTRAINT t_id_name UNIQUE (id, name, 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+- statement: 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);+ 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);+ 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: 1diff --git a/plugin/advisor/pg/test/index_no_duplicate_column.yaml b/plugin/advisor/pg/test/index_no_duplicate_column.yamlnew file mode 100644index 0000000000..d332203a35--- /dev/null+++ b/plugin/advisor/pg/test/index_no_duplicate_column.yaml@@ -0,0 +1,53 @@+- statement: CREATE TABLE t (a INT, PRIMARY KEY (a));+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ CREATE TABLE t (+ a int,+ PRIMARY KEY (a, a));+ want:+ - status: WARN+ code: 812+ title: index.no-duplicate-column+ content: PRIMARY KEY "" has duplicate column "t"."a"+ line: 3+- statement: |-+ CREATE TABLE t(a int);+ CREATE INDEX idx_a on t(a, a);+ want:+ - status: WARN+ code: 812+ title: index.no-duplicate-column+ content: INDEX "idx_a" has duplicate column "t"."a"+ line: 2+- statement: |-+ CREATE TABLE t(a int);+ ALTER TABLE t ADD CONSTRAINT uk_a UNIQUE (a, a);+ want:+ - status: WARN+ code: 812+ title: index.no-duplicate-column+ content: UNIQUE KEY "uk_a" has duplicate column "t"."a"+ line: 2+- statement: |-+ CREATE TABLE t(a int);+ ALTER TABLE t ADD CONSTRAINT pk_a PRIMARY KEY (a, a);+ want:+ - status: WARN+ code: 812+ title: index.no-duplicate-column+ content: PRIMARY KEY "pk_a" has duplicate column "t"."a"+ line: 2+- statement: |-+ CREATE TABLE t(a int);+ ALTER TABLE t ADD CONSTRAINT fk_a FOREIGN KEY (a, a) REFERENCES t1(a, b);+ want:+ - status: WARN+ code: 812+ title: index.no-duplicate-column+ content: FOREIGN KEY "fk_a" has duplicate column "t"."a"+ line: 2diff --git a/plugin/advisor/pg/test/naming_column.yaml b/plugin/advisor/pg/test/naming_column.yamlnew file mode 100644index 0000000000..0112ae27fe--- /dev/null+++ b/plugin/advisor/pg/test/naming_column.yaml@@ -0,0 +1,64 @@+- statement: CREATE TABLE book(id int, "creatorId" int)+ want:+ - status: WARN+ code: 302+ title: naming.column+ content: '"book"."creatorId" mismatches column naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+- statement: CREATE TABLE book(id int, qavszgslczmsacubtvyymzhojfytkcmon int)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE book(id int, creator_Id int)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE book(id int, creator_id int)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ CREATE TABLE book(id int, creator_id int);+ ALTER TABLE book ADD COLUMN "creatorId" int+ want:+ - status: WARN+ code: 302+ title: naming.column+ content: '"book"."creatorId" mismatches column naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 2+- statement: |-+ CREATE TABLE book(id int, creator_id int);+ ALTER TABLE book ADD COLUMN "creator" int+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ CREATE TABLE book(id int, creator_id int);+ ALTER TABLE book RENAME COLUMN creator_id TO "creatorId"+ want:+ - status: WARN+ code: 302+ title: naming.column+ content: '"book"."creatorId" mismatches column naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 2+- statement: |-+ CREATE TABLE book(id int, creator_id int);+ ALTER TABLE book RENAME COLUMN creator_id TO "creator"+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0diff --git a/plugin/advisor/pg/test/naming_index_fk.yaml b/plugin/advisor/pg/test/naming_index_fk.yamlnew file mode 100644index 0000000000..2789b3702a--- /dev/null+++ b/plugin/advisor/pg/test/naming_index_fk.yaml@@ -0,0 +1,68 @@+- statement: ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT fk_author_id FOREIGN KEY (author_id) REFERENCES author (id)+ want:+ - status: WARN+ code: 305+ title: naming.index.fk+ content: Foreign key in table "tech_book" mismatches the naming convention, expect "^$|^fk_tech_book_author_id_author_id$" but found "fk_author_id"+ line: 1+- statement: ALTER TABLE tech_book ADD CONSTRAINT wlwflloczhmazamdotbibvqrrzyhmvkslvyzpmlmxt FOREIGN KEY (author_id) REFERENCES author (id)+ want:+ - status: WARN+ code: 305+ title: naming.index.fk+ content: Foreign key in table "tech_book" mismatches the naming convention, expect "^$|^fk_tech_book_author_id_author_id$" but found "wlwflloczhmazamdotbibvqrrzyhmvkslvyzpmlmxt"+ line: 1+- statement: ALTER TABLE tech_book ADD COLUMN author_id INT CONSTRAINT fk_tech_book_author_id_author_id REFERENCES author (id)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD COLUMN author_id INT CONSTRAINT fk_author_id REFERENCES author (id)+ want:+ - status: WARN+ code: 305+ title: naming.index.fk+ content: Foreign key in table "tech_book" mismatches the naming convention, expect "^$|^fk_tech_book_author_id_author_id$" but found "fk_author_id"+ line: 1+- statement: CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id FOREIGN KEY (author_id) REFERENCES author (id))+ want:+ - status: WARN+ code: 305+ title: naming.index.fk+ content: Foreign key in table "book" mismatches the naming convention, expect "^$|^fk_book_author_id_author_id$" but found "fk_book_author_id"+ line: 1+- statement: CREATE TABLE book(id INT, author_id INT CONSTRAINT fk_book_author_id_author_id REFERENCES author (id))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ -- this is the first line.+ CREATE TABLE book(+ id INT,+ author_id INT CONSTRAINT fk_book_author_id REFERENCES author (id)+ )+ want:+ - status: WARN+ code: 305+ title: naming.index.fk+ content: Foreign key in table "book" mismatches the naming convention, expect "^$|^fk_book_author_id_author_id$" but found "fk_book_author_id"+ line: 4diff --git a/plugin/advisor/pg/test/naming_index_idx.yaml b/plugin/advisor/pg/test/naming_index_idx.yamlnew file mode 100644index 0000000000..21e22323e4--- /dev/null+++ b/plugin/advisor/pg/test/naming_index_idx.yaml@@ -0,0 +1,35 @@+- statement: CREATE INDEX idx_tech_book_id_name ON tech_book(id, name)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE INDEX tech_book_id_name ON tech_book(id, name)+ want:+ - status: WARN+ code: 303+ title: naming.index.idx+ content: Index in table "tech_book" mismatches the naming convention, expect "^$|^idx_tech_book_id_name$" but found "tech_book_id_name"+ line: 1+- statement: CREATE INDEX wfdtqyetsyoovcvikjlyfukxyjxxxhifl ON tech_book(id, name)+ want:+ - status: WARN+ code: 303+ title: naming.index.idx+ content: Index in table "tech_book" mismatches the naming convention, expect "^$|^idx_tech_book_id_name$" but found "wfdtqyetsyoovcvikjlyfukxyjxxxhifl"+ line: 1+- statement: ALTER INDEX old_index RENAME TO idx_tech_book_id_name+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER INDEX old_index RENAME TO idx_tech_book+ want:+ - status: WARN+ code: 303+ title: naming.index.idx+ content: Index in table "tech_book" mismatches the naming convention, expect "^$|^idx_tech_book_id_name$" but found "idx_tech_book"+ line: 1diff --git a/plugin/advisor/pg/test/naming_index_pk.yaml b/plugin/advisor/pg/test/naming_index_pk.yamlnew file mode 100644index 0000000000..f25a7a7691--- /dev/null+++ b/plugin/advisor/pg/test/naming_index_pk.yaml@@ -0,0 +1,97 @@+- statement: ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id_name PRIMARY KEY (id, name)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT tech_book_id_name PRIMARY KEY (id, name)+ want:+ - status: WARN+ code: 306+ title: naming.index.pk+ content: Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_id_name$" but found "tech_book_id_name"+ line: 1+- statement: ALTER TABLE tech_book ADD CONSTRAINT udmhjtnsaablcmjhqcznfiwtnevcehcvw PRIMARY KEY (id, name)+ want:+ - status: WARN+ code: 306+ 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))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ -- this is the first line.+ CREATE TABLE tech_book(+ id INT,+ name VARCHAR(20),+ CONSTRAINT tech_book_name PRIMARY KEY (name)+ )+ want:+ - 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"+ line: 5+- statement: CREATE TABLE tech_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)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id_name PRIMARY KEY USING INDEX old_index+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book PRIMARY KEY USING INDEX old_index+ want:+ - status: WARN+ code: 306+ title: naming.index.pk+ content: Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_id_name$" but found "pk_tech_book"+ line: 1+- statement: ALTER TABLE tech_book RENAME CONSTRAINT old_pk TO pk_tech_book_id_name+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book RENAME CONSTRAINT old_pk TO pk_tech_book+ want:+ - status: WARN+ code: 306+ title: naming.index.pk+ content: Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_id_name$" but found "pk_tech_book"+ line: 1+- statement: ALTER INDEX old_pk RENAME TO pk_tech_book_id_name+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER INDEX old_pk RENAME TO pk_tech_book+ want:+ - status: WARN+ code: 306+ title: naming.index.pk+ content: Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_id_name$" but found "pk_tech_book"+ line: 1diff --git a/plugin/advisor/pg/test/naming_index_uk.yaml b/plugin/advisor/pg/test/naming_index_uk.yamlnew file mode 100644index 0000000000..4a5ecb3f9c--- /dev/null+++ b/plugin/advisor/pg/test/naming_index_uk.yaml@@ -0,0 +1,111 @@+- statement: CREATE UNIQUE INDEX uk_tech_book_id_name ON tech_book(id, name)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE UNIQUE INDEX tech_book_id_name ON tech_book(id, 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_id_name$" but found "tech_book_id_name"+ line: 1+- statement: CREATE UNIQUE INDEX dzfzqbhnkiiegdhvqjeqoevesfuwcmokrehxlapoqj ON tech_book(id, 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_id_name$" but found "dzfzqbhnkiiegdhvqjeqoevesfuwcmokrehxlapoqj"+ line: 1+- statement: ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id_name UNIQUE (id, name)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT tech_book_id_name UNIQUE (id, 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_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))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ -- this is the first line.+ CREATE TABLE tech_book(+ id INT PRIMARY KEY,+ name VARCHAR(20),+ CONSTRAINT tech_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"+ line: 5+- statement: CREATE TABLE tech_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)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id_name UNIQUE USING INDEX old_index+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book UNIQUE USING INDEX old_index+ want:+ - status: WARN+ code: 304+ title: naming.index.uk+ content: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "uk_tech_book"+ line: 1+- statement: ALTER TABLE tech_book RENAME CONSTRAINT old_uk TO uk_tech_book_id_name+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book RENAME CONSTRAINT old_uk TO uk_tech_book+ want:+ - status: WARN+ code: 304+ title: naming.index.uk+ content: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "uk_tech_book"+ line: 1+- statement: ALTER INDEX old_uk RENAME TO uk_tech_book_id_name+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER INDEX old_uk RENAME TO uk_tech_book+ want:+ - status: WARN+ code: 304+ title: naming.index.uk+ content: Unique key in table "tech_book" mismatches the naming convention, expect "^$|^uk_tech_book_id_name$" but found "uk_tech_book"+ line: 1diff --git a/plugin/advisor/pg/test/naming_table.yaml b/plugin/advisor/pg/test/naming_table.yamlnew file mode 100644index 0000000000..dec2826971--- /dev/null+++ b/plugin/advisor/pg/test/naming_table.yaml@@ -0,0 +1,63 @@+- statement: CREATE TABLE "techBook"(id int, name varchar(255))+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"techBook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+- statement: CREATE TABLE "rlcmidzlevbivwvcntihenpoibtiutqeb"(id int, name varchar(255))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE _techBook(id int, name varchar(255))+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"_techbook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+- statement: CREATE TABLE techBook(id int, name varchar(255))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE tech_book(id int, name varchar(255))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE tech_book RENAME TO "TechBook"+ want:+ - status: WARN+ code: 301+ 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+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: |-+ CREATE TABLE _techBook(id int, name varchar(255));+ ALTER TABLE tech_book RENAME TO "TechBook";+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"_techbook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+ - status: WARN+ code: 301+ title: naming.table+ content: '"TechBook" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 2diff --git a/plugin/advisor/pg/test/schema_backward_compatibility.yaml b/plugin/advisor/pg/test/schema_backward_compatibility.yamlnew file mode 100644index 0000000000..b211555c3d--- /dev/null+++ b/plugin/advisor/pg/test/schema_backward_compatibility.yaml@@ -0,0 +1,7 @@+- statement: ALTER TABLE t1 ALTER COLUMN f1 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'+ line: 1diff --git a/plugin/advisor/pg/test/statement_insert_row_limit.yaml b/plugin/advisor/pg/test/statement_insert_row_limit.yamlnew file mode 100644index 0000000000..a2f5f7242a--- /dev/null+++ b/plugin/advisor/pg/test/statement_insert_row_limit.yaml@@ -0,0 +1,14 @@+- statement: INSERT INTO tech_book values(1, 'a')+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: INSERT INTO tech_book values(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')+ want:+ - status: WARN+ code: 1101+ title: statement.insert.row-limit+ content: The value rows in "INSERT INTO tech_book values(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')" should be no more than 5, but found 6+ line: 1diff --git a/plugin/advisor/pg/test/statement_select_no_select_all.yaml b/plugin/advisor/pg/test/statement_select_no_select_all.yamlnew file mode 100644index 0000000000..04eb9fd903--- /dev/null+++ b/plugin/advisor/pg/test/statement_select_no_select_all.yaml@@ -0,0 +1,28 @@+- statement: SELECT * FROM t+ want:+ - status: WARN+ code: 203+ title: statement.select.no-select-all+ content: '"SELECT * FROM t" uses SELECT all'+ line: 1+- statement: SELECT a, b FROM t+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: SELECT a, b FROM (SELECT * from t1, t2) t+ want:+ - status: WARN+ code: 203+ title: statement.select.no-select-all+ content: '"SELECT a, b FROM (SELECT * from t1, t2) t" uses SELECT all'+ line: 1+- statement: INSERT INTO t SELECT * FROM t1+ want:+ - status: WARN+ code: 203+ title: statement.select.no-select-all+ content: '"INSERT INTO t SELECT * FROM t1" uses SELECT all'+ line: 1diff --git a/plugin/advisor/pg/test/statement_where_no_leading_wildcard_like.yaml b/plugin/advisor/pg/test/statement_where_no_leading_wildcard_like.yamlnew file mode 100644index 0000000000..f4e3424b48--- /dev/null+++ b/plugin/advisor/pg/test/statement_where_no_leading_wildcard_like.yaml@@ -0,0 +1,35 @@+- statement: SELECT * FROM t WHERE a LIKE 'abc%'+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: SELECT * FROM t WHERE a LIKE '%abc'+ want:+ - status: WARN+ code: 204+ title: statement.where.no-leading-wildcard-like+ content: '"SELECT * FROM t WHERE a LIKE ''%abc''" uses leading wildcard LIKE'+ line: 1+- statement: SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'+ want:+ - status: WARN+ code: 204+ title: statement.where.no-leading-wildcard-like+ content: '"SELECT * FROM t WHERE a LIKE ''abc'' OR a LIKE ''%abc''" uses leading wildcard LIKE'+ line: 1+- statement: SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'+ want:+ - status: WARN+ code: 204+ title: statement.where.no-leading-wildcard-like+ content: '"SELECT * FROM t WHERE a LIKE ''%acc'' OR a LIKE ''%abc''" uses leading wildcard LIKE'+ line: 1+- statement: SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1+ want:+ - status: WARN+ code: 204+ title: statement.where.no-leading-wildcard-like+ content: '"SELECT * FROM (SELECT * FROM t WHERE a LIKE ''%acc'' OR a LIKE ''%abc'') t1" uses leading wildcard LIKE'+ line: 1diff --git a/plugin/advisor/pg/test/statement_where_require.yaml b/plugin/advisor/pg/test/statement_where_require.yamlnew file mode 100644index 0000000000..21a9f8868e--- /dev/null+++ b/plugin/advisor/pg/test/statement_where_require.yaml@@ -0,0 +1,56 @@+- statement: INSERT INTO t VALUES(1)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: DELETE FROM t1+ want:+ - status: WARN+ code: 202+ title: statement.where.require+ content: '"DELETE FROM t1" requires WHERE clause'+ line: 1+- statement: UPDATE t1 SET a = 1+ want:+ - status: WARN+ code: 202+ title: statement.where.require+ content: '"UPDATE t1 SET a = 1" requires WHERE clause'+ line: 1+- statement: DELETE FROM t1 WHERE a > 0+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: UPDATE t1 SET a = 1 WHERE a > 10+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: SELECT a FROM t+ want:+ - status: WARN+ code: 202+ title: statement.where.require+ content: '"SELECT a FROM t" requires WHERE clause'+ line: 1+- statement: SELECT a FROM t WHERE a > 0+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: SELECT a FROM t WHERE a > (SELECT max(id) FROM user)+ want:+ - status: WARN+ code: 202+ title: statement.where.require+ content: '"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)" requires WHERE clause'+ line: 1diff --git a/plugin/advisor/pg/test/system_charset_allowlist.yaml b/plugin/advisor/pg/test/system_charset_allowlist.yamlnew file mode 100644index 0000000000..c931246661--- /dev/null+++ b/plugin/advisor/pg/test/system_charset_allowlist.yaml@@ -0,0 +1,28 @@+- statement: CREATE DATABASE test WITH ENCODING 'UTF8'+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE DATABASE test+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE DATABASE test WITH ENCODING 'LATIN1'+ want:+ - status: WARN+ code: 1001+ title: system.charset.allowlist+ content: '"" used disabled encoding ''latin1'''+ line: 0+- statement: /* this is a comment */+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0diff --git a/plugin/advisor/pg/test/table_no_foreign_key.yaml b/plugin/advisor/pg/test/table_no_foreign_key.yamlnew file mode 100644index 0000000000..9ffb550eaf--- /dev/null+++ b/plugin/advisor/pg/test/table_no_foreign_key.yaml@@ -0,0 +1,14 @@+- statement: ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)+ want:+ - status: WARN+ code: 602+ title: table.no-foreign-key+ content: 'Foreign key is not allowed in the table "public"."tech_book", related statement: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)"'+ line: 1+- statement: CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))+ want:+ - status: WARN+ code: 602+ title: table.no-foreign-key+ content: 'Foreign key is not allowed in the table "public"."book", related statement: "CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))"'+ line: 1diff --git a/plugin/advisor/pg/test/table_require_pk.yaml b/plugin/advisor/pg/test/table_require_pk.yamlnew file mode 100644index 0000000000..f9b7f08eae--- /dev/null+++ b/plugin/advisor/pg/test/table_require_pk.yaml@@ -0,0 +1,56 @@+- statement: CREATE TABLE t(id INT PRIMARY KEY)+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE t(id INT, PRIMARY KEY (id))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: CREATE TABLE t(id INT)+ want:+ - status: WARN+ code: 601+ title: table.require-pk+ content: 'Table "public"."t" requires PRIMARY KEY, related statement: "CREATE TABLE t(id INT)"'+ line: 1+- statement: ALTER TABLE "tech_book" DROP CONSTRAINT "old_pk"+ want:+ - status: WARN+ code: 601+ title: table.require-pk+ content: 'Table "public"."tech_book" requires PRIMARY KEY, related statement: "ALTER TABLE \"tech_book\" DROP CONSTRAINT \"old_pk\""'+ line: 1+- statement: ALTER TABLE "tech_book" DROP CONSTRAINT "old_index"+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE "tech_book" DROP CONSTRAINT constraint_not_in_catalog+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+- statement: ALTER TABLE "tech_book" DROP COLUMN id+ want:+ - status: WARN+ code: 601+ 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+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 990abb5902..27be1b9191 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -742,6 +742,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errswitch engine {case db.MySQL, db.TiDB:return MySQLColumnTypeRestriction, nil+ case db.Postgres:+ return PostgreSQLColumnTypeDisallowList, nil}case SchemaRuleColumnDisallowSetCharset:switch engine {@@ -815,11 +817,15 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errswitch engine {case db.MySQL, db.TiDB:return MySQLIndexNoDuplicateColumn, nil+ case db.Postgres:+ return PostgreSQLIndexNoDuplicateColumn, nil}case SchemaRuleIndexKeyNumberLimit:switch engine {case db.MySQL, db.TiDB:return MySQLIndexKeyNumberLimit, nil+ case db.Postgres:+ return PostgreSQLIndexKeyNumberLimit, nil}case SchemaRuleIndexTotalNumberLimit:switch engine {@@ -894,6 +900,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLStatementDMLDryRun, nil}+ case SchemaRuleCommentLength:+ if engine == db.Postgres {+ return PostgreSQLCommentConvention, nil+ }}return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)}diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.goindex 916345d1fd..2b40cb198c 100644--- a/plugin/advisor/utils_for_tests.go+++ b/plugin/advisor/utils_for_tests.go@@ -3,14 +3,18 @@ package advisorimport ("context""database/sql"+ "encoding/json""io""math/rand"+ "os"+ "path/filepath"+ "strings""testing""time""github.com/pkg/errors"- "github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ "gopkg.in/yaml.v3""github.com/bytebase/bytebase/plugin/advisor/catalog""github.com/bytebase/bytebase/plugin/advisor/db"@@ -118,8 +122,84 @@ var (// TestCase is the data struct for test.type TestCase struct {- Statement string- Want []Advice+ Statement string `yaml:"statement"`+ Want []Advice `yaml:"want"`+}++type testCatalog struct {+ finder *catalog.Finder+}++func (c *testCatalog) GetFinder() *catalog.Finder {+ return c.finder+}++// RunSQLReviewRuleTest helps to test the SQL review rule.+func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType db.Type, record bool) {+ var tests []TestCase++ fileName := strings.Map(func(r rune) rune {+ switch r {+ case '.', '-':+ return '_'+ default:+ return r+ }+ }, string(rule))+ filepath := filepath.Join("test", fileName+".yaml")+ yamlFile, err := os.Open(filepath)+ require.NoError(t, err)+ defer yamlFile.Close()++ byteValue, err := io.ReadAll(yamlFile)+ require.NoError(t, err)+ err = yaml.Unmarshal(byteValue, &tests)+ require.NoError(t, err, rule)++ for i, tc := range tests {+ database := MockMySQLDatabase+ if dbType == db.Postgres {+ database = MockPostgreSQLDatabase+ }+ finder := catalog.NewFinder(database, &catalog.FinderContext{CheckIntegrity: true})++ payload, err := SetDefaultSQLReviewRulePayload(rule)+ require.NoError(t, err)++ ruleList := []*SQLReviewRule{+ {+ Type: rule,+ Level: SchemaRuleLevelWarning,+ Payload: string(payload),+ },+ }++ ctx := SQLReviewCheckContext{+ Charset: "",+ Collation: "",+ DbType: dbType,+ Catalog: &testCatalog{finder: finder},+ Driver: nil,+ Context: context.Background(),+ }++ adviceList, err := SQLReviewCheck(tc.Statement, ruleList, ctx)+ require.NoError(t, err)+ if record {+ tests[i].Want = adviceList+ } else {+ require.Equal(t, tc.Want, adviceList, tc.Statement)+ }+ }++ if record {+ err := yamlFile.Close()+ require.NoError(t, err)+ byteValue, err := yaml.Marshal(tests)+ require.NoError(t, err)+ err = os.WriteFile(filepath, byteValue, 0644)+ require.NoError(t, err)+ }}// RunSQLReviewRuleTests helps to test the SQL review rule.@@ -146,7 +226,7 @@ func RunSQLReviewRuleTests(ctx.Catalog = finderadviceList, err := adv.Check(ctx, tc.Statement)require.NoError(t, err)- assert.Equal(t, tc.Want, adviceList, tc.Statement)+ require.Equal(t, tc.Want, adviceList, tc.Statement)}}@@ -265,3 +345,131 @@ func (*MockDriver) Dump(_ context.Context, _ string, _ io.Writer, _ bool) (strinfunc (*MockDriver) Restore(_ context.Context, _ io.Reader) error {return nil}++// SetDefaultSQLReviewRulePayload sets the default payload for this rule.+func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {+ var payload []byte+ var err error+ switch ruleTp {+ case SchemaRuleMySQLEngine,+ SchemaRuleStatementNoSelectAll,+ SchemaRuleStatementRequireWhere,+ SchemaRuleStatementNoLeadingWildcardLike,+ SchemaRuleStatementDisallowCommit,+ SchemaRuleStatementDisallowLimit,+ SchemaRuleStatementDisallowOrderBy,+ SchemaRuleStatementMergeAlterTable,+ SchemaRuleStatementInsertMustSpecifyColumn,+ SchemaRuleStatementInsertDisallowOrderByRand,+ SchemaRuleStatementDMLDryRun,+ SchemaRuleTableRequirePK,+ SchemaRuleTableNoFK,+ SchemaRuleTableDisallowPartition,+ SchemaRuleColumnNotNull,+ SchemaRuleColumnDisallowChangeType,+ SchemaRuleColumnSetDefaultForNotNull,+ SchemaRuleColumnDisallowChange,+ SchemaRuleColumnDisallowChangingOrder,+ SchemaRuleColumnAutoIncrementMustInteger,+ SchemaRuleColumnDisallowSetCharset,+ SchemaRuleColumnAutoIncrementMustUnsigned,+ SchemaRuleCurrentTimeColumnCountLimit,+ SchemaRuleColumnRequireDefault,+ SchemaRuleSchemaBackwardCompatibility,+ SchemaRuleDropEmptyDatabase,+ SchemaRuleIndexNoDuplicateColumn,+ SchemaRuleIndexPKTypeLimit,+ SchemaRuleIndexTypeNoBlob:+ case SchemaRuleTableDropNamingConvention:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "_delete$",+ })+ case SchemaRuleTableNaming:+ fallthrough+ case SchemaRuleColumnNaming:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "^[a-z]+(_[a-z]+)*$",+ MaxLength: 64,+ })+ case SchemaRuleIDXNaming:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "^$|^idx_{{table}}_{{column_list}}$",+ MaxLength: 64,+ })+ case SchemaRulePKNaming:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "^$|^pk_{{table}}_{{column_list}}$",+ MaxLength: 64,+ })+ case SchemaRuleUKNaming:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "^$|^uk_{{table}}_{{column_list}}$",+ MaxLength: 64,+ })+ case SchemaRuleFKNaming:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "^$|^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",+ MaxLength: 64,+ })+ case SchemaRuleAutoIncrementColumnNaming:+ payload, err = json.Marshal(NamingRulePayload{+ Format: "^id$",+ MaxLength: 64,+ })+ case SchemaRuleStatementInsertRowLimit, SchemaRuleStatementAffectedRowLimit:+ payload, err = json.Marshal(NumberTypeRulePayload{+ Number: 5,+ })+ case SchemaRuleTableCommentConvention, SchemaRuleColumnCommentConvention:+ payload, err = json.Marshal(CommentConventionRulePayload{+ Required: true,+ MaxLength: 10,+ })+ case SchemaRuleRequiredColumn:+ payload, err = json.Marshal(StringArrayTypeRulePayload{+ List: []string{+ "id",+ "created_ts",+ "updated_ts",+ "creator_id",+ "updater_id",+ },+ })+ case SchemaRuleColumnTypeDisallowList:+ payload, err = json.Marshal(StringArrayTypeRulePayload{+ List: []string{"JSON"},+ })+ case SchemaRuleColumnMaximumCharacterLength:+ payload, err = json.Marshal(NumberTypeRulePayload{+ Number: 20,+ })+ case SchemaRuleColumnAutoIncrementInitialValue:+ payload, err = json.Marshal(NumberTypeRulePayload{+ Number: 20,+ })+ case SchemaRuleIndexKeyNumberLimit, SchemaRuleIndexTotalNumberLimit:+ payload, err = json.Marshal(NumberTypeRulePayload{+ Number: 5,+ })+ case SchemaRuleCharsetAllowlist:+ payload, err = json.Marshal(StringArrayTypeRulePayload{+ List: []string{"utf8mb4", "UTF8"},+ })+ case SchemaRuleCollationAllowlist:+ payload, err = json.Marshal(StringArrayTypeRulePayload{+ List: []string{"utf8mb4_0900_ai_ci"},+ })+ case SchemaRuleCommentLength:+ payload, err = json.Marshal(CommentConventionRulePayload{+ Required: true,+ MaxLength: 20,+ })+ default:+ return "", errors.Errorf("unknown SQL review type for default payload: %s", ruleTp)+ }++ if err != nil {+ return "", err+ }+ return string(payload), nil+}diff --git a/tests/schema_update_test.go b/tests/schema_update_test.goindex d93417093d..a89182f72a 100644--- a/tests/schema_update_test.go+++ b/tests/schema_update_test.go@@ -1408,9 +1408,7 @@ func TestVCS_SQL_Review(t *testing.T) {Status: advisor.Warn,Content: []string{fmt.Sprintf(- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites name=\"SQL Review\">\n<testsuite name=\"%s\">\n<testcase name=\"naming.index.pk\" classname=\"%s\" file=\"%s#L1\">\n<failure>\nError: Primary key in table \"book\" mismatches the naming convention, expect \"^pk_book_id$\" but found \"\".\nYou can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#306\n</failure>\n</testcase>\n<testcase name=\"column.required\" classname=\"%s\" file=\"%s#L1\">\n<failure>\nError: Table \"book\" requires columns: created_ts, creator_id, updated_ts, updater_id.\nYou can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#401\n</failure>\n</testcase>\n<testcase name=\"column.no-null\" classname=\"%s\" file=\"%s#L1\">\n<failure>\nError: Column \"name\" in \"public\".\"book\" cannot have NULL value.\nYou can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#402\n</failure>\n</testcase>\n</testsuite>\n</testsuites>",- filePath,- filePath,+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites name=\"SQL Review\">\n<testsuite name=\"%s\">\n<testcase name=\"column.required\" classname=\"%s\" file=\"%s#L1\">\n<failure>\nError: Table \"book\" requires columns: created_ts, creator_id, updated_ts, updater_id.\nYou can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#401\n</failure>\n</testcase>\n<testcase name=\"column.no-null\" classname=\"%s\" file=\"%s#L1\">\n<failure>\nError: Column \"name\" in \"public\".\"book\" cannot have NULL value.\nYou can check the docs at https://www.bytebase.com/docs/reference/error-code/advisor#402\n</failure>\n</testcase>\n</testsuite>\n</testsuites>",filePath,filePath,filePath,@@ -1443,10 +1441,6 @@ func TestVCS_SQL_Review(t *testing.T) {return &api.VCSSQLReviewResult{Status: advisor.Warn,Content: []string{- fmt.Sprintf(- "::warning file=%s,line=1,col=1,endColumn=2,title=naming.index.pk (306)::Primary key in table \"book\" mismatches the naming convention, expect \"^pk_book_id$\" but found \"\"%%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#306",- filePath,- ),fmt.Sprintf("::warning file=%s,line=1,col=1,endColumn=2,title=column.required (401)::Table \"book\" requires columns: created_ts, creator_id, updated_ts, updater_id%%0ADoc: https://www.bytebase.com/docs/reference/error-code/advisor#401",filePath,diff --git a/tests/sql_review_test.go b/tests/sql_review_test.goindex e9909ee615..4ffa72d5aa 100644--- a/tests/sql_review_test.go+++ b/tests/sql_review_test.go@@ -111,14 +111,14 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {Namespace: api.AdvisorNamespace,Code: advisor.NamingUKConventionMismatch.Int(),Title: "naming.index.uk",- Content: `Unique key in table "userTable" mismatches the naming convention, expect "^uk_userTable_id_name$" but found "uk1"`,+ Content: `Unique key in table "userTable" mismatches the naming convention, expect "^$|^uk_userTable_id_name$" but found "uk1"`,},{Status: api.TaskCheckStatusWarn,Namespace: api.AdvisorNamespace,Code: advisor.NamingFKConventionMismatch.Int(),Title: "naming.index.fk",- Content: `Foreign key in table "userTable" mismatches the naming convention, expect "^fk_userTable_roomId_room_id$" but found "fk1"`,+ Content: `Foreign key in table "userTable" mismatches the naming convention, expect "^$|^fk_userTable_roomId_room_id$" but found "fk1"`,},{Status: api.TaskCheckStatusError,@@ -242,7 +242,7 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {Namespace: api.AdvisorNamespace,Code: advisor.NamingPKConventionMismatch.Int(),Title: "naming.index.pk",- Content: `Primary key in table "tech_book" mismatches the naming convention, expect "^pk_tech_book_id$" but found "pk1"`,+ Content: `Primary key in table "tech_book" mismatches the naming convention, expect "^$|^pk_tech_book_id$" but found "pk1"`,},},},@@ -472,21 +472,21 @@ func TestSQLReviewForMySQL(t *testing.T) {Namespace: api.AdvisorNamespace,Code: advisor.NamingIndexConventionMismatch.Int(),Title: "naming.index.idx",- Content: "Index in table `userTable` mismatches the naming convention, expect \"^idx_userTable_name$\" but found `idx1`",+ Content: "Index in table `userTable` mismatches the naming convention, expect \"^$|^idx_userTable_name$\" but found `idx1`",},{Status: api.TaskCheckStatusWarn,Namespace: api.AdvisorNamespace,Code: advisor.NamingUKConventionMismatch.Int(),Title: "naming.index.uk",- Content: "Unique key in table `userTable` mismatches the naming convention, expect \"^uk_userTable_id_name$\" but found `uk1`",+ Content: "Unique key in table `userTable` mismatches the naming convention, expect \"^$|^uk_userTable_id_name$\" but found `uk1`",},{Status: api.TaskCheckStatusWarn,Namespace: api.AdvisorNamespace,Code: advisor.NamingFKConventionMismatch.Int(),Title: "naming.index.fk",- Content: "Foreign key in table `userTable` mismatches the naming convention, expect \"^fk_userTable_roomId_room_id$\" but found `fk1`",+ Content: "Foreign key in table `userTable` mismatches the naming convention, expect \"^$|^fk_userTable_roomId_room_id$\" but found `fk1`",},{Status: api.TaskCheckStatusError,diff --git a/tests/tests.go b/tests/tests.goindex c0f1373a0d..b5935cd01e 100644--- a/tests/tests.go+++ b/tests/tests.go@@ -1948,129 +1948,6 @@ func (ctl *controller) GetSQLReviewResult(id int) ([]api.TaskCheckResult, error)return nil, nil}-// setDefaultSQLReviewRulePayload sets the default payload for this rule.-func setDefaultSQLReviewRulePayload(ruleTp advisor.SQLReviewRuleType) (string, error) {- var payload []byte- var err error- switch ruleTp {- case advisor.SchemaRuleMySQLEngine,- advisor.SchemaRuleStatementNoSelectAll,- advisor.SchemaRuleStatementRequireWhere,- advisor.SchemaRuleStatementNoLeadingWildcardLike,- advisor.SchemaRuleStatementDisallowCommit,- advisor.SchemaRuleStatementDisallowLimit,- advisor.SchemaRuleStatementDisallowOrderBy,- advisor.SchemaRuleStatementMergeAlterTable,- advisor.SchemaRuleStatementInsertMustSpecifyColumn,- advisor.SchemaRuleStatementInsertDisallowOrderByRand,- advisor.SchemaRuleStatementDMLDryRun,- advisor.SchemaRuleTableRequirePK,- advisor.SchemaRuleTableNoFK,- advisor.SchemaRuleTableDisallowPartition,- advisor.SchemaRuleColumnNotNull,- advisor.SchemaRuleColumnDisallowChangeType,- advisor.SchemaRuleColumnSetDefaultForNotNull,- advisor.SchemaRuleColumnDisallowChange,- advisor.SchemaRuleColumnDisallowChangingOrder,- advisor.SchemaRuleColumnAutoIncrementMustInteger,- advisor.SchemaRuleColumnDisallowSetCharset,- advisor.SchemaRuleColumnAutoIncrementMustUnsigned,- advisor.SchemaRuleCurrentTimeColumnCountLimit,- advisor.SchemaRuleColumnRequireDefault,- advisor.SchemaRuleSchemaBackwardCompatibility,- advisor.SchemaRuleDropEmptyDatabase,- advisor.SchemaRuleIndexNoDuplicateColumn,- advisor.SchemaRuleIndexPKTypeLimit,- advisor.SchemaRuleIndexTypeNoBlob:- case advisor.SchemaRuleTableDropNamingConvention:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "_delete$",- })- case advisor.SchemaRuleTableNaming:- fallthrough- case advisor.SchemaRuleColumnNaming:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "^[a-z]+(_[a-z]+)*$",- MaxLength: 64,- })- case advisor.SchemaRuleIDXNaming:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "^idx_{{table}}_{{column_list}}$",- MaxLength: 64,- })- case advisor.SchemaRulePKNaming:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "^pk_{{table}}_{{column_list}}$",- MaxLength: 64,- })- case advisor.SchemaRuleUKNaming:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "^uk_{{table}}_{{column_list}}$",- MaxLength: 64,- })- case advisor.SchemaRuleFKNaming:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",- MaxLength: 64,- })- case advisor.SchemaRuleAutoIncrementColumnNaming:- payload, err = json.Marshal(advisor.NamingRulePayload{- Format: "^id$",- MaxLength: 64,- })- case advisor.SchemaRuleStatementInsertRowLimit, advisor.SchemaRuleStatementAffectedRowLimit:- payload, err = json.Marshal(advisor.NumberTypeRulePayload{- Number: 5,- })- case advisor.SchemaRuleTableCommentConvention, advisor.SchemaRuleColumnCommentConvention:- payload, err = json.Marshal(advisor.CommentConventionRulePayload{- Required: true,- MaxLength: 10,- })- case advisor.SchemaRuleRequiredColumn:- payload, err = json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{- "id",- "created_ts",- "updated_ts",- "creator_id",- "updater_id",- },- })- case advisor.SchemaRuleColumnTypeDisallowList:- payload, err = json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{"JSON"},- })- case advisor.SchemaRuleColumnMaximumCharacterLength:- payload, err = json.Marshal(advisor.NumberTypeRulePayload{- Number: 20,- })- case advisor.SchemaRuleColumnAutoIncrementInitialValue:- payload, err = json.Marshal(advisor.NumberTypeRulePayload{- Number: 20,- })- case advisor.SchemaRuleIndexKeyNumberLimit, advisor.SchemaRuleIndexTotalNumberLimit:- payload, err = json.Marshal(advisor.NumberTypeRulePayload{- Number: 5,- })- case advisor.SchemaRuleCharsetAllowlist:- payload, err = json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{"utf8mb4"},- })- case advisor.SchemaRuleCollationAllowlist:- payload, err = json.Marshal(advisor.StringArrayTypeRulePayload{- List: []string{"utf8mb4_0900_ai_ci"},- })- default:- return "", errors.Errorf("unknown SQL review type for default payload: %s", ruleTp)- }-- if err != nil {- return "", err- }- return string(payload), nil-}-// prodTemplateSQLReviewPolicy returns the default SQL review policy.func prodTemplateSQLReviewPolicy() (string, error) {policy := advisor.SQLReviewPolicy{@@ -2285,7 +2162,7 @@ func prodTemplateSQLReviewPolicy() (string, error) {}for _, rule := range policy.RuleList {- payload, err := setDefaultSQLReviewRulePayload(rule.Type)+ payload, err := advisor.SetDefaultSQLReviewRulePayload(rule.Type)if err != nil {return "", err}