Skip to content

Commit

Permalink
improve go migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Nov 4, 2023
1 parent 03626bd commit deb86d4
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 29 deletions.
55 changes: 50 additions & 5 deletions tests/gomigrations/success/gomigrations_success_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -42,11 +45,53 @@ 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)
}
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
}
19 changes: 14 additions & 5 deletions tests/gomigrations/success/testdata/001_up_down.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package gomigrations

import (
"context"
"database/sql"
"fmt"

"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/database"
)

func init() {
goose.AddMigration(up001, down001)
}

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
}
4 changes: 1 addition & 3 deletions tests/gomigrations/success/testdata/002_up_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 1 addition & 3 deletions tests/gomigrations/success/testdata/003_down_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 2 additions & 6 deletions tests/gomigrations/success/testdata/005_up_down_no_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 1 addition & 3 deletions tests/gomigrations/success/testdata/006_up_only_no_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 1 addition & 3 deletions tests/gomigrations/success/testdata/007_down_only_no_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion tests/gomigrations/success/testdata/008_empty_no_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func init() {
goose.AddMigration(nil, nil)
goose.AddMigrationNoTx(nil, nil)
}
20 changes: 20 additions & 0 deletions tests/gomigrations/success/testdata/009_up_down_ctx.go
Original file line number Diff line number Diff line change
@@ -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")
}
16 changes: 16 additions & 0 deletions tests/gomigrations/success/testdata/010_up_only_ctx.go
Original file line number Diff line number Diff line change
@@ -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")
}
16 changes: 16 additions & 0 deletions tests/gomigrations/success/testdata/011_down_only_ctx.go
Original file line number Diff line number Diff line change
@@ -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")
}
9 changes: 9 additions & 0 deletions tests/gomigrations/success/testdata/012_empty_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gomigrations

import (
"github.com/pressly/goose/v3"
)

func init() {
goose.AddMigrationContext(nil, nil)
}
20 changes: 20 additions & 0 deletions tests/gomigrations/success/testdata/013_up_down_no_tx_ctx.go
Original file line number Diff line number Diff line change
@@ -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")
}
16 changes: 16 additions & 0 deletions tests/gomigrations/success/testdata/014_up_only_no_tx_ctx.go
Original file line number Diff line number Diff line change
@@ -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")
}
16 changes: 16 additions & 0 deletions tests/gomigrations/success/testdata/015_down_only_no_tx_ctx.go
Original file line number Diff line number Diff line change
@@ -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")
}
9 changes: 9 additions & 0 deletions tests/gomigrations/success/testdata/016_empty_no_tx_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gomigrations

import (
"github.com/pressly/goose/v3"
)

func init() {
goose.AddMigrationNoTxContext(nil, nil)
}

0 comments on commit deb86d4

Please sign in to comment.