MCPcopy Create free account
hub / github.com/rilldata/rill / MigrateCmd

Function MigrateCmd

cli/cmd/admin/migrate.go:16–72  ·  view source on GitHub ↗
(ch *cmdutil.Helper)

Source from the content-addressed store, hash-verified

14)
15
16func MigrateCmd(ch *cmdutil.Helper) *cobra.Command {
17 cmd := &cobra.Command{
18 Use: "migrate",
19 Short: "Used to validate and run database migrations",
20 RunE: func(cmd *cobra.Command, args []string) error {
21 // Load .env (note: fails silently if .env has errors)
22 _ = godotenv.Load()
23 ctx := cmd.Context()
24
25 // Init config
26 var conf Config
27 err := envconfig.Process("rill_admin", &conf)
28 if err != nil {
29 fmt.Printf("failed to load config: %s\n", err.Error())
30 os.Exit(1)
31 }
32
33 // Init logger
34 cfg := zap.NewProductionConfig()
35 cfg.Level.SetLevel(conf.LogLevel)
36 cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
37 logger, err := cfg.Build()
38 if err != nil {
39 fmt.Printf("error: failed to create logger: %s\n", err.Error())
40 os.Exit(1)
41 }
42
43 // Init db
44 db, err := database.Open(conf.DatabaseDriver, conf.DatabaseURL, "")
45 if err != nil {
46 logger.Fatal("error connecting to database", zap.Error(err))
47 }
48
49 // Auto-run migrations
50 v1, err := db.FindMigrationVersion(ctx)
51 if err != nil {
52 logger.Fatal("error getting migration version", zap.Error(err))
53 }
54 err = db.Migrate(ctx)
55 if err != nil {
56 logger.Fatal("error migrating database", zap.Error(err))
57 }
58 v2, err := db.FindMigrationVersion(ctx)
59 if err != nil {
60 logger.Fatal("error getting migration version", zap.Error(err))
61 }
62 if v1 == v2 {
63 logger.Info("database is up to date", zap.Int("version", v2))
64 } else {
65 logger.Info("database migrated", zap.Int("from_version", v1), zap.Int("to_version", v2))
66 }
67
68 return nil
69 },
70 }
71 return cmd
72}

Callers 1

AdminCmdFunction · 0.85

Calls 7

OpenFunction · 0.92
PrintfMethod · 0.80
InfoMethod · 0.80
ContextMethod · 0.65
FindMigrationVersionMethod · 0.65
MigrateMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected