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: support diff foreign key (#3023)
h3n4l8ddeecf11a2d
diff --git a/plugin/parser/differ/mysql/index_test.go b/plugin/parser/differ/mysql/constraint_test.gosimilarity index 79%rename from plugin/parser/differ/mysql/index_test.gorename to plugin/parser/differ/mysql/constraint_test.goindex 92e930f604..e80a586f21 100644--- a/plugin/parser/differ/mysql/index_test.go+++ b/plugin/parser/differ/mysql/constraint_test.go@@ -233,54 +233,6 @@ func TestIsIndexOptionEqual(t *testing.T) {}}-func TestGetIndexName(t *testing.T) {- tests := []struct {- constraint *ast.Constraint- want string- }{- {- constraint: &ast.Constraint{- Name: "id_idx",- Keys: []*ast.IndexPartSpecification{- {- Column: &ast.ColumnName{- Name: model.NewCIStr("id"),- },- },- {- Column: &ast.ColumnName{- Name: model.NewCIStr("name"),- },- },- },- },- want: "id_idx",- },- {- constraint: &ast.Constraint{- Keys: []*ast.IndexPartSpecification{- {- Column: &ast.ColumnName{- Name: model.NewCIStr("id"),- },- },- {- Column: &ast.ColumnName{- Name: model.NewCIStr("name"),- },- },- },- },- want: "id",- },- }- a := require.New(t)- for _, test := range tests {- got := getIndexName(test.constraint)- a.Equalf(test.want, got, "constraint: %v", test.constraint)- }-}-func TestIndexType(t *testing.T) {tests := []struct {old string@@ -468,7 +420,96 @@ func TestKeyPart(t *testing.T) {}}-func TestIndex(t *testing.T) {+func TestForeignKeyDefination(t *testing.T) {+ tests := []struct {+ old string+ new string+ want string+ }{+ {+ old: `CREATE TABLE department(id INT, name VARCHAR(50) NOT NULL, PRIMARY KEY(department));+ CREATE TABLE employee(id INT, name VARCHAR(50) NOT NULL, department_id INT, PRIMARY KEY(id), FOREIGN KEY employee_ibfk_1(department_id) REFERENCES department(id));`,+ new: `CREATE TABLE department(id INT, name VARCHAR(50) NOT NULL, PRIMARY KEY(department));+ CREATE TABLE employee(id INT, name VARCHAR(50) NOT NULL, department_id INT, PRIMARY KEY(id), FOREIGN KEY fk_2(department_id) REFERENCES department(id));`,+ want: "ALTER TABLE `employee` ADD CONSTRAINT `fk_2` FOREIGN KEY (`department_id`) REFERENCES `department`(`id`);\nALTER TABLE `employee` DROP FOREIGN KEY `employee_ibfk_1`;\n",+ },+ {+ old: "CREATE TABLE `department` (" ++ " `id` int NOT NULL," ++ " `name` varchar(50) NOT NULL," ++ " PRIMARY KEY (`id`)," ++ " KEY `id_name_idx` (`id`,`name`)" ++ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;" +++ "CREATE TABLE `employee` (" ++ " `id` int NOT NULL," ++ " `name` varchar(50) NOT NULL," ++ " `department_id` int DEFAULT NULL," ++ " `department_name` varchar(50) DEFAULT NULL," ++ " PRIMARY KEY (`id`)," ++ " KEY `department_id_name_idx` (`department_id`,`department_name`)," ++ " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`, `department_name`) REFERENCES `department` (`id`, `name`)" ++ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",++ new: "CREATE TABLE `department` (" ++ " `id` int NOT NULL," ++ " `name` varchar(50) NOT NULL," ++ " PRIMARY KEY (`id`)," ++ " KEY `id_idx` (`id`)" ++ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;" +++ "CREATE TABLE `employee` (" ++ " `id` int NOT NULL," ++ " `name` varchar(50) NOT NULL," ++ " `department_id` int DEFAULT NULL," ++ " `department_name` varchar(50) DEFAULT NULL," ++ " PRIMARY KEY (`id`)," ++ " KEY `department_id_idx` (`department_id`)," ++ " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`)" ++ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",++ want: "ALTER TABLE `department` ADD INDEX `id_idx`(`id`);\n" ++ "ALTER TABLE `employee` ADD INDEX `department_id_idx`(`department_id`);\n" ++ "ALTER TABLE `employee` DROP FOREIGN KEY `employee_ibfk_1`;\n" ++ "ALTER TABLE `employee` ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `department`(`id`);\n" ++ "ALTER TABLE `employee` DROP INDEX `department_id_name_idx`;\n" ++ "ALTER TABLE `department` DROP INDEX `id_name_idx`;\n",+ },+ // Reference itself.+ {+ old: "CREATE TABLE `employeee` (" ++ " `id` int NOT NULL," ++ " `name` varchar(50) NOT NULL," ++ " `leader_id` int DEFAULT NULL," ++ " PRIMARY KEY (`id`)," ++ " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`leader_id`) REFERENCES `employeee` (`id`)" ++ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",++ new: "CREATE TABLE `employeee` (" ++ " `id` int NOT NULL," ++ " `name` varchar(50) NOT NULL," ++ " `leader_id` int DEFAULT NULL," ++ " `manager_id` int DEFAULT NULL," ++ " PRIMARY KEY (`id`)," ++ " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`manager_id`) REFERENCES `employeee` (`id`)" ++ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",++ want: "ALTER TABLE `employeee` ADD COLUMN (`manager_id` INT DEFAULT NULL);\n" ++ "ALTER TABLE `employeee` DROP FOREIGN KEY `employee_ibfk_1`;\n" ++ "ALTER TABLE `employeee` ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`manager_id`) REFERENCES `employeee`(`id`);\n",+ },+ }++ a := require.New(t)+ mysqlDiffer := &SchemaDiffer{}+ for _, test := range tests {+ out, err := mysqlDiffer.SchemaDiff(test.old, test.new)+ a.NoError(err)+ a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)+ }+}++func TestConstraint(t *testing.T) {tests := []struct {old stringnew stringdiff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.goindex 8f0588e62b..a3fb6fad13 100644--- a/plugin/parser/differ/mysql/differ.go+++ b/plugin/parser/differ/mysql/differ.go@@ -36,6 +36,7 @@ type SchemaDiffer struct {type constraintMap map[string]*ast.Constraint// SchemaDiff returns the schema diff.+// It only supports schema information from mysqldump.func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {oldNodes, _, err := parser.New().Parse(oldStmt, "", "")if err != nil {@@ -46,10 +47,6 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {return "", errors.Wrapf(err, "failed to parse new statement %q", newStmt)}- if err := validateStmtNodes(newNodes); err != nil {- return "", errors.Wrapf(err, "failed to validate the statement %q", newStmt)- }-var newNodeList []ast.Nodevar inplaceUpdate []ast.Node// inplaceDropNodeList and inplaceAddNodeList are used to handle destructive node updates.@@ -74,7 +71,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {if alterTableOptionStmt := diffTableOptions(newStmt.Table, oldStmt.Options, newStmt.Options); alterTableOptionStmt != nil {inplaceUpdate = append(inplaceUpdate, alterTableOptionStmt)}- indexMap := buildIndexMap(oldStmt)+ constraintMap := buildConstraintMap(oldStmt)var alterTableAddColumnSpecs []*ast.AlterTableSpecvar alterTableModifyColumnSpecs []*ast.AlterTableSpecvar alterTableAddNewConstraintSpecs []*ast.AlterTableSpec@@ -106,8 +103,8 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {for _, constraint := range newStmt.Constraints {switch constraint.Tp {case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext:- indexName := getIndexName(constraint)- if oldConstraint, ok := indexMap[indexName]; ok {+ indexName := constraint.Name+ if oldConstraint, ok := constraintMap[indexName]; ok {if !isIndexEqual(constraint, oldConstraint) {alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableDropIndex,@@ -118,7 +115,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {Constraint: constraint,})}- delete(indexMap, indexName)+ delete(constraintMap, indexName)continue}alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{@@ -127,18 +124,40 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {})case ast.ConstraintPrimaryKey:primaryKeyName := "PRIMARY"- if oldConstraint, ok := indexMap[primaryKeyName]; ok {+ if oldConstraint, ok := constraintMap[primaryKeyName]; ok {if !isIndexEqual(constraint, oldConstraint) {alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{- Tp: ast.AlterTableDropPrimaryKey,- Constraint: oldConstraint,+ Tp: ast.AlterTableDropPrimaryKey,+ })+ alterTableInplaceAddConstraintSpecs = append(alterTableInplaceAddConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableAddConstraint,+ Constraint: constraint,+ })+ }+ delete(constraintMap, primaryKeyName)+ continue+ }+ alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableAddConstraint,+ Constraint: constraint,+ })+ // The parent column in the foreign key always needs an index, so in the case of referencing itself,+ // we need to drop the foreign key before dropping the primary key.+ // Since the mysqldump statement always puts the primary key in front of the foreign key, and we will reverse the drop statements order.+ // TODO(zp): So we don't have to worry about this now until one of the statements doesn't come from mysqldump.+ case ast.ConstraintForeignKey:+ if oldConstraint, ok := constraintMap[constraint.Name]; ok {+ if !isForeignKeyConstraintEqual(constraint, oldConstraint) {+ alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableDropForeignKey,+ Name: constraint.Name,})alterTableInplaceAddConstraintSpecs = append(alterTableInplaceAddConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableAddConstraint,Constraint: constraint,})}- delete(indexMap, primaryKeyName)+ delete(constraintMap, constraint.Name)continue}alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{@@ -164,17 +183,23 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {})}// We should drop the remaining indices in the indexMap.- for indexName := range indexMap {- if indexName == "PRIMARY" {+ for indexName, constraint := range constraintMap {+ switch constraint.Tp {+ case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext:+ alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableDropIndex,+ Name: indexName,+ })+ case ast.ConstraintPrimaryKey:alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{Tp: ast.AlterTableDropPrimaryKey,})- continue+ case ast.ConstraintForeignKey:+ alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{+ Tp: ast.AlterTableDropForeignKey,+ Name: constraint.Name,+ })}- alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{- Tp: ast.AlterTableDropIndex,- Name: indexName,- })}if len(alterTableAddNewConstraintSpecs) > 0 {@@ -215,31 +240,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {default:}}-return deparse(newNodeList, inplaceUpdate, inplaceAddNodeList, inplaceDropNodeList, dropNodeList, format.DefaultRestoreFlags|format.RestoreStringWithoutCharset)}-// We're not supporting all the MySQL statements for now.-// validateStmtNodes validates the stmt nodes, return nil if they are valid.-func validateStmtNodes(nodes []ast.StmtNode) error {- // TODO(zp): validate more statements.- for _, node := range nodes {- switch stmt := node.(type) {- case *ast.CreateTableStmt:- for _, columnDef := range stmt.Cols {- for _, option := range columnDef.Options {- if option.Tp == ast.ColumnOptionPrimaryKey {- return errors.New("unsupported column inline PK in CREATE TABLE statement likes `CREATE TABLE tbl(id INT PARIMARY KEY);`")- }- }- }- default:- return errors.Errorf("unsupported statement: %T", node)- }- }- return nil-}-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:@@ -327,17 +330,14 @@ func buildColumnMap(nodes []ast.StmtNode, tableName model.CIStr) map[string]*astreturn oldColumnMap}-// buildIndexMap build a map of index name to constraint on given table name.-func buildIndexMap(stmt *ast.CreateTableStmt) constraintMap {+// buildConstraintMap build a map of index name to constraint on given table name.+func buildConstraintMap(stmt *ast.CreateTableStmt) constraintMap {indexMap := make(constraintMap)for _, constraint := range stmt.Constraints {- if constraint.Tp == ast.ConstraintIndex || constraint.Tp == ast.ConstraintKey ||- constraint.Tp == ast.ConstraintUniq || constraint.Tp == ast.ConstraintUniqKey ||- constraint.Tp == ast.ConstraintUniqIndex || constraint.Tp == ast.ConstraintFulltext {- indexName := getIndexName(constraint)- indexMap[indexName] = constraint- }- if constraint.Tp == ast.ConstraintPrimaryKey {+ switch constraint.Tp {+ case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext, ast.ConstraintForeignKey:+ indexMap[constraint.Name] = constraint+ case ast.ConstraintPrimaryKey:// A table can have only one PRIMARY KEY.// The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.// https://dev.mysql.com/doc/refman/8.0/en/create-table.html@@ -510,15 +510,61 @@ func isIndexOptionEqual(old, new *ast.IndexOption) bool {return true}-// getIndexName returns the name of the index.-func getIndexName(index *ast.Constraint) string {- if index.Name != "" {- return index.Name+// isForeignKeyEqual returns true if two foreign keys are the same.+func isForeignKeyConstraintEqual(old, new *ast.Constraint) bool {+ // FOREIGN KEY [index_name] (index_col_name,...) reference_definition+ if old.Name != new.Name {+ return false+ }+ if !isKeyPartEqual(old.Keys, new.Keys) {+ return false+ }+ if !isReferenceDefinitionEqual(old.Refer, new.Refer) {+ return false+ }+ return true+}++// isReferenceDefinitionEqual returns true if two reference definitions are the same.+func isReferenceDefinitionEqual(old, new *ast.ReferenceDef) bool {+ // reference_definition:+ // REFERENCES tbl_name (index_col_name,...)+ // [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]+ // [ON DELETE reference_option]+ // [ON UPDATE reference_option]+ if old.Table.Name.String() != new.Table.Name.String() {+ return false+ }++ if len(old.IndexPartSpecifications) != len(new.IndexPartSpecifications) {+ return false+ }+ for idx, oldIndexColName := range old.IndexPartSpecifications {+ newIndexColName := new.IndexPartSpecifications[idx]+ if oldIndexColName.Column.Name.String() != newIndexColName.Column.Name.String() {+ return false+ }+ }++ if old.Match != new.Match {+ return false+ }++ if (old.OnDelete == nil) != (new.OnDelete == nil) {+ return false+ }+ if old.OnDelete != nil && new.OnDelete != nil && old.OnDelete.ReferOpt != new.OnDelete.ReferOpt {+ return false+ }++ if (old.OnUpdate == nil) != (new.OnUpdate == nil) {+ return false}- // If the index name is empty, it will be generated by MySQL.- // https://dba.stackexchange.com/questions/160708/is-naming-an-index-required- // TODO(zp): handle the duplicated index name.- return index.Keys[0].Column.Name.O+ if old.OnUpdate != nil && new.OnUpdate != nil && old.OnUpdate.ReferOpt != new.OnUpdate.ReferOpt {+ return false+ }++ return true}// diffTableOptions returns the diff of two table options, returns nil if they are the same.