Skip to content

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

feat(advisor): implement PostgreSQL disallow-add-column-with-default (#4345)

rebelicef6a024bf3f23
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index a8951b9ffd..f363c4983e 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -299,6 +299,9 @@ const (
// PostgreSQLMergeAlterTable is an advisor type for PostgreSQL no redundant ALTER TABLE statements.
PostgreSQLMergeAlterTable Type = "bb.plugin.advisor.postgresql.statement.merge-alter-table"
+ // 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"
+
// 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.go
index bcc8158cf8..47a11488a0 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -33,6 +33,7 @@ const (
StatementRedundantAlterTable Code = 207
StatementDMLDryRunFailed Code = 208
StatementAffectedRowExceedsLimit Code = 209
+ StatementAddColumnWithDefault Code = 210
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/plugin/advisor/pg/advisor_statement_disallow_add_column_with_default.go b/plugin/advisor/pg/advisor_statement_disallow_add_column_with_default.go
new file mode 100644
index 0000000000..b51b18cbd8
--- /dev/null
+++ b/plugin/advisor/pg/advisor_statement_disallow_add_column_with_default.go
@@ -0,0 +1,89 @@
+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 = (*StatementDisallowAddColumnWithDefaultAdvisor)(nil)
+ _ ast.Visitor = (*statementDisallowAddColumnWithDefaultChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLDisallowAddColumnWithDefault, &StatementDisallowAddColumnWithDefaultAdvisor{})
+}
+
+// StatementDisallowAddColumnWithDefaultAdvisor is the advisor checking for to disallow add column with default.
+type StatementDisallowAddColumnWithDefaultAdvisor struct {
+}
+
+// Check checks for to disallow add column with default.
+func (*StatementDisallowAddColumnWithDefaultAdvisor) 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 := &statementDisallowAddColumnWithDefaultChecker{
+ 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 statementDisallowAddColumnWithDefaultChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ line int
+}
+
+// Visit implements ast.Visitor interface.
+func (checker *statementDisallowAddColumnWithDefaultChecker) Visit(in ast.Node) ast.Visitor {
+ if node, ok := in.(*ast.AddColumnListStmt); ok {
+ for _, column := range node.ColumnList {
+ if setDefault(column) {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementAddColumnWithDefault,
+ Title: checker.title,
+ Content: "Adding column with DEFAULT will locked the whole table and rewriting each rows",
+ Line: checker.line,
+ })
+ }
+ }
+ }
+ return checker
+}
+
+func setDefault(column *ast.ColumnDef) bool {
+ for _, constraint := range column.ConstraintList {
+ if constraint.Type == ast.ConstraintTypeDefault {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.go
index 622301c21d..071711ab15 100644
--- a/plugin/advisor/pg/pg_rules_test.go
+++ b/plugin/advisor/pg/pg_rules_test.go
@@ -43,6 +43,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleStatementAffectedRowLimit,
advisor.SchemaRuleStatementMergeAlterTable,
advisor.SchemaRuleColumnRequireDefault,
+ advisor.SchemaRuleStatementDisallowAddColumnWithDefault,
}
for _, rule := range pgRules {
diff --git a/plugin/advisor/pg/test/statement_disallow_add_column_with_default.yaml b/plugin/advisor/pg/test/statement_disallow_add_column_with_default.yaml
new file mode 100644
index 0000000000..a7fa120a35
--- /dev/null
+++ b/plugin/advisor/pg/test/statement_disallow_add_column_with_default.yaml
@@ -0,0 +1,14 @@
+- statement: ALTER TABLE tech_book ADD COLUMN c int;
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: ALTER TABLE tech_book ADD COLUMN c int DEFAULT 0;
+ want:
+ - status: WARN
+ code: 210
+ title: statement.disallow-add-column-with-default
+ content: Adding column with DEFAULT will locked the whole table and rewriting each rows
+ line: 1
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 2fc431aadb..ec8e62ad16 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -75,6 +75,8 @@ const (
SchemaRuleStatementAffectedRowLimit SQLReviewRuleType = "statement.affected-row-limit"
// SchemaRuleStatementDMLDryRun dry run the dml.
SchemaRuleStatementDMLDryRun SQLReviewRuleType = "statement.dml-dry-run"
+ // SchemaRuleStatementDisallowAddColumnWithDefault disallow to add column with DEFAULT.
+ SchemaRuleStatementDisallowAddColumnWithDefault = "statement.disallow-add-column-with-default"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -950,6 +952,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.Postgres:
return PostgreSQLStatementDMLDryRun, nil
}
+ case SchemaRuleStatementDisallowAddColumnWithDefault:
+ if engine == db.Postgres {
+ return PostgreSQLDisallowAddColumnWithDefault, 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 e91bb6f957..b9c0812f14 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -380,6 +380,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {
SchemaRuleDropEmptyDatabase,
SchemaRuleIndexNoDuplicateColumn,
SchemaRuleIndexPKTypeLimit,
+ SchemaRuleStatementDisallowAddColumnWithDefault,
SchemaRuleIndexTypeNoBlob:
case SchemaRuleTableDropNamingConvention:
payload, err = json.Marshal(NamingRulePayload{