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): validate INSERT statement in walk through (#2959)
rebelicea705a74bf1f1
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.goindex 03bd187821..5819f45abd 100644--- a/plugin/advisor/catalog/walk_through.go+++ b/plugin/advisor/catalog/walk_through.go@@ -11,6 +11,7 @@ import (tidbast "github.com/pingcap/tidb/parser/ast""github.com/pingcap/tidb/parser/format""github.com/pingcap/tidb/parser/model"+ "github.com/pingcap/tidb/parser/mysql")// WalkThroughErrorType is the type of WalkThroughError.@@ -73,6 +74,15 @@ const (ErrorTypeIncorrectIndexName = 506// ErrorTypeSpatialIndexKeyNullable is the error that keys in spatial index are nullable.ErrorTypeSpatialIndexKeyNullable = 507++ // 601 ~ 699 insert statement error type.++ // ErrorTypeInsertColumnCountNotMatchValueCount is the error that column count doesn't match value count.+ ErrorTypeInsertColumnCountNotMatchValueCount = 601+ // ErrorTypeInsertSpecifiedColumnTwice is the error that column specified twice in INSERT.+ ErrorTypeInsertSpecifiedColumnTwice = 602+ // ErrorTypeInsertNullIntoNotNullColumn is the error that insert NULL into NOT NULL columns.+ ErrorTypeInsertNullIntoNotNullColumn = 603)// WalkThroughError is the error for walking-through.@@ -254,10 +264,46 @@ func (d *DatabaseState) checkInsert(node *tidbast.InsertStmt) *WalkThroughErrorif table == nil {return nil}+ specifiedColumnMap := make(map[string]bool)for _, col := range node.Columns {if _, exists := table.columnSet[col.Name.O]; !exists {return NewColumnNotExistsError(table.name, col.Name.O)}+ if _, ok := specifiedColumnMap[col.Name.O]; ok {+ return &WalkThroughError{+ Type: ErrorTypeInsertSpecifiedColumnTwice,+ // Content is from MySQL error content+ Content: fmt.Sprintf("Column '%s' specified twice", col.Name.O),+ }+ }+ specifiedColumnMap[col.Name.O] = true+ }++ if len(node.Lists) > 0 {+ for i, row := range node.Lists {+ if node.Columns != nil && len(node.Columns) != len(row) {+ return &WalkThroughError{+ Type: ErrorTypeInsertColumnCountNotMatchValueCount,+ // Content is from MySQL error content+ Content: fmt.Sprintf("Column count doesn't match value count at row %d", i+1),+ }+ }++ for columnPos, value := range row {+ if value.GetType().GetType() == mysql.TypeNull {+ columnName := node.Columns[columnPos].Name.O+ // after check, all columns exist+ column := table.columnSet[columnName]+ if column.nullable != nil && !*column.nullable {+ return &WalkThroughError{+ Type: ErrorTypeInsertNullIntoNotNullColumn,+ // Content is from MySQL error content+ Content: fmt.Sprintf("Column '%s' cannot be null", columnName),+ }+ }+ }+ }+ }}}return nildiff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.goindex 1131c9005f..ad41430a7e 100644--- a/plugin/advisor/catalog/walk_through_test.go+++ b/plugin/advisor/catalog/walk_through_test.go@@ -23,6 +23,51 @@ var (func TestWalkThrough(t *testing.T) {tests := []testData{+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a int NOT NULL);+ INSERT INTO t(a) values (1), (NULL);+ `,+ err: &WalkThroughError{+ Type: ErrorTypeInsertNullIntoNotNullColumn,+ Content: "Column 'a' cannot be null",+ Line: 3,+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a int);+ INSERT INTO t(a) values (1), ();+ `,+ err: &WalkThroughError{+ Type: ErrorTypeInsertColumnCountNotMatchValueCount,+ Content: "Column count doesn't match value count at row 2",+ Line: 3,+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a int);+ INSERT INTO t(a, a) values (1, 2);+ `,+ err: &WalkThroughError{+ Type: ErrorTypeInsertSpecifiedColumnTwice,+ Content: "Column 'a' specified twice",+ Line: 3,+ },+ },{origin: &Database{Name: "test",