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: implement Oracle column disallow list rule (#5983)
rebelice54f53749c773
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 04583f5583..4205740336 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -346,6 +346,9 @@ const (// OracleColumnRequirement is an advisor type for Oracle column requirement.OracleColumnRequirement Type = "bb.plugin.advisor.oracle.column.require"++ // OracleColumnTypeDisallowList is an advisor type for Oracle column type disallow list.+ OracleColumnTypeDisallowList Type = "bb.plugin.advisor.oracle.column.type-disallow-list")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/oracle/advisor_column_type_disallow_list.go b/backend/plugin/advisor/oracle/advisor_column_type_disallow_list.gonew file mode 100644index 0000000000..1a2122fe3a--- /dev/null+++ b/backend/plugin/advisor/oracle/advisor_column_type_disallow_list.go@@ -0,0 +1,128 @@+// Package oracle is the advisor for oracle database.+package oracle++import (+ "fmt"++ "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"+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"+)++var (+ _ advisor.Advisor = (*ColumnTypeDisallowListAdvisor)(nil)+)++func init() {+ advisor.Register(db.Oracle, advisor.OracleColumnTypeDisallowList, &ColumnTypeDisallowListAdvisor{})+}++// ColumnTypeDisallowListAdvisor is the advisor checking for column type disallow list.+type ColumnTypeDisallowListAdvisor struct {+}++// Check checks for column type disallow list.+func (*ColumnTypeDisallowListAdvisor) 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+ }++ payload, err := advisor.UnmarshalStringArrayTypeRulePayload(ctx.Rule.Payload)+ if err != nil {+ return nil, err+ }+ listener := &columnTypeDisallowListListener{+ level: level,+ title: string(ctx.Rule.Type),+ currentSchema: ctx.CurrentSchema,+ disallowList: payload.List,+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// columnTypeDisallowListListener is the listener for column type disallow list.+type columnTypeDisallowListListener struct {+ *parser.BasePlSqlParserListener++ level advisor.Status+ title string+ currentSchema string+ disallowList []string+ adviceList []advisor.Advice+}++func (l *columnTypeDisallowListListener) generateAdvice() ([]advisor.Advice, error) {+ if len(l.adviceList) == 0 {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return l.adviceList, nil+}++func (l *columnTypeDisallowListListener) isDisallowType(tp parser.IDatatypeContext) bool {+ if tp == nil {+ return false+ }+ for _, disallowType := range l.disallowList {+ if equivalent, err := bbparser.PLSQLEquivalentType(tp, disallowType); err == nil && equivalent {+ return true+ }+ }+ return false+}++// EnterColumn_definition is called when production column_definition is entered.+func (l *columnTypeDisallowListListener) EnterColumn_definition(ctx *parser.Column_definitionContext) {+ if l.isDisallowType(ctx.Datatype()) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.DisabledColumnType,+ Title: l.title,+ Content: fmt.Sprintf("Disallow column type %s but column \"%s\" is", ctx.Datatype().GetText(), normalizeIdentifier(ctx.Column_name(), l.currentSchema)),+ Line: ctx.Datatype().GetStart().GetLine(),+ })+ }+ if ctx.Regular_id() != nil {+ for _, tp := range l.disallowList {+ if ctx.Regular_id().GetText() == tp {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.DisabledColumnType,+ Title: l.title,+ Content: fmt.Sprintf("Disallow column type %s but column \"%s\" is", ctx.Regular_id().GetText(), normalizeIdentifier(ctx.Column_name(), l.currentSchema)),+ Line: ctx.Regular_id().GetStart().GetLine(),+ })+ break+ }+ }+ }+}++// EnterModify_col_properties is called when production modify_col_properties is entered.+func (l *columnTypeDisallowListListener) EnterModify_col_properties(ctx *parser.Modify_col_propertiesContext) {+ if l.isDisallowType(ctx.Datatype()) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.DisabledColumnType,+ Title: l.title,+ Content: fmt.Sprintf("Disallow column type %s but column \"%s\" is", ctx.Datatype().GetText(), normalizeIdentifier(ctx.Column_name(), l.currentSchema)),+ Line: ctx.Datatype().GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.goindex 12d404b385..f624a46568 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -14,6 +14,7 @@ func TestOracleRules(t *testing.T) {advisor.SchemaRuleTableNoFK,advisor.SchemaRuleTableNaming,advisor.SchemaRuleRequiredColumn,+ advisor.SchemaRuleColumnTypeDisallowList,}for _, rule := range oracleRules {diff --git a/backend/plugin/advisor/oracle/test/column_type_disallow_list.yaml b/backend/plugin/advisor/oracle/test/column_type_disallow_list.yamlnew file mode 100644index 0000000000..cdff2d6167--- /dev/null+++ b/backend/plugin/advisor/oracle/test/column_type_disallow_list.yaml@@ -0,0 +1,54 @@+- statement: CREATE TABLE t(a char(5));+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: CREATE TABLE t(a int, b JSON);+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type JSON but column "B" is+ line: 1+ details: ""+- statement: |-+ CREATE TABLE t(d char(5));+ ALTER TABLE t add a JSON;+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type JSON but column "A" is+ line: 2+ details: ""+- statement: |-+ CREATE TABLE t(d char(5));+ ALTER TABLE t MODIFY d BINARY_FLOAT;+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type BINARY_FLOAT but column "D" is+ line: 2+ details: ""+- statement: CREATE TABLE t(a int, b BINARY_FLOAT);+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type BINARY_FLOAT but column "B" is+ line: 1+ details: ""+- statement: |-+ CREATE TABLE t(d char(5));+ ALTER TABLE t add a BINARY_FLOAT;+ want:+ - status: WARN+ code: 411+ title: column.type-disallow-list+ content: Disallow column type BINARY_FLOAT but column "A" is+ line: 2+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex c589b3b767..6cb7340dc1 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -890,6 +890,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errreturn MySQLColumnTypeRestriction, nilcase db.Postgres:return PostgreSQLColumnTypeDisallowList, nil+ case db.Oracle:+ return OracleColumnTypeDisallowList, nil}case SchemaRuleColumnDisallowSetCharset:switch engine {diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex f3a4f261e0..26c8582e78 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -433,7 +433,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {})case SchemaRuleColumnTypeDisallowList:payload, err = json.Marshal(StringArrayTypeRulePayload{- List: []string{"JSON"},+ List: []string{"JSON", "BINARY_FLOAT"},})case SchemaRuleColumnMaximumCharacterLength:payload, err = json.Marshal(NumberTypeRulePayload{diff --git a/backend/plugin/parser/sql/plsql.go b/backend/plugin/parser/sql/plsql.goindex a5e586bd6c..796be7a8d0 100644--- a/backend/plugin/parser/sql/plsql.go+++ b/backend/plugin/parser/sql/plsql.go@@ -89,3 +89,158 @@ func ParsePLSQL(sql string) (antlr.Tree, error) {return tree, nil}++// PLSQLEquivalentType returns true if the given type is equivalent to the given text.+func PLSQLEquivalentType(tp parser.IDatatypeContext, text string) (bool, error) {+ tree, err := ParsePLSQL(fmt.Sprintf(`CREATE TABLE t(a %s);`, text))+ if err != nil {+ return false, err+ }++ listener := &typeEquivalentListener{tp: tp, equivalent: false}+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)+ return listener.equivalent, nil+}++type typeEquivalentListener struct {+ *parser.BasePlSqlParserListener++ tp parser.IDatatypeContext+ equivalent bool+}++// EnterColumn_definition is called when production column_definition is entered.+func (l *typeEquivalentListener) EnterColumn_definition(ctx *parser.Column_definitionContext) {+ if ctx.Datatype() != nil {+ l.equivalent = equivalentType(l.tp, ctx.Datatype())+ }+}++func equivalentType(lType parser.IDatatypeContext, rType parser.IDatatypeContext) bool {+ if lType == nil || rType == nil {+ return false+ }+ lNative := lType.Native_datatype_element()+ rNative := rType.Native_datatype_element()++ if lNative != nil && rNative != nil {+ switch {+ case lNative.BINARY_INTEGER() != nil:+ return rNative.BINARY_INTEGER() != nil+ case lNative.PLS_INTEGER() != nil:+ return rNative.PLS_INTEGER() != nil+ case lNative.NATURAL() != nil:+ return rNative.NATURAL() != nil+ case lNative.BINARY_FLOAT() != nil:+ return rNative.BINARY_FLOAT() != nil+ case lNative.BINARY_DOUBLE() != nil:+ return rNative.BINARY_DOUBLE() != nil+ case lNative.NATURALN() != nil:+ return rNative.NATURALN() != nil+ case lNative.POSITIVE() != nil:+ return rNative.POSITIVE() != nil+ case lNative.POSITIVEN() != nil:+ return rNative.POSITIVEN() != nil+ case lNative.SIGNTYPE() != nil:+ return rNative.SIGNTYPE() != nil+ case lNative.SIMPLE_INTEGER() != nil:+ return rNative.SIMPLE_INTEGER() != nil+ case lNative.NVARCHAR2() != nil:+ return rNative.NVARCHAR2() != nil+ case lNative.DEC() != nil:+ return rNative.DEC() != nil+ case lNative.INTEGER() != nil:+ return rNative.INTEGER() != nil+ case lNative.INT() != nil:+ return rNative.INT() != nil+ case lNative.NUMERIC() != nil:+ return rNative.NUMERIC() != nil+ case lNative.SMALLINT() != nil:+ return rNative.SMALLINT() != nil+ case lNative.NUMBER() != nil:+ return rNative.NUMBER() != nil+ case lNative.DECIMAL() != nil:+ return rNative.DECIMAL() != nil+ case lNative.DOUBLE() != nil:+ return rNative.DOUBLE() != nil+ case lNative.FLOAT() != nil:+ return rNative.FLOAT() != nil+ case lNative.REAL() != nil:+ return rNative.REAL() != nil+ case lNative.NCHAR() != nil:+ return rNative.NCHAR() != nil+ case lNative.LONG() != nil:+ return rNative.LONG() != nil+ case lNative.CHAR() != nil:+ return rNative.CHAR() != nil+ case lNative.CHARACTER() != nil:+ return rNative.CHARACTER() != nil+ case lNative.VARCHAR2() != nil:+ return rNative.VARCHAR2() != nil+ case lNative.VARCHAR() != nil:+ return rNative.VARCHAR() != nil+ case lNative.STRING() != nil:+ return rNative.STRING() != nil+ case lNative.RAW() != nil:+ return rNative.RAW() != nil+ case lNative.BOOLEAN() != nil:+ return rNative.BOOLEAN() != nil+ case lNative.DATE() != nil:+ return rNative.DATE() != nil+ case lNative.ROWID() != nil:+ return rNative.ROWID() != nil+ case lNative.UROWID() != nil:+ return rNative.UROWID() != nil+ case lNative.YEAR() != nil:+ return rNative.YEAR() != nil+ case lNative.MONTH() != nil:+ return rNative.MONTH() != nil+ case lNative.DAY() != nil:+ return rNative.DAY() != nil+ case lNative.HOUR() != nil:+ return rNative.HOUR() != nil+ case lNative.MINUTE() != nil:+ return rNative.MINUTE() != nil+ case lNative.SECOND() != nil:+ return rNative.SECOND() != nil+ case lNative.TIMEZONE_HOUR() != nil:+ return rNative.TIMEZONE_HOUR() != nil+ case lNative.TIMEZONE_MINUTE() != nil:+ return rNative.TIMEZONE_MINUTE() != nil+ case lNative.TIMEZONE_REGION() != nil:+ return rNative.TIMEZONE_REGION() != nil+ case lNative.TIMEZONE_ABBR() != nil:+ return rNative.TIMEZONE_ABBR() != nil+ case lNative.TIMESTAMP() != nil:+ return rNative.TIMESTAMP() != nil+ case lNative.TIMESTAMP_UNCONSTRAINED() != nil:+ return rNative.TIMESTAMP_UNCONSTRAINED() != nil+ case lNative.TIMESTAMP_TZ_UNCONSTRAINED() != nil:+ return rNative.TIMESTAMP_TZ_UNCONSTRAINED() != nil+ case lNative.TIMESTAMP_LTZ_UNCONSTRAINED() != nil:+ return rNative.TIMESTAMP_LTZ_UNCONSTRAINED() != nil+ case lNative.YMINTERVAL_UNCONSTRAINED() != nil:+ return rNative.YMINTERVAL_UNCONSTRAINED() != nil+ case lNative.DSINTERVAL_UNCONSTRAINED() != nil:+ return rNative.DSINTERVAL_UNCONSTRAINED() != nil+ case lNative.BFILE() != nil:+ return rNative.BFILE() != nil+ case lNative.BLOB() != nil:+ return rNative.BLOB() != nil+ case lNative.CLOB() != nil:+ return rNative.CLOB() != nil+ case lNative.NCLOB() != nil:+ return rNative.NCLOB() != nil+ case lNative.MLSLABEL() != nil:+ return rNative.MLSLABEL() != nil+ default:+ return false+ }+ }++ if lNative != nil || rNative != nil {+ return false+ }++ return lType.GetText() == rType.GetText()+}