Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(SBM): update same name index (#3011)
h3n4l25701d31f245
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.goindex d9188565f5..8f0588e62b 100644--- a/plugin/parser/differ/mysql/differ.go+++ b/plugin/parser/differ/mysql/differ.go@@ -52,7 +52,11 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {var newNodeList []ast.Nodevar inplaceUpdate []ast.Node- var dropNodeList [][]ast.Node+ // inplaceDropNodeList and inplaceAddNodeList are used to handle destructive node updates.+ // For example, we should drop the old index named 'id_idx' and then add a new index named 'id_idx' in the same table.+ var inplaceDropNodeList []ast.Node+ var inplaceAddNodeList []ast.Node+ var dropNodeList []ast.NodeoldTableMap := buildTableMap(oldNodes)@@ -73,8 +77,10 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {indexMap := buildIndexMap(oldStmt)var alterTableAddColumnSpecs []*ast.AlterTableSpecvar alterTableModifyColumnSpecs []*ast.AlterTableSpec- var alterTableAddConstraintSpecs []*ast.AlterTableSpec- var alterTableDropConstraintSpecs []*ast.AlterTableSpec+ var alterTableAddNewConstraintSpecs []*ast.AlterTableSpec+ var alterTableDropExcessConstraintSpecs []*ast.AlterTableSpec+ var alterTableInplaceAddConstraintSpecs []*ast.AlterTableSpec+ var alterTableInplaceDropConstraintSpecs []*ast.AlterTableSpecoldColumnMap := buildColumnMap(oldNodes, newStmt.Table.Name)for _, columnDef := range newStmt.Cols {@@ -102,23 +108,40 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext:indexName := getIndexName(constraint)if oldConstraint, ok := indexMap[indexName]; ok {- if isIndexEqual(constraint, oldConstraint) {- delete(indexMap, indexName)- continue+ if !isIndexEqual(constraint, oldConstraint) {+ alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableDropIndex,+ Name: indexName,+ })+ alterTableInplaceAddConstraintSpecs = append(alterTableInplaceAddConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableAddConstraint,+ Constraint: constraint,+ })}+ delete(indexMap, indexName)+ continue}- alterTableAddConstraintSpecs = append(alterTableAddConstraintSpecs, &ast.AlterTableSpec{+ alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableAddConstraint,Constraint: constraint,})case ast.ConstraintPrimaryKey:- if oldConstraint, ok := indexMap["PRIMARY"]; ok {- if isIndexEqual(constraint, oldConstraint) {- delete(indexMap, "PRIMARY")- continue+ primaryKeyName := "PRIMARY"+ if oldConstraint, ok := indexMap[primaryKeyName]; ok {+ if !isIndexEqual(constraint, oldConstraint) {+ alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableDropPrimaryKey,+ Constraint: oldConstraint,+ })+ alterTableInplaceAddConstraintSpecs = append(alterTableInplaceAddConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableAddConstraint,+ Constraint: constraint,+ })}+ delete(indexMap, primaryKeyName)+ continue}- alterTableAddConstraintSpecs = append(alterTableAddConstraintSpecs, &ast.AlterTableSpec{+ alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableAddConstraint,Constraint: constraint,})@@ -143,41 +166,57 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {// We should drop the remaining indices in the indexMap.for indexName := range indexMap {if indexName == "PRIMARY" {- alterTableDropConstraintSpecs = append(alterTableDropConstraintSpecs, &ast.AlterTableSpec{+ alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableDropPrimaryKey,})continue}- alterTableDropConstraintSpecs = append(alterTableDropConstraintSpecs, &ast.AlterTableSpec{+ alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableDropIndex,Name: indexName,})}- if len(alterTableAddConstraintSpecs) > 0 {+ if len(alterTableAddNewConstraintSpecs) > 0 {newNodeList = append(newNodeList, &ast.AlterTableStmt{Table: &ast.TableName{Name: model.NewCIStr(tableName),},- Specs: alterTableDropConstraintSpecs,+ Specs: alterTableAddNewConstraintSpecs,})}- if len(alterTableDropConstraintSpecs) > 0 {- dropNodeList = append(dropNodeList, []ast.Node{- &ast.AlterTableStmt{- Table: &ast.TableName{- Name: model.NewCIStr(tableName),- },- Specs: alterTableAddConstraintSpecs,+ if len(alterTableDropExcessConstraintSpecs) > 0 {+ dropNodeList = append(dropNodeList, &ast.AlterTableStmt{+ Table: &ast.TableName{+ Name: model.NewCIStr(tableName),},+ Specs: alterTableDropExcessConstraintSpecs,+ })+ }++ if len(alterTableInplaceDropConstraintSpecs) > 0 {+ inplaceDropNodeList = append(inplaceDropNodeList, &ast.AlterTableStmt{+ Table: &ast.TableName{+ Name: model.NewCIStr(tableName),+ },+ Specs: alterTableInplaceDropConstraintSpecs,+ })+ }++ if len(alterTableInplaceAddConstraintSpecs) > 0 {+ inplaceAddNodeList = append(inplaceAddNodeList, &ast.AlterTableStmt{+ Table: &ast.TableName{+ Name: model.NewCIStr(tableName),+ },+ Specs: alterTableInplaceAddConstraintSpecs,})}default:}}- return deparse(newNodeList, inplaceUpdate, dropNodeList, format.DefaultRestoreFlags|format.RestoreStringWithoutCharset)+ return deparse(newNodeList, inplaceUpdate, inplaceAddNodeList, inplaceDropNodeList, dropNodeList, format.DefaultRestoreFlags|format.RestoreStringWithoutCharset)}// We're not supporting all the MySQL statements for now.@@ -201,7 +240,7 @@ func validateStmtNodes(nodes []ast.StmtNode) error {return nil}-func deparse(newNodeList []ast.Node, inplaceUpdate []ast.Node, dropNodeList [][]ast.Node, flag format.RestoreFlags) (string, error) {+func deparse(newNodeList []ast.Node, inplaceUpdate []ast.Node, inplaceAdd []ast.Node, inplaceDrop []ast.Node, dropNodeList []ast.Node, flag format.RestoreFlags) (string, error) {var buf bytes.Buffer// We should following the right order to avoid break the dependency:// Additions for new nodes.@@ -227,22 +266,30 @@ func deparse(newNodeList []ast.Node, inplaceUpdate []ast.Node, dropNodeList [][]}}- var reDropNodes [][]ast.Node- for i := len(dropNodeList) - 1; i >= 0; i-- {- var nodes []ast.Node- for j := len(dropNodeList[i]) - 1; j >= 0; j-- {- nodes = append(nodes, dropNodeList[i][j])+ for i := len(inplaceDrop) - 1; i >= 0; i-- {+ if err := inplaceDrop[i].Restore(format.NewRestoreCtx(flag, &buf)); err != nil {+ return "", err+ }+ if _, err := buf.Write([]byte(";\n")); err != nil {+ return "", err}- reDropNodes = append(reDropNodes, nodes)}- for _, nodes := range reDropNodes {- for _, node := range nodes {- if err := node.Restore(format.NewRestoreCtx(flag, &buf)); err != nil {- return "", err- }- if _, err := buf.Write([]byte(";\n")); err != nil {- return "", err- }++ for i := len(inplaceAdd) - 1; i >= 0; i-- {+ if err := inplaceAdd[i].Restore(format.NewRestoreCtx(flag, &buf)); err != nil {+ return "", err+ }+ if _, err := buf.Write([]byte(";\n")); err != nil {+ return "", err+ }+ }++ for i := len(dropNodeList) - 1; i >= 0; i-- {+ if err := dropNodeList[i].Restore(format.NewRestoreCtx(flag, &buf)); err != nil {+ return "", err+ }+ if _, err := buf.Write([]byte(";\n")); err != nil {+ return "", err}}return buf.String(), nildiff --git a/plugin/parser/differ/mysql/index_test.go b/plugin/parser/differ/mysql/index_test.goindex cd06b09fc7..92e930f604 100644--- a/plugin/parser/differ/mysql/index_test.go+++ b/plugin/parser/differ/mysql/index_test.go@@ -164,6 +164,7 @@ func TestIsKeyPartEqual(t *testing.T) {a.Equalf(test.eq, got, "old: %v, new: %v", test.old, test.new)}}+func TestIsIndexOptionEqual(t *testing.T) {tests := []struct {old *ast.IndexOption@@ -481,13 +482,21 @@ func TestIndex(t *testing.T) {"ALTER TABLE `book` DROP PRIMARY KEY;\n" +"ALTER TABLE `book` ADD PRIMARY KEY(`id`, `address`);\n",},- // ADD COLUMN -> DROP INDEX -> ADD INDEX+ // ADD COLUMN -> ADD INDEX WITH ANOTHER NAME-> DROP INDEX{old: `CREATE TABLE book(id INT, name VARCHAR(50), INDEX id_name_idx (id, name));`,new: `CREATE TABLE book(id INT, name VARCHAR(50), address VARCHAR(50) NOT NULL, INDEX id_address_idx (id, address));`,want: "ALTER TABLE `book` ADD COLUMN (`address` VARCHAR(50) NOT NULL);\n" +- "ALTER TABLE `book` DROP INDEX `id_name_idx`;\n" +- "ALTER TABLE `book` ADD INDEX `id_address_idx`(`id`, `address`);\n",+ "ALTER TABLE `book` ADD INDEX `id_address_idx`(`id`, `address`);\n" ++ "ALTER TABLE `book` DROP INDEX `id_name_idx`;\n",+ },+ // ADD COLUMN -> ADD INDEX WITH SAME NAME -> DROP INDEX+ {+ old: `CREATE TABLE book(id INT, name VARCHAR(50), INDEX idx (id, name));`,+ new: `CREATE TABLE book(id INT, name VARCHAR(50), address VARCHAR(50) NOT NULL, INDEX idx (id, address));`,+ want: "ALTER TABLE `book` ADD COLUMN (`address` VARCHAR(50) NOT NULL);\n" ++ "ALTER TABLE `book` DROP INDEX `idx`;\n" ++ "ALTER TABLE `book` ADD INDEX `idx`(`id`, `address`);\n",},}a := require.New(t)