-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.go
36 lines (31 loc) · 907 Bytes
/
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
package cmd
import (
"github.com/lib/pq"
"github.com/spf13/cobra"
"github.com/diamnet/go/services/ticker/internal/tickerdb"
)
func init() {
rootCmd.AddCommand(cmdMigrate)
}
var cmdMigrate = &cobra.Command{
Use: "migrate",
Short: "Updates the database to the latest schema version.",
Run: func(cmd *cobra.Command, args []string) {
Logger.Info("Refreshing the asset database")
dbInfo, err := pq.ParseURL(DatabaseURL)
if err != nil {
Logger.Fatal("could not parse db-url:", err)
}
session, err := tickerdb.CreateSession("postgres", dbInfo)
if err != nil {
Logger.Fatal("could not connect to db:", err)
}
defer session.DB.Close()
Logger.Infoln("Upgrading the database")
n, err := tickerdb.MigrateDB(&session)
if err != nil {
Logger.Fatal("could not upgrade the database:", err)
}
Logger.Infof("Database Successfully Upgraded. Applied %d migrations.\n", n)
},
}