Go · 1393 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package main
4
5 import (
6 "github.com/spf13/cobra"
7
8 "github.com/tenseleyFlow/shithub/internal/infra/db"
9 _ "github.com/tenseleyFlow/shithub/internal/migrationsfs" // registers the embedded migrations FS with db
10 )
11
12 var migrateCmd = &cobra.Command{
13 Use: "migrate",
14 Short: "Run database migrations",
15 Long: "Run database migrations against SHITHUB_DATABASE_URL using goose. Subcommands: up, down, status, version, redo, reset.",
16 }
17
18 func newMigrateActionCmd(use, short string, action db.MigrateAction) *cobra.Command {
19 return &cobra.Command{
20 Use: use,
21 Short: short,
22 RunE: func(cmd *cobra.Command, args []string) error {
23 return db.Migrate(cmd.Context(), db.Defaults(), action)
24 },
25 }
26 }
27
28 func init() {
29 migrateCmd.AddCommand(newMigrateActionCmd("up", "Apply all pending migrations", db.MigrateUp))
30 migrateCmd.AddCommand(newMigrateActionCmd("down", "Roll back the most recent migration", db.MigrateDown))
31 migrateCmd.AddCommand(newMigrateActionCmd("status", "Show applied/pending migrations", db.MigrateStatus))
32 migrateCmd.AddCommand(newMigrateActionCmd("version", "Show current schema version", db.MigrateVersion))
33 migrateCmd.AddCommand(newMigrateActionCmd("redo", "Re-apply the most recent migration", db.MigrateRedo))
34 migrateCmd.AddCommand(newMigrateActionCmd("reset", "Roll back all migrations (DANGEROUS)", db.MigrateReset))
35 }
36