Skip to content

Commit

Permalink
Merge pull request #43 from phelrine/fix-foreign-key-constraints-issue
Browse files Browse the repository at this point in the history
Fixed an issue with foreign key constraints in `Load()` function
  • Loading branch information
andreynering authored Dec 15, 2018
2 parents 82bd348 + 7d0bc3e commit b7588e2
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ func (h *MySQL) tableNames(q queryable) ([]string, error) {
}

func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
// re-enable after load
defer func() {
if _, err2 := db.Exec("SET FOREIGN_KEY_CHECKS = 1"); err2 != nil && err == nil {
err = err2
}
}()

tx, err := db.Begin()
if err != nil {
return err
Expand All @@ -87,9 +80,14 @@ func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (er
return err
}

if err = loadFn(tx); err != nil {
err = loadFn(tx)
_, err2 := tx.Exec("SET FOREIGN_KEY_CHECKS = 1")
if err != nil {
return err
}
if err2 != nil {
return err2
}

return tx.Commit()
}
Expand Down

0 comments on commit b7588e2

Please sign in to comment.