-
Notifications
You must be signed in to change notification settings - Fork 246
/
migrate.go
105 lines (88 loc) · 3.51 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package migrate
import (
"fmt"
"github.com/fatih/color"
"github.com/jzelinskie/cobrautil"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
crdbmigrations "github.com/authzed/spicedb/internal/datastore/crdb/migrations"
"github.com/authzed/spicedb/internal/datastore/postgres/migrations"
defaults "github.com/authzed/spicedb/pkg/cmd"
"github.com/authzed/spicedb/pkg/migrate"
)
func RegisterMigrateFlags(cmd *cobra.Command) {
cmd.Flags().String("datastore-engine", "memory", `type of datastore to initialize ("memory", "postgres", "cockroachdb")`)
cmd.Flags().String("datastore-conn-uri", "", `connection string used by remote datastores (e.g. "postgres://postgres:password@localhost:5432/spicedb")`)
}
func NewMigrateCommand(programName string) *cobra.Command {
return &cobra.Command{
Use: "migrate [revision]",
Short: "execute datastore schema migrations",
Long: fmt.Sprintf("Executes datastore schema migrations for the datastore.\nThe special value \"%s\" can be used to migrate to the latest revision.", color.YellowString(migrate.Head)),
PreRunE: defaults.DefaultPreRunE(programName),
Run: migrateRun,
Args: cobra.ExactArgs(1),
}
}
func migrateRun(cmd *cobra.Command, args []string) {
datastoreEngine := cobrautil.MustGetStringExpanded(cmd, "datastore-engine")
dbURL := cobrautil.MustGetStringExpanded(cmd, "datastore-conn-uri")
if datastoreEngine == "cockroachdb" {
log.Info().Msg("migrating cockroachdb datastore")
migrationDriver, err := crdbmigrations.NewCRDBDriver(dbURL)
if err != nil {
log.Fatal().Err(err).Msg("unable to create migration driver")
}
targetRevision := args[0]
log.Info().Str("targetRevision", targetRevision).Msg("running migrations")
err = crdbmigrations.CRDBMigrations.Run(migrationDriver, targetRevision, migrate.LiveRun)
if err != nil {
log.Fatal().Err(err).Msg("unable to complete requested migrations")
}
} else if datastoreEngine == "postgres" {
log.Info().Msg("migrating postgres datastore")
migrationDriver, err := migrations.NewAlembicPostgresDriver(dbURL)
if err != nil {
log.Fatal().Err(err).Msg("unable to create migration driver")
}
targetRevision := args[0]
log.Info().Str("targetRevision", targetRevision).Msg("running migrations")
err = migrations.DatabaseMigrations.Run(migrationDriver, targetRevision, migrate.LiveRun)
if err != nil {
log.Fatal().Err(err).Msg("unable to complete requested migrations")
}
} else {
log.Fatal().Str("datastore-engine", datastoreEngine).Msg("cannot migrate datastore engine type")
}
}
func RegisterHeadFlags(cmd *cobra.Command) {
cmd.Flags().String("datastore-engine", "postgres", "type of datastore to initialize (e.g. postgres, cockroachdb, memory")
}
func NewHeadCommand(programName string) *cobra.Command {
return &cobra.Command{
Use: "head",
Short: "compute the head database migration revision",
PreRunE: defaults.DefaultPreRunE(programName),
Run: headRevisionRun,
Args: cobra.ExactArgs(0),
}
}
func headRevisionRun(cmd *cobra.Command, args []string) {
var (
engine = cobrautil.MustGetStringExpanded(cmd, "datastore-engine")
headRevision string
err error
)
switch engine {
case "cockroachdb":
headRevision, err = crdbmigrations.CRDBMigrations.HeadRevision()
case "postgres":
headRevision, err = migrations.DatabaseMigrations.HeadRevision()
default:
log.Fatal().Str("engine", engine).Msg("cannot migrate datastore engine type")
}
if err != nil {
log.Fatal().Err(err).Msg("unable to compute head revision")
}
fmt.Println(headRevision)
}