Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error mapper for constraint errors #63

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,33 @@
msg = err.Error()
errCodeSep = ':'
errCodeIndex = strings.IndexRune(msg, errCodeSep)
errStateSep = '('
errStateIndex = -1
)

if errCodeIndex < 0 {
errCodeIndex = 0
}

switch msg[:errCodeIndex] {
errStateIndex = strings.IndexRune(msg[:errCodeIndex], errStateSep)
errStateIndex--
if errStateIndex < 0 {
errStateIndex = errCodeIndex
}

switch (msg[:errCodeIndex])[:errStateIndex] {
case "Error 1062":
return rel.ConstraintError{
Key: sql.ExtractString(msg, "key '", "'"),
Type: rel.UniqueConstraint,
Err: err,
}
case "Error 1451":
return rel.ConstraintError{
Key: sql.ExtractString(msg, "CONSTRAINT `", "`"),
Type: rel.ForeignKeyConstraint,
Err: err,
}

Check warning on line 151 in mysql.go

View check run for this annotation

Codecov / codecov/patch

mysql.go#L146-L151

Added lines #L146 - L151 were not covered by tests
case "Error 1452":
nItroFreeZer marked this conversation as resolved.
Show resolved Hide resolved
return rel.ConstraintError{
Key: sql.ExtractString(msg, "CONSTRAINT `", "`"),
Expand Down
Loading