From deb86d46f1705a92fc99e07f5fb5d13a06d4ddf1 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Fri, 3 Nov 2023 21:23:05 -0400 Subject: [PATCH] improve go migration tests --- .../success/gomigrations_success_test.go | 55 +++++++++++++++++-- .../success/testdata/001_up_down.go | 19 +++++-- .../success/testdata/002_up_only.go | 4 +- .../success/testdata/003_down_only.go | 4 +- .../success/testdata/005_up_down_no_tx.go | 8 +-- .../success/testdata/006_up_only_no_tx.go | 4 +- .../success/testdata/007_down_only_no_tx.go | 4 +- .../success/testdata/008_empty_no_tx.go | 2 +- .../success/testdata/009_up_down_ctx.go | 20 +++++++ .../success/testdata/010_up_only_ctx.go | 16 ++++++ .../success/testdata/011_down_only_ctx.go | 16 ++++++ .../success/testdata/012_empty_ctx.go | 9 +++ .../success/testdata/013_up_down_no_tx_ctx.go | 20 +++++++ .../success/testdata/014_up_only_no_tx_ctx.go | 16 ++++++ .../testdata/015_down_only_no_tx_ctx.go | 16 ++++++ .../success/testdata/016_empty_no_tx_ctx.go | 9 +++ 16 files changed, 193 insertions(+), 29 deletions(-) create mode 100644 tests/gomigrations/success/testdata/009_up_down_ctx.go create mode 100644 tests/gomigrations/success/testdata/010_up_only_ctx.go create mode 100644 tests/gomigrations/success/testdata/011_down_only_ctx.go create mode 100644 tests/gomigrations/success/testdata/012_empty_ctx.go create mode 100644 tests/gomigrations/success/testdata/013_up_down_no_tx_ctx.go create mode 100644 tests/gomigrations/success/testdata/014_up_only_no_tx_ctx.go create mode 100644 tests/gomigrations/success/testdata/015_down_only_no_tx_ctx.go create mode 100644 tests/gomigrations/success/testdata/016_empty_no_tx_ctx.go diff --git a/tests/gomigrations/success/gomigrations_success_test.go b/tests/gomigrations/success/gomigrations_success_test.go index 87bebf48d..0da33249f 100644 --- a/tests/gomigrations/success/gomigrations_success_test.go +++ b/tests/gomigrations/success/gomigrations_success_test.go @@ -1,27 +1,30 @@ -package gomigrations +package gomigrations_test import ( + "database/sql" "path/filepath" "testing" "github.com/pressly/goose/v3" "github.com/pressly/goose/v3/internal/check" - "github.com/pressly/goose/v3/internal/testdb" _ "github.com/pressly/goose/v3/tests/gomigrations/success/testdata" + _ "modernc.org/sqlite" ) func TestGoMigrationByOne(t *testing.T) { - db, cleanup, err := testdb.NewPostgres() - check.NoError(t, err) - t.Cleanup(cleanup) + t.Parallel() + goose.SetDialect("sqlite3") + db, err := sql.Open("sqlite", ":memory:") + check.NoError(t, err) dir := "testdata" files, err := filepath.Glob(dir + "/*.go") check.NoError(t, err) upByOne := func(t *testing.T) int64 { err = goose.UpByOne(db, dir) + t.Logf("err: %v %s", err, dir) check.NoError(t, err) version, err := goose.GetDBVersion(db) check.NoError(t, err) @@ -42,6 +45,21 @@ func TestGoMigrationByOne(t *testing.T) { check.NoError(t, err) check.Number(t, version, len(files)) + tables, err := ListTables(db) + check.NoError(t, err) + check.Equal(t, tables, []string{ + "alpha", + "bravo", + "charlie", + "delta", + "echo", + "foxtrot", + "golf", + "goose_db_version", + "hotel", + "sqlite_sequence", + }) + // Migrate all files down-by-one. for i := len(files) - 1; i >= 0; i-- { check.Number(t, downByOne(t), i) @@ -49,4 +67,31 @@ func TestGoMigrationByOne(t *testing.T) { version, err = goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, version, 0) + + tables, err = ListTables(db) + check.NoError(t, err) + check.Equal(t, tables, []string{ + "goose_db_version", + "sqlite_sequence", + }) +} + +func ListTables(db *sql.DB) ([]string, error) { + rows, err := db.Query(`SELECT name FROM sqlite_master WHERE type='table' ORDER BY name`) + if err != nil { + return nil, err + } + defer rows.Close() + var tables []string + for rows.Next() { + var name string + if err := rows.Scan(&name); err != nil { + return nil, err + } + tables = append(tables, name) + } + if err := rows.Err(); err != nil { + return nil, err + } + return tables, nil } diff --git a/tests/gomigrations/success/testdata/001_up_down.go b/tests/gomigrations/success/testdata/001_up_down.go index 9fed61cac..b3c65e92b 100644 --- a/tests/gomigrations/success/testdata/001_up_down.go +++ b/tests/gomigrations/success/testdata/001_up_down.go @@ -1,9 +1,12 @@ package gomigrations import ( + "context" "database/sql" + "fmt" "github.com/pressly/goose/v3" + "github.com/pressly/goose/v3/database" ) func init() { @@ -11,13 +14,19 @@ func init() { } func up001(tx *sql.Tx) error { - q := "CREATE TABLE foo (id INT, subid INT, name TEXT)" - _, err := tx.Exec(q) - return err + return createTable(tx, "alpha") } func down001(tx *sql.Tx) error { - q := "DROP TABLE IF EXISTS foo" - _, err := tx.Exec(q) + return dropTable(tx, "alpha") +} + +func createTable(db database.DBTxConn, name string) error { + _, err := db.ExecContext(context.Background(), fmt.Sprintf("CREATE TABLE %s (id INTEGER)", name)) + return err +} + +func dropTable(db database.DBTxConn, name string) error { + _, err := db.ExecContext(context.Background(), fmt.Sprintf("DROP TABLE %s", name)) return err } diff --git a/tests/gomigrations/success/testdata/002_up_only.go b/tests/gomigrations/success/testdata/002_up_only.go index 6ece192db..e5aab5afa 100644 --- a/tests/gomigrations/success/testdata/002_up_only.go +++ b/tests/gomigrations/success/testdata/002_up_only.go @@ -11,7 +11,5 @@ func init() { } func up002(tx *sql.Tx) error { - q := "INSERT INTO foo VALUES (1, 1, 'Alice')" - _, err := tx.Exec(q) - return err + return createTable(tx, "bravo") } diff --git a/tests/gomigrations/success/testdata/003_down_only.go b/tests/gomigrations/success/testdata/003_down_only.go index ff39f5fb6..c9d062ba9 100644 --- a/tests/gomigrations/success/testdata/003_down_only.go +++ b/tests/gomigrations/success/testdata/003_down_only.go @@ -11,7 +11,5 @@ func init() { } func down003(tx *sql.Tx) error { - q := "TRUNCATE TABLE foo" - _, err := tx.Exec(q) - return err + return dropTable(tx, "bravo") } diff --git a/tests/gomigrations/success/testdata/005_up_down_no_tx.go b/tests/gomigrations/success/testdata/005_up_down_no_tx.go index 7a6838d1c..1ef5a5759 100644 --- a/tests/gomigrations/success/testdata/005_up_down_no_tx.go +++ b/tests/gomigrations/success/testdata/005_up_down_no_tx.go @@ -11,13 +11,9 @@ func init() { } func up005(db *sql.DB) error { - q := "CREATE TABLE users (id INT, email TEXT)" - _, err := db.Exec(q) - return err + return createTable(db, "charlie") } func down005(db *sql.DB) error { - q := "DROP TABLE IF EXISTS users" - _, err := db.Exec(q) - return err + return dropTable(db, "charlie") } diff --git a/tests/gomigrations/success/testdata/006_up_only_no_tx.go b/tests/gomigrations/success/testdata/006_up_only_no_tx.go index 26aa88c62..2aa770c0c 100644 --- a/tests/gomigrations/success/testdata/006_up_only_no_tx.go +++ b/tests/gomigrations/success/testdata/006_up_only_no_tx.go @@ -11,7 +11,5 @@ func init() { } func up006(db *sql.DB) error { - q := "INSERT INTO users VALUES (1, 'admin@example.com')" - _, err := db.Exec(q) - return err + return createTable(db, "delta") } diff --git a/tests/gomigrations/success/testdata/007_down_only_no_tx.go b/tests/gomigrations/success/testdata/007_down_only_no_tx.go index 318b02e4b..86edd41d0 100644 --- a/tests/gomigrations/success/testdata/007_down_only_no_tx.go +++ b/tests/gomigrations/success/testdata/007_down_only_no_tx.go @@ -11,7 +11,5 @@ func init() { } func down007(db *sql.DB) error { - q := "TRUNCATE TABLE users" - _, err := db.Exec(q) - return err + return dropTable(db, "delta") } diff --git a/tests/gomigrations/success/testdata/008_empty_no_tx.go b/tests/gomigrations/success/testdata/008_empty_no_tx.go index 5efb3764f..76aaedf00 100644 --- a/tests/gomigrations/success/testdata/008_empty_no_tx.go +++ b/tests/gomigrations/success/testdata/008_empty_no_tx.go @@ -5,5 +5,5 @@ import ( ) func init() { - goose.AddMigration(nil, nil) + goose.AddMigrationNoTx(nil, nil) } diff --git a/tests/gomigrations/success/testdata/009_up_down_ctx.go b/tests/gomigrations/success/testdata/009_up_down_ctx.go new file mode 100644 index 000000000..09ce310bc --- /dev/null +++ b/tests/gomigrations/success/testdata/009_up_down_ctx.go @@ -0,0 +1,20 @@ +package gomigrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationContext(up009, down009) +} + +func up009(ctx context.Context, tx *sql.Tx) error { + return createTable(tx, "echo") +} + +func down009(ctx context.Context, tx *sql.Tx) error { + return dropTable(tx, "echo") +} diff --git a/tests/gomigrations/success/testdata/010_up_only_ctx.go b/tests/gomigrations/success/testdata/010_up_only_ctx.go new file mode 100644 index 000000000..04395307f --- /dev/null +++ b/tests/gomigrations/success/testdata/010_up_only_ctx.go @@ -0,0 +1,16 @@ +package gomigrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationContext(up010, nil) +} + +func up010(ctx context.Context, tx *sql.Tx) error { + return createTable(tx, "foxtrot") +} diff --git a/tests/gomigrations/success/testdata/011_down_only_ctx.go b/tests/gomigrations/success/testdata/011_down_only_ctx.go new file mode 100644 index 000000000..c1f1fc7b7 --- /dev/null +++ b/tests/gomigrations/success/testdata/011_down_only_ctx.go @@ -0,0 +1,16 @@ +package gomigrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationContext(nil, down011) +} + +func down011(ctx context.Context, tx *sql.Tx) error { + return dropTable(tx, "foxtrot") +} diff --git a/tests/gomigrations/success/testdata/012_empty_ctx.go b/tests/gomigrations/success/testdata/012_empty_ctx.go new file mode 100644 index 000000000..a8df35396 --- /dev/null +++ b/tests/gomigrations/success/testdata/012_empty_ctx.go @@ -0,0 +1,9 @@ +package gomigrations + +import ( + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationContext(nil, nil) +} diff --git a/tests/gomigrations/success/testdata/013_up_down_no_tx_ctx.go b/tests/gomigrations/success/testdata/013_up_down_no_tx_ctx.go new file mode 100644 index 000000000..da3a27cdb --- /dev/null +++ b/tests/gomigrations/success/testdata/013_up_down_no_tx_ctx.go @@ -0,0 +1,20 @@ +package gomigrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationNoTxContext(up013, down013) +} + +func up013(ctx context.Context, db *sql.DB) error { + return createTable(db, "golf") +} + +func down013(ctx context.Context, db *sql.DB) error { + return dropTable(db, "golf") +} diff --git a/tests/gomigrations/success/testdata/014_up_only_no_tx_ctx.go b/tests/gomigrations/success/testdata/014_up_only_no_tx_ctx.go new file mode 100644 index 000000000..a32de869c --- /dev/null +++ b/tests/gomigrations/success/testdata/014_up_only_no_tx_ctx.go @@ -0,0 +1,16 @@ +package gomigrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationNoTxContext(up014, nil) +} + +func up014(ctx context.Context, db *sql.DB) error { + return createTable(db, "hotel") +} diff --git a/tests/gomigrations/success/testdata/015_down_only_no_tx_ctx.go b/tests/gomigrations/success/testdata/015_down_only_no_tx_ctx.go new file mode 100644 index 000000000..18b8c2d77 --- /dev/null +++ b/tests/gomigrations/success/testdata/015_down_only_no_tx_ctx.go @@ -0,0 +1,16 @@ +package gomigrations + +import ( + "context" + "database/sql" + + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationNoTxContext(nil, down015) +} + +func down015(ctx context.Context, db *sql.DB) error { + return dropTable(db, "hotel") +} diff --git a/tests/gomigrations/success/testdata/016_empty_no_tx_ctx.go b/tests/gomigrations/success/testdata/016_empty_no_tx_ctx.go new file mode 100644 index 000000000..e97c86b83 --- /dev/null +++ b/tests/gomigrations/success/testdata/016_empty_no_tx_ctx.go @@ -0,0 +1,9 @@ +package gomigrations + +import ( + "github.com/pressly/goose/v3" +) + +func init() { + goose.AddMigrationNoTxContext(nil, nil) +}