Skip to content

Commit

Permalink
Reland "Bump github.com/go-sql-driver/mysql from 1.5.0 to 1.7.0" (#6662)
Browse files Browse the repository at this point in the history
This reverts #6660, and relands
#6625.
  • Loading branch information
aarongable committed Feb 21, 2023
1 parent a8783eb commit 85b146e
Show file tree
Hide file tree
Showing 29 changed files with 740 additions and 538 deletions.
47 changes: 10 additions & 37 deletions db/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"errors"
"fmt"
"regexp"
"strings"

gorp "github.com/go-gorp/gorp/v3"
"github.com/go-sql-driver/mysql"
)

// ErrDatabaseOp wraps an underlying err with a description of the operation
Expand All @@ -20,21 +20,6 @@ type ErrDatabaseOp struct {
Err error
}

// noRows returns true when the underlying error is sql.ErrNoRows and indicates
// that the error was that no results were found.
func (e ErrDatabaseOp) noRows() bool {
return e.Err == sql.ErrNoRows
}

// duplicate returns true when the underlying error has a message with a prefix
// matching "Error 1062: Duplicate entry". This is the error prefixed returned
// by MariaDB when a duplicate row is to be inserted.
func (e ErrDatabaseOp) duplicate() bool {
return strings.HasPrefix(
e.Err.Error(),
"Error 1062: Duplicate entry")
}

// Error for an ErrDatabaseOp composes a message with context about the
// operation and table as well as the underlying Err's error message.
func (e ErrDatabaseOp) Error() string {
Expand All @@ -57,31 +42,19 @@ func (e ErrDatabaseOp) Unwrap() error {
return e.Err
}

// IsNoRows is a utility function for casting an error to ErrDatabaseOp and
// returning true if its wrapped err is sql.ErrNoRows. If the error is not an
// ErrDatabaseOp the return value of IsNoRows will always be false.
// IsNoRows is a utility function for determining if an error wraps the go sql
// package's ErrNoRows, which is returned when a Scan operation has no more
// results to return, and as such is returned by many gorp methods.
func IsNoRows(err error) bool {
// if the err is an ErrDatabaseOp instance, return its noRows() result to see
// if the inner err is sql.ErrNoRows
var dbErr ErrDatabaseOp
if errors.As(err, &dbErr) {
return dbErr.noRows()
}
return false
return errors.Is(err, sql.ErrNoRows)
}

// IsDuplicate is a utility function for casting an error to ErrDatabaseOp and
// returning a boolean indicating if it is a duplicate error or not. If the
// error is not an ErrDatabaseOp the return value of IsDuplicate will always be
// false.
// IsDuplicate is a utility function for determining if an error wrap MySQL's
// Error 1062: Duplicate entry. This error is returned when inserting a row
// would violate a unique key constraint.
func IsDuplicate(err error) bool {
// if the err is an ErrDatabaseOp instance, return its duplicate() result to
// see if the inner err indicates a duplicate row error.
var dbErr ErrDatabaseOp
if errors.As(err, &dbErr) {
return dbErr.duplicate()
}
return false
var dbErr *mysql.MySQLError
return errors.As(err, &dbErr) && dbErr.Number == 1062
}

// WrappedMap wraps a *gorp.DbMap such that its major functions wrap error
Expand Down
16 changes: 8 additions & 8 deletions db/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestErrDatabaseOpError(t *testing.T) {
}
}

func TestErrDatabaseOpNoRows(t *testing.T) {
func TestIsNoRows(t *testing.T) {
testCases := []struct {
name string
err ErrDatabaseOp
Expand All @@ -59,7 +59,7 @@ func TestErrDatabaseOpNoRows(t *testing.T) {
err: ErrDatabaseOp{
Op: "test",
Table: "testTable",
Err: sql.ErrNoRows,
Err: fmt.Errorf("some wrapper around %w", sql.ErrNoRows),
},
expectedNoRows: true,
},
Expand All @@ -68,20 +68,20 @@ func TestErrDatabaseOpNoRows(t *testing.T) {
err: ErrDatabaseOp{
Op: "test",
Table: "testTable",
Err: errors.New("lots of rows. too many rows."),
Err: fmt.Errorf("some wrapper around %w", errors.New("lots of rows. too many rows.")),
},
expectedNoRows: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
test.AssertEquals(t, tc.err.noRows(), tc.expectedNoRows)
test.AssertEquals(t, IsNoRows(tc.err), tc.expectedNoRows)
})
}
}

func TestErrDatabaseOpDuplicate(t *testing.T) {
func TestIsDuplicate(t *testing.T) {
testCases := []struct {
name string
err ErrDatabaseOp
Expand All @@ -92,7 +92,7 @@ func TestErrDatabaseOpDuplicate(t *testing.T) {
err: ErrDatabaseOp{
Op: "test",
Table: "testTable",
Err: errors.New("Error 1062: Duplicate entry detected!!!!!!!"),
Err: fmt.Errorf("some wrapper around %w", &mysql.MySQLError{Number: 1062}),
},
expectDuplicate: true,
},
Expand All @@ -101,15 +101,15 @@ func TestErrDatabaseOpDuplicate(t *testing.T) {
err: ErrDatabaseOp{
Op: "test",
Table: "testTable",
Err: errors.New("DB forgot to save your data."),
Err: fmt.Errorf("some wrapper around %w", &mysql.MySQLError{Number: 1234}),
},
expectDuplicate: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
test.AssertEquals(t, tc.err.duplicate(), tc.expectDuplicate)
test.AssertEquals(t, IsDuplicate(tc.err), tc.expectDuplicate)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/eggsampler/acme/v3 v3.3.0
github.com/go-gorp/gorp/v3 v3.0.2
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.5.0
github.com/go-sql-driver/mysql v1.7.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/certificate-transparency-go v1.0.22-0.20181127102053-c25855a82c75
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXS
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/attrs v1.0.2/go.mod h1:tJ7wJj6XbMNhYwJ8fl2PFDpDcUfsG1spWdUJISvPAZQ=
github.com/gobuffalo/envy v1.10.1/go.mod h1:AWx4++KnNOW3JOeEvhSaq+mvgAvnMYOY1XSIin4Mago=
Expand Down
129 changes: 0 additions & 129 deletions vendor/github.com/go-sql-driver/mysql/.travis.yml

This file was deleted.

19 changes: 19 additions & 0 deletions vendor/github.com/go-sql-driver/mysql/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 85b146e

Please sign in to comment.