Skip to content

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

fix: diable foreign key checks after diff SQL (#3111)

h3n4l60d4972c8983
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index 716f6c1a25..6af69d14b4 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -43,6 +43,7 @@ const (
)
const (
disableFKCheckStmt string = "SET FOREIGN_KEY_CHECKS=0;\n"
+ enableFKCheckStmt string = "SET FOREIGN_KEY_CHECKS=1;\n"
)
// SchemaDiffer it the parser for MySQL dialect.
@@ -432,7 +433,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
return "", errors.Wrapf(err, "deparse failed")
}
if buf.Len() > 0 {
- return fmt.Sprintf("%s%s", disableFKCheckStmt, buf.String()), nil
+ return fmt.Sprintf("%s%s%s", disableFKCheckStmt, buf.String(), enableFKCheckStmt), nil
}
return "", nil
}
diff --git a/plugin/parser/differ/mysql/differ_test.go b/plugin/parser/differ/mysql/differ_test.go
index ea09b856cb..f7b7441876 100644
--- a/plugin/parser/differ/mysql/differ_test.go
+++ b/plugin/parser/differ/mysql/differ_test.go
@@ -94,6 +94,8 @@ func testDiffWithoutDisableForeignKeyCheck(t *testing.T, testCases []testCase) {
if len(out) > 0 {
a.Equal(disableFKCheckStmt, out[:len(disableFKCheckStmt)])
out = out[len(disableFKCheckStmt):]
+ a.Equal(enableFKCheckStmt, out[len(out)-len(enableFKCheckStmt):])
+ out = out[:len(out)-len(enableFKCheckStmt)]
}
a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)
}