Migration

/app/cmd/migrate.go (1.1 KB)

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cmd

import (
"context"

"github.com/muesli/coral"
"github.com/pkg/errors"

"{{{ .Package }}}/app/lib/database"
"{{{ .Package }}}/app/lib/database/migrate"
"{{{ .Package }}}/app/lib/log"
"{{{ .Package }}}/queries/migrations"
)

func migrateCmd() *coral.Command {
f := func(*coral.Command, []string) error { return runMigrations(context.Background()) }
ret := &coral.Command{Use: "migrate", Short: "Runs database migrations and exits", RunE: f}
return ret
}

func runMigrations(ctx context.Context) error {
logger, _ := log.InitLogging(false){{{ if .HasModule "postgres" }}}
db, err := database.OpenDefaultPostgres(ctx, logger){{{ else }}}{{{ if .HasModule "sqlite" }}}
db, err := database.OpenDefaultSQLite(ctx, logger){{{ else }}}{{{ if .HasModule "sqlserver" }}}
db, err := database.OpenDefaultSQLServer(ctx, logger){{{ end }}}{{{ end }}}{{{ end }}}
if err != nil {
return errors.Wrap(err, "unable to open database")
}
migrations.LoadMigrations(_flags.Debug)
err = migrate.Migrate(ctx, db, logger)
if err != nil {
return errors.Wrap(err, "unable to run database migrations")
}
return nil
}