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(sql-review): implement postgresql column type disallow list (#3143)
Vũ Hải Lâmbed27f2d4958
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 670019bd50..1a9361a729 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -259,6 +259,9 @@ const (// PostgreSQLIndexNoDuplicateColumn is an advisor type for Postgresql no duplicate columns in index.PostgreSQLIndexNoDuplicateColumn Type = "bb.plugin.advisor.postgresql.index.no-duplicate-column"++ // PostgreSQLColumnTypeDisallowList is an advisor type for Postgresql column type disallow list.+ PostgreSQLColumnTypeDisallowList Type = "bb.plugin.advisor.postgresql.column.type-disallow-list")// Advice is the result of an advisor.diff --git a/plugin/advisor/pg/advisor_column_type_disallow_list.go b/plugin/advisor/pg/advisor_column_type_disallow_list.gonew file mode 100644index 0000000000..95b6863b61--- /dev/null+++ b/plugin/advisor/pg/advisor_column_type_disallow_list.go@@ -0,0 +1,153 @@+package pg++import (+ "fmt"+ "strings"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/db"+ "github.com/bytebase/bytebase/plugin/parser/ast"+)++func init() {+ advisor.Register(db.Postgres, advisor.PostgreSQLColumnTypeDisallowList, &ColumnTypeDisallowListAdvisor{})+}++// ColumnTypeDisallowListAdvisor is the advisor checking for column type restriction.+type ColumnTypeDisallowListAdvisor struct {+}++// Check checks for column type restriction.+func (*ColumnTypeDisallowListAdvisor) 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+ }++ payload, err := advisor.UnmarshalStringArrayTypeRulePayload(ctx.Rule.Payload)+ if err != nil {+ return nil, err+ }+ checker := &columnTypeDisallowListChecker{+ level: level,+ title: string(ctx.Rule.Type),+ typeRestriction: make(map[string]bool),+ }+ for _, tp := range payload.List {+ checker.typeRestriction[strings.ToLower(tp)] = true+ }++ for _, stmt := range stmtList {+ checker.text = stmt.Text()+ 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 columnTypeDisallowListChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+ typeRestriction map[string]bool+}++type columnTypeData struct {+ table string+ column string+ tp string+ line int+}++func (checker *columnTypeDisallowListChecker) Visit(in ast.Node) ast.Visitor {+ var columnList []columnTypeData+ switch node := in.(type) {+ case *ast.CreateTableStmt:+ for _, column := range node.ColumnList {+ exist := false+ typeDisallow := ""+ for tp := range checker.typeRestriction {+ if exist = column.Type.EquivalentType(tp); exist {+ typeDisallow = tp+ break+ }+ }+ if exist {+ columnList = append(columnList, columnTypeData{+ table: node.Name.Name,+ column: column.ColumnName,+ tp: strings.ToUpper(typeDisallow),+ line: column.LastLine(),+ })+ }+ }+ case *ast.AlterTableStmt:+ for _, item := range node.AlterItemList {+ switch cmd := item.(type) {+ case *ast.AddColumnListStmt:+ for _, column := range cmd.ColumnList {+ exist := false+ typeDisallow := ""+ for tp := range checker.typeRestriction {+ if exist = column.Type.EquivalentType(tp); exist {+ typeDisallow = tp+ break+ }+ }+ if exist {+ columnList = append(columnList, columnTypeData{+ table: node.Table.Name,+ column: column.ColumnName,+ tp: strings.ToUpper(typeDisallow),+ line: checker.line,+ })+ }+ }+ case *ast.ChangeColumnStmt:+ exist := false+ typeDisallow := ""+ for tp := range checker.typeRestriction {+ if exist = cmd.Column.Type.EquivalentType(tp); exist {+ typeDisallow = tp+ break+ }+ }+ if exist {+ columnList = append(columnList, columnTypeData{+ table: node.Table.Name,+ column: cmd.Column.ColumnName,+ tp: strings.ToUpper(typeDisallow),+ line: checker.line,+ })+ }+ }+ }+ }++ for _, column := range columnList {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.DisabledColumnType,+ Title: checker.title,+ Content: fmt.Sprintf("Disallow column type %s but column \"%s\".\"%s\" is", column.tp, column.table, column.column),+ Line: column.line,+ })+ }+ return checker+}diff --git a/plugin/advisor/pg/advisor_column_type_disallow_list_test.go b/plugin/advisor/pg/advisor_column_type_disallow_list_test.gonew file mode 100644index 0000000000..df616508b4--- /dev/null+++ b/plugin/advisor/pg/advisor_column_type_disallow_list_test.go@@ -0,0 +1,76 @@+package pg++import (+ "encoding/json"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestColumnTypeDisallowList(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: `CREATE TABLE t(a char(5));`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ Statement: `CREATE TABLE t(a int, b bigint, c real);`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.DisabledColumnType,+ Title: "column.type-disallow-list",+ Content: "Disallow column type INT but column \"t\".\"a\" is",+ Line: 1,+ },+ {+ Status: advisor.Warn,+ Code: advisor.DisabledColumnType,+ Title: "column.type-disallow-list",+ Content: "Disallow column type BIGINT but column \"t\".\"b\" is",+ Line: 1,+ },+ {+ Status: advisor.Warn,+ Code: advisor.DisabledColumnType,+ Title: "column.type-disallow-list",+ Content: "Disallow column type REAL but column \"t\".\"c\" is",+ Line: 1,+ },+ },+ },+ {+ Statement: `+ CREATE TABLE t(d char(5));+ ALTER TABLE t add COLUMN a int;`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.DisabledColumnType,+ Title: "column.type-disallow-list",+ Content: "Disallow column type INT but column \"t\".\"a\" is",+ Line: 3,+ },+ },+ },+ }++ payload, err := json.Marshal(advisor.StringArrayTypeRulePayload{+ List: []string{"int", "float", "bigint", "real"},+ })+ require.NoError(t, err)+ advisor.RunSQLReviewRuleTests(t, tests, &ColumnTypeDisallowListAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleColumnTypeDisallowList,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: string(payload),+ }, advisor.MockPostgreSQLDatabase)+}diff --git a/scripts/.air.toml b/scripts/.air.tomlindex 1eb85131db..a73c2f4c69 100644--- a/scripts/.air.toml+++ b/scripts/.air.toml@@ -6,7 +6,7 @@ tmp_dir = ".air"## Use --tags "store.db" to enable SQL query logging against our metadata db.cmd = """go build \- --tags "" \+ --tags "store.db" \-gcflags=all=\"-N -l\" \-ldflags " \-X 'github.com/bytebase/bytebase/bin/server/cmd.version=development' \