Skip to content

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

feat: support PG migration compatibility rule (#2090)

rebelice2013eb68375c
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index d57880a00a..c2a20a66a5 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -131,6 +131,9 @@ const (
// PostgreSQLNoSelectAll is an advisor type for PostgreSQL no select all.
PostgreSQLNoSelectAll Type = "bb.plugin.advisor.postgresql.select.no-select-all"
+
+ // PostgreSQLMigrationCompatibility is an advisor type for PostgreSQL migration compatibility.
+ PostgreSQLMigrationCompatibility Type = "bb.plugin.advisor.postgresql.migration-compatibility"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_migration_compatibility.go b/plugin/advisor/pg/advisor_migration_compatibility.go
new file mode 100644
index 0000000000..2e3ecba695
--- /dev/null
+++ b/plugin/advisor/pg/advisor_migration_compatibility.go
@@ -0,0 +1,117 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*CompatibilityAdvisor)(nil)
+ _ ast.Visitor = (*compatibilityChecker)(nil)
+)
+
+func init() {
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLMigrationCompatibility, &CompatibilityAdvisor{})
+}
+
+// CompatibilityAdvisor is the advisor checking for schema backward compatibility.
+type CompatibilityAdvisor struct {
+}
+
+// Check checks schema backward compatibility.
+func (*CompatibilityAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmts, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &compatibilityChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+ for _, stmt := range stmts {
+ checker.text = stmt.Text()
+ ast.Walk(checker, stmt)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type compatibilityChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+}
+
+func (checker *compatibilityChecker) Visit(node ast.Node) ast.Visitor {
+ code := advisor.Ok
+ switch n := node.(type) {
+ // DROP DATABASE
+ case *ast.DropDatabaseStmt:
+ code = advisor.CompatibilityDropDatabase
+ // RENAME TABLE/VIEW
+ case *ast.RenameTableStmt:
+ code = advisor.CompatibilityRenameTable
+ // DROP TABLE/VIEW
+ case *ast.DropTableStmt:
+ code = advisor.CompatibilityDropTable
+ // ALTER TABLE RENAME COLUMN
+ case *ast.RenameColumnStmt:
+ code = advisor.CompatibilityRenameColumn
+ // ALTER TABLE DROP COLUMN
+ case *ast.DropColumnStmt:
+ code = advisor.CompatibilityDropColumn
+ case *ast.AddConstraintStmt:
+ switch n.Constraint.Type {
+ // ADD PRIMARY KEY/ ADD PRIMARY KEY USING INDEX
+ case ast.ConstraintTypePrimary, ast.ConstraintTypePrimaryUsingIndex:
+ code = advisor.CompatibilityAddPrimaryKey
+ // ADD UNIQUE CONSTRAINT
+ case ast.ConstraintTypeUnique:
+ // ConstraintTypeUniqueUsingIndex doesn't add a new constraint or unique index.
+ code = advisor.CompatibilityAddUniqueKey
+ // ADD FOREIGIN KEY
+ case ast.ConstraintTypeForeign:
+ code = advisor.CompatibilityAddForeignKey
+ // ADD CHECK
+ case ast.ConstraintTypeCheck:
+ if !n.Constraint.SkipValidation {
+ code = advisor.CompatibilityAddCheck
+ }
+ }
+ // ALTER TABLE ALTER COLUMN TYPE
+ case *ast.AlterColumnTypeStmt:
+ code = advisor.CompatibilityAlterColumn
+ // CREATE UNIQUE INDEX
+ case *ast.CreateIndexStmt:
+ if n.Index.Unique {
+ code = advisor.CompatibilityAddUniqueKey
+ }
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: code,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", checker.text),
+ })
+ }
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_migration_compatibility_test.go b/plugin/advisor/pg/advisor_migration_compatibility_test.go
new file mode 100644
index 0000000000..369cf848f6
--- /dev/null
+++ b/plugin/advisor/pg/advisor_migration_compatibility_test.go
@@ -0,0 +1,243 @@
+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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ {
+ Status: advisor.Warn,
+ Code: advisor.CompatibilityDropTable,
+ Title: "schema.backward-compatibility",
+ Content: "\"DROP TABLE t2;\" may cause incompatibility with the existing data and code",
+ },
+ },
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ {
+ 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",
+ },
+ },
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(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",
+ },
+ },
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleSchemaBackwardCompatibility,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockPostgreSQLDatabase)
+}