Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(mysqlv2): suuport alter index (#9448)
zll600d8809a9ca368
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yamlindex f5ca535e86..5700339a59 100644--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml@@ -1,3 +1,97 @@+- statement: |-+ CREATE TABLE t1(+ a INT PRIMARY KEY,+ b INT,+ UNIQUE uk1 (b)+ );+ ALTER TABLE t1 DROP PRIMARY KEY;+ ALTER TABLE t1 DROP INDEX uk1;+ ignore_case_sensitive: false+ want: |-+ {+ "name": "test",+ "schemas": [+ {+ "tables": [+ {+ "name": "t1",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "type": "int"+ },+ {+ "name": "b",+ "position": 2,+ "nullable": true,+ "type": "int"+ }+ ]+ }+ ]+ }+ ]+ }+ err: null+- statement: |-+ CREATE TABLE t1(+ a INT,+ b INT,+ UNIQUE uk1 (b)+ );+ ALTER TABLE t1 RENAME INDEX uk1 TO uk_b;+ ALTER TABLE t1 ADD INDEX idx1 (b);+ ALTER TABLE t1 RENAME INDEX idx1 TO idx2;+ ALTER TABLE t1 ALTER INDEX idx2 INVISIBLE;+ ALTER TABLE t1 RENAME TO t2;+ ignore_case_sensitive: false+ want: |-+ {+ "name": "test",+ "schemas": [+ {+ "tables": [+ {+ "name": "t2",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "int"+ },+ {+ "name": "b",+ "position": 2,+ "nullable": true,+ "type": "int"+ }+ ],+ "indexes": [+ {+ "name": "idx2",+ "expressions": [+ "b"+ ],+ "type": "BTREE"+ },+ {+ "name": "uk_b",+ "expressions": [+ "b"+ ],+ "type": "BTREE",+ "unique": true,+ "visible": true+ }+ ]+ }+ ]+ }+ ]+ }+ err: null- statement: |-CREATE TABLE t1(a INT NOT NULL);ALTER TABLE t1 ALTER COLUMN a SET DEFAULT NULL;diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.goindex 1d2289350d..765292c05f 100644--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go@@ -253,7 +253,10 @@ func (l *mysqlV2Listener) EnterAlterTable(ctx *mysql.AlterTableContext) {}// add constraint.case item.TableConstraintDef() != nil:- // todo+ if err := table.mysqlV2CreateConstraint(l.databaseState.ctx, item.TableConstraintDef()); err != nil {+ l.err = err+ return+ }}// drop column.case item.DROP_SYMBOL() != nil && item.ALTER_SYMBOL() == nil:@@ -267,10 +270,20 @@ func (l *mysqlV2Listener) EnterAlterTable(ctx *mysql.AlterTableContext) {}// drop primary key.case item.PRIMARY_SYMBOL() != nil && item.KEY_SYMBOL() != nil:+ if err := table.dropIndex(l.databaseState.ctx, PrimaryKeyName); err != nil {+ l.err = err+ return+ }// drop key/index.case item.KeyOrIndex() != nil && item.IndexRef() != nil:+ _, _, indexName := mysqlparser.NormalizeIndexRef(item.IndexRef())+ if err := table.dropIndex(l.databaseState.ctx, indexName); err != nil {+ l.err = err+ return+ }// drop foreign key.case item.FOREIGN_SYMBOL() != nil && item.KEY_SYMBOL() != nil:+ // we do not deal with DROP FOREIGN KEY statements.}// modify column.case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil:@@ -303,16 +316,52 @@ func (l *mysqlV2Listener) EnterAlterTable(ctx *mysql.AlterTableContext) {l.err = errreturn}+ // alter index visibility.+ case item.INDEX_SYMBOL() != nil && item.IndexRef() != nil && item.Visibility() != nil:+ _, _, indexName := mysqlparser.NormalizeIndexRef(item.IndexRef())+ if err := table.mysqlV2ChangeIndexVisibility(l.databaseState.ctx, indexName, item.Visibility()); err != nil {+ l.err = err+ return+ }default:}// rename table.case item.RENAME_SYMBOL() != nil && item.TableName() != nil:+ _, newTableName := mysqlparser.NormalizeMySQLTableName(item.TableName())+ schema := l.databaseState.schemaSet[""]+ if err := schema.renameTable(l.databaseState.ctx, table.name, newTableName); err != nil {+ l.err = err+ return+ }// rename index.- case item.RENAME_SYMBOL() != nil && item.KeyOrIndex() != nil:+ case item.RENAME_SYMBOL() != nil && item.KeyOrIndex() != nil && item.IndexRef() != nil && item.IndexName() != nil:+ _, _, oldIndexName := mysqlparser.NormalizeIndexRef(item.IndexRef())+ newIndexName := mysqlparser.NormalizeIndexName(item.IndexName())+ if err := table.renameIndex(l.databaseState.ctx, oldIndexName, newIndexName); err != nil {+ l.err = err+ return+ }}}}+func (t *TableState) mysqlV2ChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {+ index, exists := t.indexSet[indexName]+ if !exists {+ if ctx.CheckIntegrity {+ return NewIndexNotExistsError(t.name, indexName)+ }+ index = t.createIncompleteIndex(indexName)+ }+ switch {+ case visibility.VISIBLE_SYMBOL() != nil:+ index.visible = newTruePointer()+ case visibility.INVISIBLE_SYMBOL() != nil:+ index.visible = newFalsePointer()+ }+ return nil+}+func (t *TableState) mysqlV2AlterColumn(ctx *FinderContext, itemDef mysql.IAlterListItemContext) *WalkThroughError {if itemDef.ColumnInternalRef() == nil {// should not reach here.