forked from runabol/tork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.go
40 lines (37 loc) · 1.02 KB
/
migrate.go
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
36
37
38
39
40
package cli
import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/runabol/tork/conf"
"github.com/runabol/tork/datastore"
"github.com/runabol/tork/db/postgres"
ucli "github.com/urfave/cli/v2"
)
func (c *CLI) migrationCmd() *ucli.Command {
return &ucli.Command{
Name: "migration",
Usage: "Run the db migration script",
Action: migration,
}
}
func migration(ctx *ucli.Context) error {
dstype := conf.StringDefault("datastore.type", datastore.DATASTORE_INMEMORY)
switch dstype {
case datastore.DATASTORE_POSTGRES:
dsn := conf.StringDefault(
"datastore.postgres.dsn",
"host=localhost user=tork password=tork dbname=tork port=5432 sslmode=disable",
)
pg, err := datastore.NewPostgresDataStore(dsn)
if err != nil {
return err
}
if err := pg.ExecScript(postgres.SCHEMA); err != nil {
return errors.Wrapf(err, "error when trying to create db schema")
}
default:
return errors.Errorf("can't perform db migration on: %s", dstype)
}
log.Info().Msg("migration completed!")
return nil
}