diff --git a/.gitignore b/.gitignore index df6a79e..cd1410b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ dist/ uploads/* !uploads/.keep internal/frontend/frontend_generated.go +internal/migrations/migrations_generated.go citadel conf/app.toml diff --git a/internal/commands/migrate.go b/internal/commands/migrate.go index a87e77f..8edc590 100644 --- a/internal/commands/migrate.go +++ b/internal/commands/migrate.go @@ -7,8 +7,10 @@ import ( "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/postgres" _ "github.com/golang-migrate/migrate/v4/source/file" + "github.com/golang-migrate/migrate/v4/source/httpfs" "github.com/jmoiron/sqlx" "github.com/jordanknott/project-citadel/api/internal/config" + "github.com/jordanknott/project-citadel/api/internal/migrations" log "github.com/sirupsen/logrus" ) @@ -50,9 +52,12 @@ func newMigrateCmd() *cobra.Command { if err != nil { return err } - m, err := migrate.NewWithDatabaseInstance( - "file://migrations", - "postgres", driver) + + src, err := httpfs.New(migrations.Migrations, "./") + if err != nil { + return err + } + m, err := migrate.NewWithInstance("httpfs", src, "postgres", driver) if err != nil { return err } diff --git a/magefile.go b/magefile.go index 5feccb3..bb8c004 100644 --- a/magefile.go +++ b/magefile.go @@ -31,6 +31,22 @@ func (Frontend) Build() error { type Backend mg.Namespace +func (Backend) GenMigrations() error { + if _, err := os.Stat("internal/migrations"); os.IsNotExist(err) { + os.Mkdir("internal/migrations/", 0755) + } + var fs http.FileSystem = http.Dir("migrations") + err := vfsgen.Generate(fs, vfsgen.Options{ + Filename: "internal/migrations/migrations_generated.go", + PackageName: "migrations", + VariableName: "Migrations", + }) + if err != nil { + panic(err) + } + return nil +} + func (Backend) GenFrontend() error { if _, err := os.Stat("internal/frontend/"); os.IsNotExist(err) { os.Mkdir("internal/frontend/", 0755) @@ -83,7 +99,7 @@ func Install() { } func Build() { - mg.SerialDeps(Frontend.Build, Backend.GenFrontend, Backend.Build) + mg.SerialDeps(Frontend.Build, Backend.GenMigrations, Backend.GenFrontend, Backend.Build) } type Docker mg.Namespace