From 32114dc65dd84900c18e149ee54f25f13bdc7dd6 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Sat, 16 Sep 2023 09:08:05 -0400 Subject: [PATCH] use multierr instead of errors.join --- go.mod | 1 + go.sum | 2 ++ internal/sqladapter/store_test.go | 30 ++++++++++++++++++++---------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index b8aaaa724..43ca83dc4 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 659c7bb7a..89df3d7ba 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/sqladapter/store_test.go b/internal/sqladapter/store_test.go index 8c69e405b..6888f313a 100644 --- a/internal/sqladapter/store_test.go +++ b/internal/sqladapter/store_test.go @@ -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() @@ -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) @@ -53,6 +55,7 @@ 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 { @@ -60,6 +63,7 @@ func TestStore_Postgres(t *testing.T) { }) 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) @@ -72,6 +76,7 @@ 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 { @@ -79,6 +84,7 @@ func TestStore_Postgres(t *testing.T) { }) 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) @@ -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 { @@ -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) @@ -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) @@ -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 { @@ -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 {