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 UPDATE and DELETE for single table (#2968)
rebelice8ccd3ff1a680
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.goindex 9a972fbb07..3511c6915d 100644--- a/plugin/advisor/catalog/walk_through.go+++ b/plugin/advisor/catalog/walk_through.go@@ -212,6 +212,10 @@ func (d *DatabaseState) validateDML(in tidbast.DMLNode) (err *WalkThroughError)switch node := in.(type) {case *tidbast.InsertStmt:return d.checkInsert(node)+ case *tidbast.DeleteStmt:+ return d.checkDelete(node)+ case *tidbast.UpdateStmt:+ return d.checkUpdate(node)default:return nil}@@ -256,6 +260,41 @@ func (d *DatabaseState) changeState(in tidbast.StmtNode) (err *WalkThroughError)}}+func (d *DatabaseState) checkUpdate(node *tidbast.UpdateStmt) *WalkThroughError {+ // Only check the UPDATE single TABLE case.+ tableName, ok := getTableSourceName(node.TableRefs)+ if ok {+ table, err := d.findTableState(tableName, false /* createIncompleteTable */)+ if err != nil {+ return err+ }+ if table == nil {+ return nil+ }+ for _, item := range node.List {+ columName := item.Column.Name.O+ if _, exists := table.columnSet[columName]; !exists {+ return NewColumnNotExistsError(table.name, columName)+ }+ }+ }+ return nil+}++func (d *DatabaseState) checkDelete(node *tidbast.DeleteStmt) *WalkThroughError {+ // Only check the DELETE FROM single TABLE case.+ if !node.IsMultiTable {+ tableName, ok := getTableSourceName(node.TableRefs)+ if ok {+ _, err := d.findTableState(tableName, false /* createIncopleteTable */)+ if err != nil {+ return err+ }+ }+ }+ return nil+}+func (d *DatabaseState) checkInsert(node *tidbast.InsertStmt) *WalkThroughError {tableName, ok := getTableSourceName(node.Table)if ok {diff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.goindex 2dacb55972..e5590d6928 100644--- a/plugin/advisor/catalog/walk_through_test.go+++ b/plugin/advisor/catalog/walk_through_test.go@@ -23,6 +23,49 @@ var (func TestWalkThrough(t *testing.T) {tests := []testData{+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ UPDATE t SET a = 1;+ `,+ err: &WalkThroughError{+ Type: ErrorTypeTableNotExists,+ Content: "Table `t` does not exist",+ Line: 2,+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a int);+ UPDATE t set b = 222;+ `,+ err: &WalkThroughError{+ Type: ErrorTypeColumnNotExists,+ Content: "Column `b` does not exist in table `t`",+ Line: 3,+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ DELETE FROM t;+ `,+ err: &WalkThroughError{+ Type: ErrorTypeTableNotExists,+ Content: "Table `t` does not exist",+ Line: 2,+ },+ },{origin: &Database{Name: "test",diff --git a/tests/sql_review_test.go b/tests/sql_review_test.goindex 979ca55f9b..fca516c085 100644--- a/tests/sql_review_test.go+++ b/tests/sql_review_test.go@@ -512,7 +512,16 @@ func TestSQLReviewForMySQL(t *testing.T) {},},{- statement: "DELETE FROM tech_book",+ statement: `+ CREATE TABLE tech_book(+ id int NOT NULL,+ creator_id INT NOT NULL,+ created_ts TIMESTAMP NOT NULL,+ updater_id INT NOT NULL,+ updated_ts TIMESTAMP NOT NULL,+ CONSTRAINT pk_user_id PRIMARY KEY (id)+ );+ DELETE FROM tech_book`,result: []api.TaskCheckResult{{Status: api.TaskCheckStatusError,@@ -524,7 +533,18 @@ func TestSQLReviewForMySQL(t *testing.T) {},},{- statement: "DELETE FROM tech_book WHERE name like `%abc`",+ statement: `+ CREATE TABLE tech_book(+ id int NOT NULL,+ name varchar(220) NOT NULL,+ creator_id INT NOT NULL,+ created_ts TIMESTAMP NOT NULL,+ updater_id INT NOT NULL,+ updated_ts TIMESTAMP NOT NULL,+ CONSTRAINT pk_user_id PRIMARY KEY (id)+ );+ ` ++ "DELETE FROM tech_book WHERE name like `%abc`",result: []api.TaskCheckResult{{Status: api.TaskCheckStatusError,@@ -593,7 +613,18 @@ func TestSQLReviewForMySQL(t *testing.T) {},},{- statement: "DELETE FROM tech_book WHERE a = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')",+ statement: `+ CREATE TABLE tech_book(+ id int NOT NULL,+ name varchar(220) NOT NULL,+ creator_id INT NOT NULL,+ created_ts TIMESTAMP NOT NULL,+ updater_id INT NOT NULL,+ updated_ts TIMESTAMP NOT NULL,+ CONSTRAINT pk_user_id PRIMARY KEY (id)+ );+ ` ++ "DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')",result: []api.TaskCheckResult{{Status: api.TaskCheckStatusSuccess,