Skip to content

Commit f4a7ae3

Browse files
committed
Point out that embed is the way to go now
1 parent 791a117 commit f4a7ae3

File tree

5 files changed

+47
-95
lines changed

5 files changed

+47
-95
lines changed

README.md

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ migrations := &migrate.FileMigrationSource{
219219
}
220220

221221
// OR: Use migrations from a packr box
222+
// Note: Packr is no longer supported, your best option these days is [embed](https://pkg.go.dev/embed)
222223
migrations := &migrate.PackrMigrationSource{
223224
Box: packr.New("migrations", "./migrations"),
224225
}
@@ -316,62 +317,31 @@ CREATE UNIQUE INDEX CONCURRENTLY people_unique_id_idx ON people (id);
316317
DROP INDEX people_unique_id_idx;
317318
```
318319

319-
## Embedding migrations with [packr](https://github.com/gobuffalo/packr)
320+
## Embedding migrations with [embed](https://pkg.go.dev/embed)
320321

321-
If you like your Go applications self-contained (that is: a single binary): use [packr](https://github.com/gobuffalo/packr) to embed the migration files.
322+
If you like your Go applications self-contained (that is: a single binary): use [embed](https://pkg.go.dev/embed) to embed the migration files.
322323

323324
Just write your migration files as usual, as a set of SQL files in a folder.
324325

325-
Import the packr package into your application:
326+
Import the embed package into your application and point it to your migrations:
326327

327328
```go
328-
import "github.com/gobuffalo/packr/v2"
329-
```
330-
331-
Use the `PackrMigrationSource` in your application to find the migrations:
329+
import "embed"
332330

333-
```go
334-
migrations := &migrate.PackrMigrationSource{
335-
Box: packr.New("migrations", "./migrations"),
336-
}
331+
//go:embed migrations/*
332+
var dbMigrations embed.FS
337333
```
338334

339-
If you already have a box and would like to use a subdirectory:
335+
Use the `EmbedFileSystemMigrationSource` in your application to find the migrations:
340336

341337
```go
342-
migrations := &migrate.PackrMigrationSource{
343-
Box: myBox,
344-
Dir: "./migrations",
338+
migrations := migrate.EmbedFileSystemMigrationSource{
339+
FileSystem: dbMigrations,
340+
Root: "migrations",
345341
}
346342
```
347343

348-
## Embedding migrations with [bindata](https://github.com/shuLhan/go-bindata)
349-
350-
As an alternative, but slightly less maintained, you can use [bindata](https://github.com/shuLhan/go-bindata) to embed the migration files.
351-
352-
Just write your migration files as usual, as a set of SQL files in a folder.
353-
354-
Then use bindata to generate a `.go` file with the migrations embedded:
355-
356-
```bash
357-
go-bindata -pkg myapp -o bindata.go db/migrations/
358-
```
359-
360-
The resulting `bindata.go` file will contain your migrations. Remember to regenerate your `bindata.go` file whenever you add/modify a migration (`go generate` will help here, once it arrives).
361-
362-
Use the `AssetMigrationSource` in your application to find the migrations:
363-
364-
```go
365-
migrations := &migrate.AssetMigrationSource{
366-
Asset: Asset,
367-
AssetDir: AssetDir,
368-
Dir: "db/migrations",
369-
}
370-
```
371-
372-
Both `Asset` and `AssetDir` are functions provided by bindata.
373-
374-
Then proceed as usual.
344+
Other options such as [packr](https://github.com/gobuffalo/packr) or [go-bindata](https://github.com/shuLhan/go-bindata) are no longer recommended.
375345

376346
## Embedding migrations with libraries that implement `http.FileSystem`
377347

migrate.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"database/sql"
7+
"embed"
78
"errors"
89
"fmt"
910
"io"
@@ -343,6 +344,19 @@ func (a AssetMigrationSource) FindMigrations() ([]*Migration, error) {
343344
return migrations, nil
344345
}
345346

347+
// A set of migrations loaded from an go1.16 embed.FS
348+
type EmbedFileSystemMigrationSource struct {
349+
FileSystem embed.FS
350+
351+
Root string
352+
}
353+
354+
var _ MigrationSource = (*EmbedFileSystemMigrationSource)(nil)
355+
356+
func (f EmbedFileSystemMigrationSource) FindMigrations() ([]*Migration, error) {
357+
return findMigrations(http.FS(f.FileSystem), f.Root)
358+
}
359+
346360
// Avoids pulling in the packr library for everyone, mimicks the bits of
347361
// packr.Box that we need.
348362
type PackrBox interface {

migrate_go116.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

migrate_go116_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

migrate_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package migrate
33
import (
44
"context"
55
"database/sql"
6+
"embed"
67
"net/http"
78
"time"
89

@@ -809,3 +810,23 @@ func (s *SqliteMigrateSuite) TestContextTimeout(c *C) {
809810
c.Assert(err, Not(IsNil))
810811
c.Assert(n, Equals, 2)
811812
}
813+
814+
//go:embed test-migrations/*
815+
var testEmbedFS embed.FS
816+
817+
func (s *SqliteMigrateSuite) TestEmbedSource(c *C) {
818+
migrations := EmbedFileSystemMigrationSource{
819+
FileSystem: testEmbedFS,
820+
Root: "test-migrations",
821+
}
822+
823+
// Executes two migrations
824+
n, err := Exec(s.Db, "sqlite3", migrations, Up)
825+
c.Assert(err, IsNil)
826+
c.Assert(n, Equals, 2)
827+
828+
// Has data
829+
id, err := s.DbMap.SelectInt("SELECT id FROM people")
830+
c.Assert(err, IsNil)
831+
c.Assert(id, Equals, int64(1))
832+
}

0 commit comments

Comments
 (0)