Skip to content

Commit 7792810

Browse files
authored
fix(migrators/goose): read from filesytem if no FS (#26)
This CR should allow to read migrations folder from parent directory like `../migrations`. I have a `migrations` directory in the root of the project, so to make migrations work, I need to provide `FS` with root directory: ```go root, err := filepath.Abs("../../../..") if err != nil { t.Fatal("get abs path", err) } migrator := goosemigrator.New("migrations", goosemigrator.WithFS(os.DirFS(root))) ``` This CR should allow to do just: ```go migrator := goosemigrator.New("../../../../migrations") ``` Also, `golangmigrator` does not have `fs.FS` by default too.
1 parent 4aafd60 commit 7792810

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

migrators/goosemigrator/goose.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"io/fs"
77
"os"
8+
"path/filepath"
89

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

4546
// WithFS specifies a `fs.FS` from which to read the migration files.
4647
//
47-
// Default: `os.DirFS(".")` (reads from the real filesystem in the current working directory)
48+
// Default: The base directory of migrationsDir, `os.DirFS(".")` for a local directory,
49+
// or `os.DirFS(filepath.Dir(migrationsDir))` for a relative path (e.g., "../../..").
4850
//
4951
// https://github.com/pressly/goose#embedded-sql-migrations
5052
func WithFS(dir fs.FS) Option {
@@ -66,10 +68,16 @@ func WithFS(dir fs.FS) Option {
6668
// - [WithTableName] is the same as -table
6769
func New(migrationsDir string, opts ...Option) *GooseMigrator {
6870
gm := &GooseMigrator{
69-
MigrationsDir: migrationsDir,
71+
MigrationsDir: filepath.Clean(migrationsDir),
7072
TableName: DefaultTableName,
7173
FS: os.DirFS("."),
7274
}
75+
76+
if !filepath.IsLocal(gm.MigrationsDir) {
77+
gm.FS = os.DirFS(filepath.Dir(gm.MigrationsDir))
78+
gm.MigrationsDir = filepath.Base(gm.MigrationsDir)
79+
}
80+
7381
for _, opt := range opts {
7482
opt(gm)
7583
}

migrators/goosemigrator/goose_test.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,49 @@ import (
1313
"github.com/peterldowns/pgtestdb/migrators/goosemigrator"
1414
)
1515

16-
func TestGooseMigratorFromDisk(t *testing.T) {
16+
func TestGooseMigratorFromDiskWithSubdirectory(t *testing.T) {
1717
t.Parallel()
1818
ctx := context.Background()
1919

20-
m := goosemigrator.New("migrations")
20+
m := goosemigrator.New("./migrations")
21+
db := pgtestdb.New(t, pgtestdb.Config{
22+
DriverName: "pgx",
23+
Host: "localhost",
24+
User: "postgres",
25+
Password: "password",
26+
Port: "5433",
27+
Options: "sslmode=disable",
28+
}, m)
29+
assert.NotEqual(t, nil, db)
30+
31+
assert.NoFailures(t, func() {
32+
var lastAppliedMigration int
33+
err := db.QueryRowContext(ctx, "select max(version_id) from goose_db_version").Scan(&lastAppliedMigration)
34+
assert.Nil(t, err)
35+
check.Equal(t, 2, lastAppliedMigration)
36+
})
37+
38+
var numUsers int
39+
err := db.QueryRowContext(ctx, "select count(*) from users").Scan(&numUsers)
40+
assert.Nil(t, err)
41+
check.Equal(t, 0, numUsers)
42+
43+
var numCats int
44+
err = db.QueryRowContext(ctx, "select count(*) from cats").Scan(&numCats)
45+
assert.Nil(t, err)
46+
check.Equal(t, 0, numCats)
47+
48+
var numBlogPosts int
49+
err = db.QueryRowContext(ctx, "select count(*) from blog_posts").Scan(&numBlogPosts)
50+
assert.Nil(t, err)
51+
check.Equal(t, 0, numBlogPosts)
52+
}
53+
54+
func TestGooseMigratorFromDiskWithParentDirectoryRelativePath(t *testing.T) {
55+
t.Parallel()
56+
ctx := context.Background()
57+
58+
m := goosemigrator.New("../../migrators/goosemigrator/migrations")
2159
db := pgtestdb.New(t, pgtestdb.Config{
2260
DriverName: "pgx",
2361
Host: "localhost",
@@ -54,7 +92,7 @@ func TestGooseMigratorFromDisk(t *testing.T) {
5492
//go:embed migrations/*.sql
5593
var exampleFS embed.FS
5694

57-
func TestGooseMigratorFromFS(t *testing.T) {
95+
func TestGooseMigratorFromEmbedFS(t *testing.T) {
5896
t.Parallel()
5997
ctx := context.Background()
6098

0 commit comments

Comments
 (0)