Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(SBM): support diff check constraint (#3041)

h3n4l24ce34949720
diff --git a/plugin/parser/differ/mysql/constraint_test.go b/plugin/parser/differ/mysql/constraint_test.go
index e80a586f21..2f07ca3e6e 100644
--- a/plugin/parser/differ/mysql/constraint_test.go
+++ b/plugin/parser/differ/mysql/constraint_test.go
@@ -509,6 +509,41 @@ func TestForeignKeyDefination(t *testing.T) {
}
}
+func TestCheckConstraint(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
+ {
+ old: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0));",
+ new: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 1));",
+ want: "ALTER TABLE `book` DROP CHECK `check_price`;\n" +
+ "ALTER TABLE `book` ADD CONSTRAINT `check_price` CHECK(`price`>1) ENFORCED;\n",
+ },
+ {
+ old: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0));",
+ new: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0) NOT ENFORCED);",
+ want: "ALTER TABLE `book` DROP CHECK `check_price`;\n" +
+ "ALTER TABLE `book` ADD CONSTRAINT `check_price` CHECK(`price`>0) NOT ENFORCED;\n",
+ },
+ {
+ old: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0), CONSTRAINT `check_price2` CHECK(price > 1));",
+ new: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0) NOT ENFORCED);",
+ want: "ALTER TABLE `book` DROP CHECK `check_price`;\n" +
+ "ALTER TABLE `book` ADD CONSTRAINT `check_price` CHECK(`price`>0) NOT ENFORCED;\n" +
+ "ALTER TABLE `book` DROP CHECK `check_price2`;\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 string
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index be3ea916be..879c4344c3 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -76,6 +76,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.AlterTableSpec
var alterTableModifyColumnSpecs []*ast.AlterTableSpec
@@ -109,7 +110,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
switch constraint.Tp {
case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext:
indexName := constraint.Name
- if oldConstraint, ok := constraintMap[indexName]; ok {
+ if oldConstraint, ok := indexMap[indexName]; ok {
if !isIndexEqual(constraint, oldConstraint) {
alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{
Tp: ast.AlterTableDropIndex,
@@ -120,7 +121,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Constraint: constraint,
})
}
- delete(constraintMap, indexName)
+ delete(indexMap, indexName)
continue
}
alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{
@@ -129,7 +130,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
})
case ast.ConstraintPrimaryKey:
primaryKeyName := "PRIMARY"
- if oldConstraint, ok := constraintMap[primaryKeyName]; ok {
+ if oldConstraint, ok := indexMap[primaryKeyName]; ok {
if !isIndexEqual(constraint, oldConstraint) {
alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{
Tp: ast.AlterTableDropPrimaryKey,
@@ -139,7 +140,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Constraint: constraint,
})
}
- delete(constraintMap, primaryKeyName)
+ delete(indexMap, primaryKeyName)
continue
}
alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{
@@ -169,6 +170,25 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Tp: ast.AlterTableAddConstraint,
Constraint: constraint,
})
+ case ast.ConstraintCheck:
+ if oldConstraint, ok := constraintMap[constraint.Name]; ok {
+ if !isCheckConstraintEqual(constraint, oldConstraint) {
+ alterTableInplaceDropConstraintSpecs = append(alterTableInplaceDropConstraintSpecs, &ast.AlterTableSpec{
+ Tp: ast.AlterTableDropCheck,
+ Constraint: constraint,
+ })
+ alterTableInplaceAddConstraintSpecs = append(alterTableInplaceAddConstraintSpecs, &ast.AlterTableSpec{
+ Tp: ast.AlterTableAddConstraint,
+ Constraint: constraint,
+ })
+ }
+ delete(constraintMap, constraint.Name)
+ continue
+ }
+ alterTableAddNewConstraintSpecs = append(alterTableAddNewConstraintSpecs, &ast.AlterTableSpec{
+ Tp: ast.AlterTableAddConstraint,
+ Constraint: constraint,
+ })
}
}
if len(alterTableAddColumnSpecs) > 0 {
@@ -187,8 +207,8 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Specs: alterTableModifyColumnSpecs,
})
}
- // We should drop the remaining indices in the indexMap.
- for indexName, constraint := range constraintMap {
+ // Drop the remaining indices.
+ for indexName, constraint := range indexMap {
switch constraint.Tp {
case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext:
alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{
@@ -199,10 +219,20 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{
Tp: ast.AlterTableDropPrimaryKey,
})
+ }
+ }
+ // Drop the remaining constraints.
+ for constraintName, constraint := range constraintMap {
+ switch constraint.Tp {
case ast.ConstraintForeignKey:
alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{
Tp: ast.AlterTableDropForeignKey,
- Name: constraint.Name,
+ Name: constraintName,
+ })
+ case ast.ConstraintCheck:
+ alterTableDropExcessConstraintSpecs = append(alterTableDropExcessConstraintSpecs, &ast.AlterTableSpec{
+ Tp: ast.AlterTableDropCheck,
+ Constraint: constraint,
})
}
}
@@ -459,12 +489,12 @@ func buildColumnMap(nodes []ast.StmtNode, tableName model.CIStr) map[string]*ast
return oldColumnMap
}
-// buildConstraintMap build a map of index name to constraint on given table name.
-func buildConstraintMap(stmt *ast.CreateTableStmt) constraintMap {
+// buildIndexMap build a map of index name to constraint on given table name.
+func buildIndexMap(stmt *ast.CreateTableStmt) constraintMap {
indexMap := make(constraintMap)
for _, constraint := range stmt.Constraints {
switch constraint.Tp {
- case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext, ast.ConstraintForeignKey:
+ case ast.ConstraintIndex, ast.ConstraintKey, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex, ast.ConstraintFulltext:
indexMap[constraint.Name] = constraint
case ast.ConstraintPrimaryKey:
// A table can have only one PRIMARY KEY.
@@ -477,6 +507,18 @@ func buildConstraintMap(stmt *ast.CreateTableStmt) constraintMap {
return indexMap
}
+func buildConstraintMap(stmt *ast.CreateTableStmt) constraintMap {
+ constraintMap := make(constraintMap)
+ for _, constraint := range stmt.Constraints {
+ switch constraint.Tp {
+ case ast.ConstraintForeignKey, ast.ConstraintCheck:
+ constraintMap[constraint.Name] = constraint
+ default:
+ }
+ }
+ return constraintMap
+}
+
// isColumnEqual returns true if definitions of two columns with the same name are the same.
func isColumnEqual(old, new *ast.ColumnDef) bool {
if !isColumnTypesEqual(old, new) {
@@ -695,6 +737,34 @@ func isReferenceDefinitionEqual(old, new *ast.ReferenceDef) bool {
return true
}
+// isCheckConstraintEqual returns true if two check constraints are the same.
+func isCheckConstraintEqual(old, new *ast.Constraint) bool {
+ // check_constraint_definition:
+ // [CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]
+ if old.Name != new.Name {
+ return false
+ }
+
+ var oldBuf bytes.Buffer
+ var newBuf bytes.Buffer
+ oldRestoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &oldBuf)
+ newRestoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &newBuf)
+ if err := old.Expr.Restore(oldRestoreCtx); err != nil {
+ return false
+ }
+ if err := new.Expr.Restore(newRestoreCtx); err != nil {
+ return false
+ }
+ if oldBuf.String() != newBuf.String() {
+ return false
+ }
+
+ if old.Enforced != new.Enforced {
+ return false
+ }
+ return true
+}
+
// diffTableOptions returns the diff of two table options, returns nil if they are the same.
func diffTableOptions(tableName *ast.TableName, old, new []*ast.TableOption) *ast.AlterTableStmt {
// https://dev.mysql.com/doc/refman/8.0/en/create-table.html