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): implement PostgreSQL disallow-add-not-null rule (#4349)
rebelicec45e6768de0a
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex b2490084ae..e7d1b874ac 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -308,6 +308,9 @@ const (// PostgreSQLDisallowAddColumnWithDefault is an advisor type for PostgreSQL to disallow add column with default.PostgreSQLDisallowAddColumnWithDefault Type = "bb.plugin.advisor.postgresql.statement.disallow-add-column-with-default"+ // PostgreSQLDisallowAddNotNull is an advisor type for PostgreSQl to disallow add not null.+ PostgreSQLDisallowAddNotNull Type = "bb.plugin.advisor.postgresql.statement.disallow-add-not-null"+// PostgreSQLTableDropNamingConvention is an advisor type for PostgreSQL table drop with naming convention.PostgreSQLTableDropNamingConvention Type = "bb.plugin.advisor.postgresql.table.drop-naming-convention"diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex 5221c06626..0194831839 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -35,6 +35,7 @@ const (StatementAffectedRowExceedsLimit Code = 209StatementAddColumnWithDefault Code = 210StatementAddCheckWithValidation Code = 211+ StatementAddNotNull Code = 212// 301 ~ 399 naming error code// 301 table naming advisor error code.diff --git a/plugin/advisor/pg/advisor_statement_disallow_add_not_null.go b/plugin/advisor/pg/advisor_statement_disallow_add_not_null.gonew file mode 100644index 0000000000..ec78af9fd6--- /dev/null+++ b/plugin/advisor/pg/advisor_statement_disallow_add_not_null.go@@ -0,0 +1,78 @@+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 = (*StatementDisallowAddNotNullAdvisor)(nil)+ _ ast.Visitor = (*statementDisallowAddNotNullChecker)(nil)+)++func init() {+ advisor.Register(db.Postgres, advisor.PostgreSQLDisallowAddNotNull, &StatementDisallowAddNotNullAdvisor{})+}++// StatementDisallowAddNotNullAdvisor is the advisor checking for to disallow add not null.+type StatementDisallowAddNotNullAdvisor struct {+}++// Check checks for to disallow add not null.+func (*StatementDisallowAddNotNullAdvisor) 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 := &statementDisallowAddNotNullChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range stmtList {+ 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 statementDisallowAddNotNullChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ line int+}++// Visit implements ast.Visitor interface.+func (checker *statementDisallowAddNotNullChecker) Visit(in ast.Node) ast.Visitor {+ if node, ok := in.(*ast.SetNotNullStmt); ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.StatementAddNotNull,+ Title: checker.title,+ Content: fmt.Sprintf("Setting NOT NULL will block reads and writes. You can use CHECK (%q IS NOT NULL) instead", node.ColumnName),+ Line: checker.line,+ })+ }++ return checker+}diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.goindex d8188f447e..a78eeff803 100644--- a/plugin/advisor/pg/pg_rules_test.go+++ b/plugin/advisor/pg/pg_rules_test.go@@ -46,6 +46,7 @@ func TestPostgreSQLRules(t *testing.T) {advisor.SchemaRuleStatementDisallowAddColumnWithDefault,advisor.SchemaRuleCreateIndexConcurrently,advisor.SchemaRuleStatementAddCheckNotValid,+ advisor.SchemaRuleStatementDisallowAddNotNull,}for _, rule := range pgRules {diff --git a/plugin/advisor/pg/test/statement_disallow_add_not_null.yaml b/plugin/advisor/pg/test/statement_disallow_add_not_null.yamlnew file mode 100644index 0000000000..ba7ec5010a--- /dev/null+++ b/plugin/advisor/pg/test/statement_disallow_add_not_null.yaml@@ -0,0 +1,16 @@+- statement: alter table tech_book alter column name set not null;+ want:+ - status: WARN+ code: 212+ title: statement.disallow-add-not-null+ content: Setting NOT NULL will block reads and writes. You can use CHECK ("name" IS NOT NULL) instead+ line: 1+- statement: |-+ alter table tech_book add constraint check_name_not_null check(name IS NOT NULL) NOT VALID;+ alter table tech_book validate constraint check_name_not_null;+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 11b1045c5a..84d13b09a8 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -79,6 +79,8 @@ const (SchemaRuleStatementDisallowAddColumnWithDefault = "statement.disallow-add-column-with-default"// SchemaRuleStatementAddCheckNotValid require add check constraints not valid.SchemaRuleStatementAddCheckNotValid = "statement.add-check-not-valid"+ // SchemaRuleStatementDisallowAddNotNull disallow to add NOT NULL.+ SchemaRuleStatementDisallowAddNotNull = "statement.disallow-add-not-null"// SchemaRuleTableRequirePK require the table to have a primary key.SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"@@ -968,6 +970,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errif engine == db.Postgres {return PostgreSQLAddCheckNotValid, nil}+ case SchemaRuleStatementDisallowAddNotNull:+ if engine == db.Postgres {+ return PostgreSQLDisallowAddNotNull, nil+ }case SchemaRuleCommentLength:if engine == db.Postgres {return PostgreSQLCommentConvention, nildiff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.goindex b582c1eead..f9d97e2dfc 100644--- a/plugin/advisor/utils_for_tests.go+++ b/plugin/advisor/utils_for_tests.go@@ -383,6 +383,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {SchemaRuleStatementDisallowAddColumnWithDefault,SchemaRuleCreateIndexConcurrently,SchemaRuleStatementAddCheckNotValid,+ SchemaRuleStatementDisallowAddNotNull,SchemaRuleIndexTypeNoBlob:case SchemaRuleTableDropNamingConvention:payload, err = json.Marshal(NamingRulePayload{