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): walk-through support some MySQL statements (#2679)
rebelice9d15d9c2ed6d
diff --git a/plugin/advisor/catalog/state.go b/plugin/advisor/catalog/state.goindex 07d173654c..6897bd84d3 100644--- a/plugin/advisor/catalog/state.go+++ b/plugin/advisor/catalog/state.go@@ -35,7 +35,7 @@ func newSchemaState(s *Schema, context *FinderContext) *schemaState {}for _, table := range s.TableList {- schema.tableSet[table.Name] = newTableState(table, context)+ schema.tableSet[table.Name] = newTableState(table)}for _, view := range s.ViewList {@@ -65,7 +65,7 @@ func newExtensionState(e *Extension) *extensionState {}}-func newTableState(t *Table, context *FinderContext) *tableState {+func newTableState(t *Table) *tableState {table := &tableState{name: t.Name,tableType: t.Type,@@ -79,7 +79,6 @@ func newTableState(t *Table, context *FinderContext) *tableState {comment: t.Comment,columnSet: make(columnStateMap),indexSet: make(indexStateMap),- context: context.Copy(),}for _, column := range t.ColumnList {@@ -125,6 +124,7 @@ type databaseState struct {collation stringdbType db.TypeschemaSet schemaStateMap+ deleted boolcontext *FinderContext}@@ -159,8 +159,6 @@ type tableState struct {columnSet columnStateMap// indexSet isn't supported for ClickHouse, Snowflake.indexSet indexStateMap-- context *FinderContext}type tableStateMap map[string]*tableState@@ -181,6 +179,10 @@ func (table *tableState) convertToCatalog() *Table {}}+func (table *tableState) copy() *tableState {+ return newTableState(table.convertToCatalog())+}+type indexState struct {name string// This could refer to a column or an expression.diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.goindex 0ea33fb3e0..b2f8124bc5 100644--- a/plugin/advisor/catalog/walk_through.go+++ b/plugin/advisor/catalog/walk_through.go@@ -38,6 +38,8 @@ const (// ErrorTypeAccessOtherDatabase is the error that try to access other database.ErrorTypeAccessOtherDatabase = 201+ // ErrorTypeDatabaseIsDeleted is the error that try to access the deleted database.+ ErrorTypeDatabaseIsDeleted = 202// 301 ~ 399 table error type.@@ -127,6 +129,14 @@ func NewTableNotExistsError(tableName string) *WalkThroughError {}}+// NewTableExistsError returns a new ErrorTypeTableExists.+func NewTableExistsError(tableName string) *WalkThroughError {+ return &WalkThroughError{+ Type: ErrorTypeTableExists,+ Content: fmt.Sprintf("Table `%s` already exists", tableName),+ }+}+// Error implements the error interface.func (e *WalkThroughError) Error() string {return e.Content@@ -163,6 +173,12 @@ func (d *databaseState) WalkThrough(stmts string) error {}func (d *databaseState) changeState(in tidbast.StmtNode) error {+ if d.deleted {+ return &WalkThroughError{+ Type: ErrorTypeDatabaseIsDeleted,+ Content: fmt.Sprintf("Database `%s` is deleted", d.name),+ }+ }switch node := in.(type) {case *tidbast.CreateTableStmt:return d.createTable(node)@@ -174,11 +190,98 @@ func (d *databaseState) changeState(in tidbast.StmtNode) error {return d.createIndex(node)case *tidbast.DropIndexStmt:return d.dropIndex(node)+ case *tidbast.AlterDatabaseStmt:+ return d.alterDatabase(node)+ case *tidbast.DropDatabaseStmt:+ return d.dropDatabase(node)+ case *tidbast.CreateDatabaseStmt:+ return NewAccessOtherDatabaseError(d.name, node.Name)+ case *tidbast.RenameTableStmt:+ return d.renameTable(node)default:return nil}}+func (d *databaseState) renameTable(node *tidbast.RenameTableStmt) error {+ for _, tableToTable := range node.TableToTables {+ schema, exists := d.schemaSet[""]+ if !exists {+ schema = d.createSchema("")+ }+ if d.theCurrentDatabase(tableToTable) {+ table, exists := schema.tableSet[tableToTable.OldTable.Name.O]+ if !exists {+ return NewTableNotExistsError(tableToTable.OldTable.Name.O)+ }+ if _, exists := schema.tableSet[tableToTable.NewTable.Name.O]; exists {+ return NewTableExistsError(tableToTable.NewTable.Name.O)+ }+ delete(schema.tableSet, table.name)+ table.name = tableToTable.NewTable.Name.O+ schema.tableSet[table.name] = table+ } else if d.moveToOtherDatabase(tableToTable) {+ _, exists := schema.tableSet[tableToTable.OldTable.Name.O]+ if !exists {+ return NewTableNotExistsError(tableToTable.OldTable.Name.O)+ }+ delete(schema.tableSet, tableToTable.OldTable.Name.O)+ } else {+ return NewAccessOtherDatabaseError(d.name, d.targetDatabase(tableToTable))+ }+ }+ return nil+}++func (d *databaseState) targetDatabase(node *tidbast.TableToTable) string {+ if node.OldTable.Schema.O != "" && node.OldTable.Schema.O != d.name {+ return node.OldTable.Schema.O+ }+ return node.NewTable.Schema.O+}++func (d *databaseState) moveToOtherDatabase(node *tidbast.TableToTable) bool {+ if node.OldTable.Schema.O != "" && node.OldTable.Schema.O != d.name {+ return false+ }+ return node.OldTable.Schema.O != node.NewTable.Schema.O+}++func (d *databaseState) theCurrentDatabase(node *tidbast.TableToTable) bool {+ if node.NewTable.Schema.O != "" && node.NewTable.Schema.O != d.name {+ return false+ }+ if node.OldTable.Schema.O != "" && node.OldTable.Schema.O != d.name {+ return false+ }+ return true+}++func (d *databaseState) dropDatabase(node *tidbast.DropDatabaseStmt) error {+ if node.Name != d.name {+ return NewAccessOtherDatabaseError(d.name, node.Name)+ }++ d.deleted = true+ return nil+}++func (d *databaseState) alterDatabase(node *tidbast.AlterDatabaseStmt) error {+ if !node.AlterDefaultDatabase && node.Name != d.name {+ return NewAccessOtherDatabaseError(d.name, node.Name)+ }++ for _, option := range node.Options {+ switch option.Tp {+ case tidbast.DatabaseOptionCharset:+ d.characterSet = option.Value+ case tidbast.DatabaseOptionCollate:+ d.collation = option.Value+ }+ }+ return nil+}+func (d *databaseState) findTable(tableName *tidbast.TableName) (*tableState, error) {if tableName.Schema.O != "" && tableName.Schema.O != d.name {return nil, NewAccessOtherDatabaseError(d.name, tableName.Schema.O)@@ -636,6 +739,23 @@ func (d *databaseState) dropTable(node *tidbast.DropTableStmt) error {return nil}+func (d *databaseState) copyTable(node *tidbast.CreateTableStmt) error {+ targetTable, err := d.findTable(node.ReferTable)+ if err != nil {+ return err+ }+ if targetTable == nil {+ // For CREATE TABLE ... LIKE ... statements, we can not walk-through if target table dese not exist in catalog.+ return NewTableNotExistsError(node.ReferTable.Name.O)+ }++ schema := d.schemaSet[""]+ table := targetTable.copy()+ table.name = node.Table.Name.O+ schema.tableSet[table.name] = table+ return nil+}+func (d *databaseState) createTable(node *tidbast.CreateTableStmt) error {if node.Table.Schema.O != "" && d.name != node.Table.Schema.O {return &WalkThroughError{@@ -659,11 +779,14 @@ func (d *databaseState) createTable(node *tidbast.CreateTableStmt) error {}}+ if node.ReferTable != nil {+ return d.copyTable(node)+ }+table := &tableState{name: node.Table.Name.O,columnSet: make(columnStateMap),indexSet: make(indexStateMap),- context: &FinderContext{CheckIntegrity: true},}schema.tableSet[table.name] = tablediff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.goindex 2cadb968d2..3bca51d320 100644--- a/plugin/advisor/catalog/walk_through_test.go+++ b/plugin/advisor/catalog/walk_through_test.go@@ -23,6 +23,225 @@ var (func TestWalkThrough(t *testing.T) {tests := []testData{+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ ALTER DATABASE CHARACTER SET = utf8mb4;+ ALTER DATABASE test COLLATE utf8mb4_polish_ci;+ `,+ want: &Database{+ Name: "test",+ DbType: db.MySQL,+ CharacterSet: "utf8mb4",+ Collation: "utf8mb4_polish_ci",+ SchemaList: []*Schema{{}},+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(+ a int PRIMARY KEY DEFAULT 1,+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,+ c int auto_increment NULL COMMENT 'This is a comment',+ d varchar(10) COLLATE utf8mb4_polish_ci,+ KEY idx_a (a),+ INDEX (b, a),+ UNIQUE (b, c, d),+ FULLTEXT (b, d) WITH PARSER ngram INVISIBLE+ );+ CREATE TABLE t_copy like t;+ `,+ want: &Database{+ Name: "test",+ DbType: db.MySQL,+ SchemaList: []*Schema{+ {+ Name: "",+ TableList: []*Table{+ {+ Name: "t_copy",+ ColumnList: []*Column{+ {+ Name: "a",+ Position: 1,+ Default: &one,+ Nullable: false,+ Type: "int(11)",+ },+ {+ Name: "b",+ Position: 2,+ Default: nil,+ Nullable: false,+ Type: "varchar(200)",+ CharacterSet: "utf8mb4",+ },+ {+ Name: "c",+ Position: 3,+ Default: nil,+ Nullable: true,+ Type: "int(11)",+ Comment: "This is a comment",+ },+ {+ Name: "d",+ Position: 4,+ Default: nil,+ Nullable: true,+ Type: "varchar(10)",+ Collation: "utf8mb4_polish_ci",+ },+ },+ IndexList: []*Index{+ {+ Name: "PRIMARY",+ ExpressionList: []string{"a"},+ Type: "BTREE",+ Unique: true,+ Primary: true,+ Visible: true,+ },+ {+ Name: "b",+ ExpressionList: []string{"b"},+ Type: "BTREE",+ Unique: true,+ Primary: false,+ Visible: true,+ },+ {+ Name: "idx_a",+ ExpressionList: []string{"a"},+ Type: "BTREE",+ Unique: false,+ Primary: false,+ Visible: true,+ },+ {+ Name: "b_2",+ ExpressionList: []string{"b", "a"},+ Type: "BTREE",+ Unique: false,+ Primary: false,+ Visible: true,+ },+ {+ Name: "b_3",+ ExpressionList: []string{"b", "c", "d"},+ Type: "BTREE",+ Unique: true,+ Primary: false,+ Visible: true,+ },+ {+ Name: "b_4",+ ExpressionList: []string{"b", "d"},+ Type: "FULLTEXT",+ Unique: false,+ Primary: false,+ Visible: false,+ },+ },+ },+ {+ Name: "t",+ ColumnList: []*Column{+ {+ Name: "a",+ Position: 1,+ Default: &one,+ Nullable: false,+ Type: "int(11)",+ },+ {+ Name: "b",+ Position: 2,+ Default: nil,+ Nullable: false,+ Type: "varchar(200)",+ CharacterSet: "utf8mb4",+ },+ {+ Name: "c",+ Position: 3,+ Default: nil,+ Nullable: true,+ Type: "int(11)",+ Comment: "This is a comment",+ },+ {+ Name: "d",+ Position: 4,+ Default: nil,+ Nullable: true,+ Type: "varchar(10)",+ Collation: "utf8mb4_polish_ci",+ },+ },+ IndexList: []*Index{+ {+ Name: "PRIMARY",+ ExpressionList: []string{"a"},+ Type: "BTREE",+ Unique: true,+ Primary: true,+ Visible: true,+ },+ {+ Name: "b",+ ExpressionList: []string{"b"},+ Type: "BTREE",+ Unique: true,+ Primary: false,+ Visible: true,+ },+ {+ Name: "idx_a",+ ExpressionList: []string{"a"},+ Type: "BTREE",+ Unique: false,+ Primary: false,+ Visible: true,+ },+ {+ Name: "b_2",+ ExpressionList: []string{"b", "a"},+ Type: "BTREE",+ Unique: false,+ Primary: false,+ Visible: true,+ },+ {+ Name: "b_3",+ ExpressionList: []string{"b", "c", "d"},+ Type: "BTREE",+ Unique: true,+ Primary: false,+ Visible: true,+ },+ {+ Name: "b_4",+ ExpressionList: []string{"b", "d"},+ Type: "FULLTEXT",+ Unique: false,+ Primary: false,+ Visible: false,+ },+ },+ },+ },+ },+ },+ },+ },{origin: &Database{Name: "test",@@ -224,6 +443,70 @@ func TestWalkThrough(t *testing.T) {Content: "Table `t1` does not exist",},},+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a int);+ RENAME TABLE t to other_db.t1+ `,+ want: &Database{+ Name: "test",+ DbType: db.MySQL,+ SchemaList: []*Schema{+ {},+ },+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ CREATE TABLE t(a int);+ RENAME TABLE t to test.t1+ `,+ want: &Database{+ Name: "test",+ DbType: db.MySQL,+ SchemaList: []*Schema{+ {+ Name: "",+ TableList: []*Table{+ {+ Name: "t1",+ ColumnList: []*Column{+ {+ Name: "a",+ Position: 1,+ Default: nil,+ Nullable: true,+ Type: "int(11)",+ },+ },+ },+ },+ },+ },+ },+ },+ {+ origin: &Database{+ Name: "test",+ DbType: db.MySQL,+ },+ statement: `+ DROP DATABASE test;+ CREATE TABLE t(a int);+ `,+ err: &WalkThroughError{+ Type: ErrorTypeDatabaseIsDeleted,+ Content: "Database `test` is deleted",+ },+ },{origin: &Database{Name: "test",