Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(advisor): add PostgreSQL disallow-change-column-type rule (#3898)
rebelice37e6b160624d
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 1a9361a729..db922c5e91 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -262,6 +262,9 @@ const (// PostgreSQLColumnTypeDisallowList is an advisor type for Postgresql column type disallow list.PostgreSQLColumnTypeDisallowList Type = "bb.plugin.advisor.postgresql.column.type-disallow-list"++ // PostgreSQLColumnDisallowChangingType is an advisor type for PostgreSQL disallow changing column type.+ PostgreSQLColumnDisallowChangingType Type = "bb.plugin.advisor.postgresql.column.disallow-changing-type")// Advice is the result of an advisor.diff --git a/plugin/advisor/pg/advisor_column_disallow_changing_type.go b/plugin/advisor/pg/advisor_column_disallow_changing_type.gonew file mode 100644index 0000000000..95a57e762e--- /dev/null+++ b/plugin/advisor/pg/advisor_column_disallow_changing_type.go@@ -0,0 +1,85 @@+package pg++// Framework code is generated by the generator.++import (+ "fmt"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/db"+ "github.com/bytebase/bytebase/plugin/parser/ast"+)++var (+ _ advisor.Advisor = (*ColumnDisallowChangingTypeAdvisor)(nil)+ _ ast.Visitor = (*columnDisallowChangingTypeChecker)(nil)+)++func init() {+ advisor.Register(db.Postgres, advisor.PostgreSQLColumnDisallowChangingType, &ColumnDisallowChangingTypeAdvisor{})+}++// ColumnDisallowChangingTypeAdvisor is the advisor checking for disallow changing column type.+type ColumnDisallowChangingTypeAdvisor struct {+}++// Check checks for disallow changing column type.+func (*ColumnDisallowChangingTypeAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ stmtList, errAdvice := parseStatement(statement)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &columnDisallowChangingTypeChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range stmtList {+ checker.text = stmt.Text()+ checker.line = stmt.LastLine()+ 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 columnDisallowChangingTypeChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ text string+ line int+ title string+}++// Visit implements ast.Visitor interface.+func (checker *columnDisallowChangingTypeChecker) Visit(in ast.Node) ast.Visitor {+ code := advisor.Ok+ if _, ok := in.(*ast.AlterColumnTypeStmt); ok {+ code = advisor.ChangeColumnType+ }++ if code != advisor.Ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: code,+ Title: checker.title,+ Content: fmt.Sprintf("The statement \"%s\" changes column type", checker.text),+ Line: checker.line,+ })+ }++ return checker+}diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.goindex a1eee73000..c40d675795 100644--- a/plugin/advisor/pg/pg_rules_test.go+++ b/plugin/advisor/pg/pg_rules_test.go@@ -29,6 +29,7 @@ func TestPostgreSQLRules(t *testing.T) {advisor.SchemaRuleCharsetAllowlist,advisor.SchemaRuleTableNoFK,advisor.SchemaRuleTableRequirePK,+ advisor.SchemaRuleColumnDisallowChangeType,}for _, rule := range pgRules {diff --git a/plugin/advisor/pg/test/column_disallow_change_type.yaml b/plugin/advisor/pg/test/column_disallow_change_type.yamlnew file mode 100644index 0000000000..8f5b7e5273--- /dev/null+++ b/plugin/advisor/pg/test/column_disallow_change_type.yaml@@ -0,0 +1,14 @@+- statement: ALTER TABLE tech_book ALTER COLUMN id SET DATA TYPE INTEGER+ want:+ - status: WARN+ code: 403+ title: column.disallow-change-type+ content: The statement "ALTER TABLE tech_book ALTER COLUMN id SET DATA TYPE INTEGER" changes column type+ line: 1+- statement: ALTER TABLE tech_book ADD COLUMN c1 int+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 27be1b9191..6babdb49f1 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -712,6 +712,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errswitch engine {case db.MySQL, db.TiDB:return MySQLColumnDisallowChangingType, nil+ case db.Postgres:+ return PostgreSQLColumnDisallowChangingType, nil}case SchemaRuleColumnSetDefaultForNotNull:switch engine {