Skip to content

Commit

Permalink
use multierr instead of errors.join
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Sep 16, 2023
1 parent 77dec20 commit 32114dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/ory/dockertest/v3 v3.10.0
github.com/vertica/vertica-sql-go v1.3.3
github.com/ziutek/mymysql v1.5.4
go.uber.org/multierr v1.11.0
modernc.org/sqlite v1.25.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ go.opentelemetry.io/otel v1.16.0 h1:Z7GVAX/UkAXPKsy94IU+i6thsQS4nb7LviLpnaNeW8s=
go.opentelemetry.io/otel v1.16.0/go.mod h1:vl0h9NUa1D5s1nv3A5vZOYWn8av4K8Ml6JDeHrT/bx4=
go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZEu5MQs=
go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
30 changes: 20 additions & 10 deletions internal/sqladapter/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/pressly/goose/v3/internal/check"
"github.com/pressly/goose/v3/internal/sqladapter"
"github.com/pressly/goose/v3/internal/testdb"
"go.uber.org/multierr"
)

// The goal of this test is to verify the sqladapter package works as expected. This test is not
// meant to be exhaustive or test every possible database dialect. It is meant to verify that the
// Store interface works against a real database.
// meant to be exhaustive or test every possible database dialect. It is meant to verify the Store
// interface works against a real database.

func TestStore_Postgres(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -45,6 +46,7 @@ func TestStore_Postgres(t *testing.T) {
ok := errors.As(err, &pgErr)
check.Bool(t, ok, true)
check.Equal(t, pgErr.Code, "42P07") // duplicate_table

// List migrations. There should be none.
err = runConn(ctx, db, func(conn *sql.Conn) error {
res, err := store.ListMigrationsConn(ctx, conn)
Expand All @@ -53,13 +55,15 @@ func TestStore_Postgres(t *testing.T) {
return nil
})
check.NoError(t, err)

// Insert 5 migrations in addition to the zero migration.
for i := 0; i < 6; i++ {
err = runConn(ctx, db, func(conn *sql.Conn) error {
return store.InsertOrDelete(ctx, conn, true, int64(i))
})
check.NoError(t, err)
}

// List migrations. There should be 6.
err = runConn(ctx, db, func(conn *sql.Conn) error {
res, err := store.ListMigrationsConn(ctx, conn)
Expand All @@ -72,13 +76,15 @@ func TestStore_Postgres(t *testing.T) {
return nil
})
check.NoError(t, err)

// Delete 3 migrations backwards
for i := 5; i >= 3; i-- {
err = runConn(ctx, db, func(conn *sql.Conn) error {
return store.InsertOrDelete(ctx, conn, false, int64(i))
})
check.NoError(t, err)
}

// List migrations. There should be 3.
err = runConn(ctx, db, func(conn *sql.Conn) error {
res, err := store.ListMigrationsConn(ctx, conn)
Expand All @@ -91,6 +97,7 @@ func TestStore_Postgres(t *testing.T) {
return nil
})
check.NoError(t, err)

// Get remaining migrations one by one.
for i := 0; i < 3; i++ {
err = runConn(ctx, db, func(conn *sql.Conn) error {
Expand All @@ -102,20 +109,22 @@ func TestStore_Postgres(t *testing.T) {
})
check.NoError(t, err)
}

// Delete remaining migrations one by one and use all 3 connection types:
// *sql.DB
// *sql.Tx
// *sql.Conn.
// 1. *sql.Tx
err = runTx(ctx, db, func(tx *sql.Tx) error {
return store.InsertOrDelete(ctx, tx, false, 2) // *sql.Tx
return store.InsertOrDelete(ctx, tx, false, 2)
})
check.NoError(t, err)
// 2. *sql.Conn
err = runConn(ctx, db, func(conn *sql.Conn) error {
return store.InsertOrDelete(ctx, conn, false, 1) // *sql.Conn
return store.InsertOrDelete(ctx, conn, false, 1)
})
check.NoError(t, err)
err = store.InsertOrDelete(ctx, db, false, 0) // *sql.DB
// 3. *sql.DB
err = store.InsertOrDelete(ctx, db, false, 0)
check.NoError(t, err)

// List migrations. There should be none.
err = runConn(ctx, db, func(conn *sql.Conn) error {
res, err := store.ListMigrationsConn(ctx, conn)
Expand All @@ -124,6 +133,7 @@ func TestStore_Postgres(t *testing.T) {
return nil
})
check.NoError(t, err)

// Try to get a migration that does not exist.
err = runConn(ctx, db, func(conn *sql.Conn) error {
_, err := store.GetMigrationConn(ctx, conn, 0)
Expand All @@ -141,7 +151,7 @@ func runTx(ctx context.Context, db *sql.DB, fn func(*sql.Tx) error) (retErr erro
}
defer func() {
if retErr != nil {
retErr = errors.Join(retErr, tx.Rollback())
retErr = multierr.Append(retErr, tx.Rollback())
}
}()
if err := fn(tx); err != nil {
Expand All @@ -157,7 +167,7 @@ func runConn(ctx context.Context, db *sql.DB, fn func(*sql.Conn) error) (retErr
}
defer func() {
if retErr != nil {
retErr = errors.Join(retErr, conn.Close())
retErr = multierr.Append(retErr, conn.Close())
}
}()
if err := fn(conn); err != nil {
Expand Down

0 comments on commit 32114dc

Please sign in to comment.