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(catalog): check default value for NOT NULL column (#3060)
rebelice2219e044b506
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.goindex 826dbd1402..ae61b2781f 100644--- a/plugin/advisor/catalog/walk_through.go+++ b/plugin/advisor/catalog/walk_through.go@@ -63,6 +63,8 @@ const (ErrorTypeAutoIncrementExists = 404// ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp is the error that the ON UPDATE column is not datetime or timestamp.ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp = 405+ // ErrorTypeSetNullDefaultForNotNullColumn is the error that setting NULL default value for the NOT NULL column.+ ErrorTypeSetNullDefaultForNotNullColumn = 406// 501 ~ 599 index error type.@@ -1181,6 +1183,7 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,collation: newStringPointer(column.Tp.GetCollate()),comment: newEmptyStringPointer(),}+ setNullDefault := falsefor _, option := range column.Options {switch option.Tp {@@ -1200,6 +1203,8 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,return err}col.defaultValue = &defaultValue+ } else {+ setNullDefault = true}case tidbast.ColumnOptionUniqKey:if err := t.createIndex("", []string{col.name}, true /* unique */, model.IndexTypeBtree.String(), nil); err != nil {@@ -1239,6 +1244,14 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,}}+ if col.nullable != nil && !*col.nullable && setNullDefault {+ return &WalkThroughError{+ Type: ErrorTypeSetNullDefaultForNotNullColumn,+ // Content comes from MySQL Error content.+ Content: fmt.Sprintf("Invalid default value for column `%s`", col.name),+ }+ }+t.columnSet[col.name] = colreturn nil}diff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.goindex bd6ce678f2..c715a222b9 100644--- a/plugin/advisor/catalog/walk_through_test.go+++ b/plugin/advisor/catalog/walk_through_test.go@@ -23,6 +23,20 @@ var (func TestWalkThrough(t *testing.T) {tests := []testData{+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a INT NOT NULL DEFAULT NULL)+ `,+ err: &WalkThroughError{+ Type: ErrorTypeSetNullDefaultForNotNullColumn,+ Content: "Invalid default value for column `a`",+ Line: 2,+ },+ },{origin: &Database{Name: "test",