feat: embed schema migrations in binary

This commit is contained in:
Jordan Knott 2020-07-19 16:10:07 -05:00
parent e52620a9e2
commit 8eb41ab8a8
3 changed files with 26 additions and 4 deletions

1
.gitignore vendored
View File

@ -3,5 +3,6 @@ dist/
uploads/* uploads/*
!uploads/.keep !uploads/.keep
internal/frontend/frontend_generated.go internal/frontend/frontend_generated.go
internal/migrations/migrations_generated.go
citadel citadel
conf/app.toml conf/app.toml

View File

@ -7,8 +7,10 @@ import (
"github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres" "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file" _ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/source/httpfs"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/jordanknott/project-citadel/api/internal/config" "github.com/jordanknott/project-citadel/api/internal/config"
"github.com/jordanknott/project-citadel/api/internal/migrations"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -50,9 +52,12 @@ func newMigrateCmd() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
m, err := migrate.NewWithDatabaseInstance(
"file://migrations", src, err := httpfs.New(migrations.Migrations, "./")
"postgres", driver) if err != nil {
return err
}
m, err := migrate.NewWithInstance("httpfs", src, "postgres", driver)
if err != nil { if err != nil {
return err return err
} }

View File

@ -31,6 +31,22 @@ func (Frontend) Build() error {
type Backend mg.Namespace 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 { func (Backend) GenFrontend() error {
if _, err := os.Stat("internal/frontend/"); os.IsNotExist(err) { if _, err := os.Stat("internal/frontend/"); os.IsNotExist(err) {
os.Mkdir("internal/frontend/", 0755) os.Mkdir("internal/frontend/", 0755)
@ -83,7 +99,7 @@ func Install() {
} }
func Build() { func Build() {
mg.SerialDeps(Frontend.Build, Backend.GenFrontend, Backend.Build) mg.SerialDeps(Frontend.Build, Backend.GenMigrations, Backend.GenFrontend, Backend.Build)
} }
type Docker mg.Namespace type Docker mg.Namespace