diff --git a/db/migrate.go b/db/migrate.go index d288b62f..b6ebaefb 100644 --- a/db/migrate.go +++ b/db/migrate.go @@ -53,12 +53,7 @@ func DropMigrationsTable(dbMap *gorp.DbMap) error { func migrationSource(dbMap *gorp.DbMap) (src migrate.MigrationSource, dialect string, err error) { switch dbMap.Dialect.(type) { case gorp.PostgresDialect: - src = &migrate.AssetMigrationSource{ - Dir: migrationDir, - Asset: migrations.Asset, - AssetDir: migrations.AssetDir, - } - return src, "postgres", nil + return migrations.PostgresMigrations, "postgres", nil case gorp.SqliteDialect: src = &migrate.MemoryMigrationSource{ Migrations: []*migrate.Migration{ diff --git a/db/migrations/gen_assets.go b/db/migrations/gen_assets.go new file mode 100644 index 00000000..57013077 --- /dev/null +++ b/db/migrations/gen_assets.go @@ -0,0 +1,72 @@ +// +build ignore + +package main + +import ( + "io/ioutil" + "log" + "os" + "path/filepath" + "sort" + "strconv" + "text/template" +) + +var funcs = template.FuncMap{ + "quote": strconv.Quote, +} + +var tmpl = template.Must(template.New("assets.go").Delims("{%", "%}").Funcs(funcs).Parse(`package migrations + +// THIS FILE WAS GENERATED BY gen.go DO NOT EDIT + +import "github.com/rubenv/sql-migrate" + +var PostgresMigrations migrate.MigrationSource = &migrate.MemoryMigrationSource{ + Migrations: []*migrate.Migration{{% range $i, $m := . %} + { + Id: {% $m.Name | quote %}, + Up: []string{ + {% $m.Data | quote %}, + }, + },{% end %} + }, +} +`)) + +// A single postgres migration. +type migration struct { + Name string + Data string +} + +func init() { + log.SetPrefix("gen_assets: ") + log.SetFlags(0) +} + +func main() { + files, err := filepath.Glob("*.sql") + if err != nil { + log.Fatalf("finding sql files: %v", err) + } + + sort.Strings(files) + migrations := make([]migration, len(files)) + for i, f := range files { + data, err := ioutil.ReadFile(f) + if err != nil { + log.Fatalf("reading file: %v", err) + } + migrations[i] = migration{f, string(data)} + } + + f, err := os.OpenFile("assets.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + log.Fatalf("creating file: %v", err) + } + defer f.Close() + if err := tmpl.Execute(f, migrations); err != nil { + log.Fatal(err) + } +} diff --git a/db/migrations/migrations.go b/db/migrations/migrations.go index 83931c8a..2013dafa 100644 --- a/db/migrations/migrations.go +++ b/db/migrations/migrations.go @@ -1,6 +1,3 @@ package migrations -// To download go-bindata run `go get -u github.com/jteeuwen/go-bindata/...` - -//go:generate go-bindata -modtime=1 -pkg migrations -o assets.go -ignore \.go$ -prefix "../.." ../../db/migrations -//go:generate gofmt -w assets.go +//go:generate go run gen_assets.go