Skip to content

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

feat(advisor): implement PostgreSQL create constraint not valid (#4347)

rebelice577cb6c78f19
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 08decf9e5a..b2490084ae 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -302,6 +302,9 @@ const (
// PostgreSQLMergeAlterTable is an advisor type for PostgreSQL no redundant ALTER TABLE statements.
PostgreSQLMergeAlterTable Type = "bb.plugin.advisor.postgresql.statement.merge-alter-table"
+ // PostgreSQLAddCheckNotValid is an advisor type for PostgreSQL to add check not valid.
+ PostgreSQLAddCheckNotValid Type = "bb.plugin.advisor.postgresql.statement.add-check-not-valid"
+
// 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"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 76cb5dd2a7..5221c06626 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -34,6 +34,7 @@ const (
StatementDMLDryRunFailed Code = 208
StatementAffectedRowExceedsLimit Code = 209
StatementAddColumnWithDefault Code = 210
+ StatementAddCheckWithValidation Code = 211
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/plugin/advisor/pg/advisor_statement_add_check_not_valid.go b/plugin/advisor/pg/advisor_statement_add_check_not_valid.go
new file mode 100644
index 0000000000..4438a3fd78
--- /dev/null
+++ b/plugin/advisor/pg/advisor_statement_add_check_not_valid.go
@@ -0,0 +1,78 @@
+package pg
+
+// Framework code is generated by the generator.
+
+import (
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*StatementAddCheckNotValidAdvisor)(nil)
+ _ ast.Visitor = (*statementAddCheckNotValidChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLAddCheckNotValid, &StatementAddCheckNotValidAdvisor{})
+}
+
+// StatementAddCheckNotValidAdvisor is the advisor checking for to add check not valid.
+type StatementAddCheckNotValidAdvisor struct {
+}
+
+// Check checks for to add check not valid.
+func (*StatementAddCheckNotValidAdvisor) 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 := &statementAddCheckNotValidChecker{
+ 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 statementAddCheckNotValidChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ line int
+}
+
+// Visit implements ast.Visitor interface.
+func (checker *statementAddCheckNotValidChecker) Visit(in ast.Node) ast.Visitor {
+ if node, ok := in.(*ast.AddConstraintStmt); ok {
+ if node.Constraint.Type == ast.ConstraintTypeCheck && !node.Constraint.SkipValidation {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementAddCheckWithValidation,
+ Title: checker.title,
+ Content: "Adding check constraints with validation will block reads and writes. You can add check constraints not valid and then validate separately",
+ Line: checker.line,
+ })
+ }
+ }
+
+ return checker
+}
diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.go
index b87677d432..d8188f447e 100644
--- a/plugin/advisor/pg/pg_rules_test.go
+++ b/plugin/advisor/pg/pg_rules_test.go
@@ -45,6 +45,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleColumnRequireDefault,
advisor.SchemaRuleStatementDisallowAddColumnWithDefault,
advisor.SchemaRuleCreateIndexConcurrently,
+ advisor.SchemaRuleStatementAddCheckNotValid,
}
for _, rule := range pgRules {
diff --git a/plugin/advisor/pg/test/statement_add_check_not_valid.yaml b/plugin/advisor/pg/test/statement_add_check_not_valid.yaml
new file mode 100644
index 0000000000..a96cae2f92
--- /dev/null
+++ b/plugin/advisor/pg/test/statement_add_check_not_valid.yaml
@@ -0,0 +1,14 @@
+- statement: alter table tech_book add constraint check_id check(id > 0);
+ want:
+ - status: WARN
+ code: 211
+ title: statement.add-check-not-valid
+ content: Adding check constraints with validation will block reads and writes. You can add check constraints not valid and then validate separately
+ line: 1
+- statement: alter table tech_book add constraint check_id check(id > 0) NOT VALID;
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index f707649769..11b1045c5a 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -77,6 +77,8 @@ const (
SchemaRuleStatementDMLDryRun SQLReviewRuleType = "statement.dml-dry-run"
// SchemaRuleStatementDisallowAddColumnWithDefault disallow to add column with DEFAULT.
SchemaRuleStatementDisallowAddColumnWithDefault = "statement.disallow-add-column-with-default"
+ // SchemaRuleStatementAddCheckNotValid require add check constraints not valid.
+ SchemaRuleStatementAddCheckNotValid = "statement.add-check-not-valid"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -962,6 +964,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
if engine == db.Postgres {
return PostgreSQLDisallowAddColumnWithDefault, nil
}
+ case SchemaRuleStatementAddCheckNotValid:
+ if engine == db.Postgres {
+ return PostgreSQLAddCheckNotValid, nil
+ }
case SchemaRuleCommentLength:
if engine == db.Postgres {
return PostgreSQLCommentConvention, nil
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index 54f1f0476f..b582c1eead 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -382,6 +382,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {
SchemaRuleIndexPKTypeLimit,
SchemaRuleStatementDisallowAddColumnWithDefault,
SchemaRuleCreateIndexConcurrently,
+ SchemaRuleStatementAddCheckNotValid,
SchemaRuleIndexTypeNoBlob:
case SchemaRuleTableDropNamingConvention:
payload, err = json.Marshal(NamingRulePayload{