Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions migrators/goosemigrator/goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"io/fs"
"os"
"path/filepath"

"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/database"
Expand Down Expand Up @@ -44,7 +45,8 @@ func WithTableName(tableName string) Option {

// WithFS specifies a `fs.FS` from which to read the migration files.
//
// Default: `os.DirFS(".")` (reads from the real filesystem in the current working directory)
// Default: The base directory of migrationsDir, `os.DirFS(".")` for a local directory,
// or `os.DirFS(filepath.Dir(migrationsDir))` for a relative path (e.g., "../../..").
//
// https://github.com/pressly/goose#embedded-sql-migrations
func WithFS(dir fs.FS) Option {
Expand All @@ -66,10 +68,16 @@ func WithFS(dir fs.FS) Option {
// - [WithTableName] is the same as -table
func New(migrationsDir string, opts ...Option) *GooseMigrator {
gm := &GooseMigrator{
MigrationsDir: migrationsDir,
MigrationsDir: filepath.Clean(migrationsDir),
TableName: DefaultTableName,
FS: os.DirFS("."),
}

if !filepath.IsLocal(gm.MigrationsDir) {
gm.FS = os.DirFS(filepath.Dir(gm.MigrationsDir))
gm.MigrationsDir = filepath.Base(gm.MigrationsDir)
}

for _, opt := range opts {
opt(gm)
}
Expand Down
44 changes: 41 additions & 3 deletions migrators/goosemigrator/goose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,49 @@ import (
"github.com/peterldowns/pgtestdb/migrators/goosemigrator"
)

func TestGooseMigratorFromDisk(t *testing.T) {
func TestGooseMigratorFromDiskWithSubdirectory(t *testing.T) {
t.Parallel()
ctx := context.Background()

m := goosemigrator.New("migrations")
m := goosemigrator.New("./migrations")
db := pgtestdb.New(t, pgtestdb.Config{
DriverName: "pgx",
Host: "localhost",
User: "postgres",
Password: "password",
Port: "5433",
Options: "sslmode=disable",
}, m)
assert.NotEqual(t, nil, db)

assert.NoFailures(t, func() {
var lastAppliedMigration int
err := db.QueryRowContext(ctx, "select max(version_id) from goose_db_version").Scan(&lastAppliedMigration)
assert.Nil(t, err)
check.Equal(t, 2, lastAppliedMigration)
})

var numUsers int
err := db.QueryRowContext(ctx, "select count(*) from users").Scan(&numUsers)
assert.Nil(t, err)
check.Equal(t, 0, numUsers)

var numCats int
err = db.QueryRowContext(ctx, "select count(*) from cats").Scan(&numCats)
assert.Nil(t, err)
check.Equal(t, 0, numCats)

var numBlogPosts int
err = db.QueryRowContext(ctx, "select count(*) from blog_posts").Scan(&numBlogPosts)
assert.Nil(t, err)
check.Equal(t, 0, numBlogPosts)
}

func TestGooseMigratorFromDiskWithParentDirectoryRelativePath(t *testing.T) {
t.Parallel()
ctx := context.Background()

m := goosemigrator.New("../../migrators/goosemigrator/migrations")
db := pgtestdb.New(t, pgtestdb.Config{
DriverName: "pgx",
Host: "localhost",
Expand Down Expand Up @@ -54,7 +92,7 @@ func TestGooseMigratorFromDisk(t *testing.T) {
//go:embed migrations/*.sql
var exampleFS embed.FS

func TestGooseMigratorFromFS(t *testing.T) {
func TestGooseMigratorFromEmbedFS(t *testing.T) {
t.Parallel()
ctx := context.Background()

Expand Down