Skip to content

Commit a4a3ecf

Browse files
committed
Add: helper message for mysql foreign_key_checks
1 parent f6f9aab commit a4a3ecf

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

cmd/run.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"regexp"
7+
"slices"
8+
"strings"
69

710
"github.com/apoorvam/goterminal"
811
"github.com/pkg/errors"
@@ -109,6 +112,10 @@ func (cmd *RunCmd) Run() error {
109112
for _, table := range tablesSorted {
110113
err = cmd.run(table)
111114
if err != nil {
115+
// if FK fails on mysql, it could be due to an extra foreign keys even though the referenced table do not exist
116+
if cmd.DB.Engine == "mysql" && strings.Contains(err.Error(), "Error 1452") {
117+
helperForMySQLFKChecks(tablesSorted, err)
118+
}
112119
return errors.Wrapf(err, "failed to insert on %s.%s", table.Schema, table.Name)
113120
}
114121
}
@@ -143,3 +150,17 @@ func startProgressBar(tablename string, total int64, c chan int64) {
143150
}
144151
writer.Reset()
145152
}
153+
154+
func helperForMySQLFKChecks(tablesSorted []*db.Table, err error) {
155+
156+
// getting the table provoking the issue from the deepest error
157+
tableRegex := regexp.MustCompile("REFERENCES `(\\w+)`")
158+
submatches := tableRegex.FindStringSubmatch(errors.Cause(err).Error())
159+
160+
// checking if this table is supposed to be in our list
161+
if len(submatches) == 2 && !slices.ContainsFunc(tablesSorted, func(t *db.Table) bool {
162+
return strings.ToLower(t.Name) == submatches[1]
163+
}) {
164+
log.Warn().Msg("A foreign key pointing to a missing tables forced an error. Hint: SET GLOBAL foreign_key_checks=0;")
165+
}
166+
}

0 commit comments

Comments
 (0)