Skip to content

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

feat: implement Oracle column no null rule (#6034)

rebelice783ba5e32f4e
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 58692d6419..26c3f6d74c 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -367,6 +367,9 @@ const (
// OracleIndexKeyNumberLimit is an advisor type for Oracle index key number limit.
OracleIndexKeyNumberLimit Type = "bb.plugin.advisor.oracle.index.key-number-limit"
+
+ // OracleColumnNoNull is an advisor type for Oracle column no NULL value.
+ OracleColumnNoNull Type = "bb.plugin.advisor.oracle.column.no-null"
)
// Advice is the result of an advisor.
diff --git a/backend/plugin/advisor/oracle/advisor_column_no_null.go b/backend/plugin/advisor/oracle/advisor_column_no_null.go
new file mode 100644
index 0000000000..abfdc344ba
--- /dev/null
+++ b/backend/plugin/advisor/oracle/advisor_column_no_null.go
@@ -0,0 +1,164 @@
+// Package oracle is the advisor for oracle database.
+package oracle
+
+import (
+ "fmt"
+ "sort"
+
+ "github.com/antlr/antlr4/runtime/Go/antlr/v4"
+ parser "github.com/bytebase/plsql-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnNoNullAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.Oracle, advisor.OracleColumnNoNull, &ColumnNoNullAdvisor{})
+}
+
+type columnMap map[string]int
+
+// ColumnNoNullAdvisor is the advisor checking for column no NULL value.
+type ColumnNoNullAdvisor struct {
+}
+
+// Check checks for column no NULL value.
+func (*ColumnNoNullAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ tree, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ listener := &columnNoNullListener{
+ level: level,
+ title: string(ctx.Rule.Type),
+ currentSchema: ctx.CurrentSchema,
+ nullableColumns: make(columnMap),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// columnNoNullListener is the listener for column no NULL value.
+type columnNoNullListener struct {
+ *parser.BasePlSqlParserListener
+
+ level advisor.Status
+ title string
+ currentSchema string
+ nullableColumns columnMap
+ tableName string
+ columnID string
+}
+
+func (l *columnNoNullListener) generateAdvice() ([]advisor.Advice, error) {
+ advice := []advisor.Advice{}
+
+ var columnIDs []string
+ for columnID := range l.nullableColumns {
+ columnIDs = append(columnIDs, columnID)
+ }
+ sort.Strings(columnIDs)
+ for _, columnID := range columnIDs {
+ line := l.nullableColumns[columnID]
+ advice = append(advice, advisor.Advice{
+ Status: l.level,
+ Code: advisor.ColumnCannotNull,
+ Title: l.title,
+ Content: fmt.Sprintf("Column %q is nullable, which is not allowed.", lastIdentifier(columnID)),
+ Line: line,
+ })
+ }
+
+ if len(advice) == 0 {
+ advice = append(advice, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return advice, nil
+}
+
+// EnterCreate_table is called when production create_table is entered.
+func (l *columnNoNullListener) EnterCreate_table(ctx *parser.Create_tableContext) {
+ l.tableName = normalizeIdentifier(ctx.Table_name(), l.currentSchema)
+}
+
+// ExitCreate_table is called when production create_table is exited.
+func (l *columnNoNullListener) ExitCreate_table(_ *parser.Create_tableContext) {
+ l.tableName = ""
+}
+
+// EnterColumn_definition is called when production column_definition is entered.
+func (l *columnNoNullListener) EnterColumn_definition(ctx *parser.Column_definitionContext) {
+ if l.tableName == "" {
+ return
+ }
+ columnName := normalizeIdentifier(ctx.Column_name(), l.currentSchema)
+ l.columnID = fmt.Sprintf(`%s.%s`, l.tableName, columnName)
+ l.nullableColumns[l.columnID] = ctx.GetStart().GetLine()
+}
+
+// ExitColumn_definition is called when production column_definition is exited.
+func (l *columnNoNullListener) ExitColumn_definition(_ *parser.Column_definitionContext) {
+ l.columnID = ""
+}
+
+// EnterInline_constraint is called when production inline_constraint is entered.
+func (l *columnNoNullListener) EnterInline_constraint(ctx *parser.Inline_constraintContext) {
+ if l.columnID == "" {
+ return
+ }
+ if ctx.NULL_() != nil {
+ l.nullableColumns[l.columnID] = ctx.GetStart().GetLine()
+ }
+ if ctx.NOT() != nil || ctx.PRIMARY() != nil {
+ delete(l.nullableColumns, l.columnID)
+ }
+}
+
+// EnterOut_of_line_constraint is called when production out_of_line_constraint is entered.
+func (l *columnNoNullListener) EnterOut_of_line_constraint(ctx *parser.Out_of_line_constraintContext) {
+ if l.tableName == "" {
+ return
+ }
+ if ctx.PRIMARY() != nil {
+ for _, column := range ctx.AllColumn_name() {
+ columnName := normalizeIdentifier(column, l.currentSchema)
+ columnID := fmt.Sprintf(`%s.%s`, l.tableName, columnName)
+ delete(l.nullableColumns, columnID)
+ }
+ }
+}
+
+// EnterAlter_table is called when production alter_table is entered.
+func (l *columnNoNullListener) EnterAlter_table(ctx *parser.Alter_tableContext) {
+ l.tableName = normalizeIdentifier(ctx.Tableview_name(), l.currentSchema)
+}
+
+// ExitAlter_table is called when production alter_table is exited.
+func (l *columnNoNullListener) ExitAlter_table(_ *parser.Alter_tableContext) {
+ l.tableName = ""
+}
+
+// EnterModify_col_properties is called when production modify_col_properties is entered.
+func (l *columnNoNullListener) EnterModify_col_properties(ctx *parser.Modify_col_propertiesContext) {
+ if l.tableName == "" {
+ return
+ }
+ columnName := normalizeIdentifier(ctx.Column_name(), l.currentSchema)
+ l.columnID = fmt.Sprintf(`%s.%s`, l.tableName, columnName)
+}
diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.go
index 6b52001bb7..c2658be29e 100644
--- a/backend/plugin/advisor/oracle/oracle_rules_test.go
+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go
@@ -21,6 +21,7 @@ func TestOracleRules(t *testing.T) {
advisor.SchemaRuleStatementRequireWhere,
advisor.SchemaRuleStatementInsertMustSpecifyColumn,
advisor.SchemaRuleIndexKeyNumberLimit,
+ advisor.SchemaRuleColumnNotNull,
}
for _, rule := range oracleRules {
diff --git a/backend/plugin/advisor/oracle/test/column_no_null.yaml b/backend/plugin/advisor/oracle/test/column_no_null.yaml
new file mode 100644
index 0000000000..dee048773e
--- /dev/null
+++ b/backend/plugin/advisor/oracle/test/column_no_null.yaml
@@ -0,0 +1,94 @@
+- statement: |-
+ CREATE TABLE book (
+ id int,
+ name varchar(255)
+ )
+ want:
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "ID" is nullable, which is not allowed.
+ line: 2
+ details: ""
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "NAME" is nullable, which is not allowed.
+ line: 3
+ details: ""
+- statement: CREATE TABLE book(id int, name varchar(255), PRIMARY KEY (id))
+ want:
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "NAME" is nullable, which is not allowed.
+ line: 1
+ details: ""
+- statement: CREATE TABLE book(id int PRIMARY KEY, name varchar(255))
+ want:
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "NAME" is nullable, which is not allowed.
+ line: 1
+ details: ""
+- statement: CREATE TABLE book(id int NOT NULL, name varchar(255))
+ want:
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "NAME" is nullable, which is not allowed.
+ line: 1
+ details: ""
+- statement: CREATE TABLE book(id int PRIMARY KEY, name varchar(255) NOT NULL)
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: ALTER TABLE tech_book ADD reader int
+ want:
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "READER" is nullable, which is not allowed.
+ line: 1
+ details: ""
+- statement: |-
+ ALTER TABLE tech_book DROP COLUMN id;
+ ALTER TABLE tech_book DROP COLUMN name;
+ ALTER TABLE tech_book ADD id int PRIMARY KEY;
+ ALTER TABLE tech_book ADD name varchar(255) NOT NULL;
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: ALTER TABLE tech_book MODIFY id NOT NULL
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: ALTER TABLE tech_book MODIFY name NULL
+ want:
+ - status: WARN
+ code: 402
+ title: column.no-null
+ content: Column "NAME" is nullable, which is not allowed.
+ line: 1
+ details: ""
+- statement: -- this is a comment
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 0a14c90407..25a1b36738 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -857,6 +857,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return MySQLColumnNoNull, nil
case db.Postgres:
return PostgreSQLColumnNoNull, nil
+ case db.Oracle:
+ return OracleColumnNoNull, nil
}
case SchemaRuleColumnDisallowChangeType:
switch engine {