Files
job-search/internal/cli/migrate/down.go
T
bwilliams e19c8c2b69 Imports split into three groups: standard library, third-party, then this project's own packages
gci is configured with the sections standard, default and localmodule, and
goimports with the module path as its local prefix, so both formatters
agree.
2026-10-14 17:40:06 +01:00

79 lines
1.9 KiB
Go

package migrate
import (
"errors"
"fmt"
"log/slog"
"github.com/spf13/cobra"
"git.avsdev.uk/Go/job-search/internal/cli"
"git.avsdev.uk/Go/job-search/internal/store"
)
func commandDown(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "down",
Short: "Migrate SQLite database to a previous version",
Long: `Roll back the most recent migrations.
--steps 0 rolls back every migration which deletes all data and must be
confirmed by providing -yes.`,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.down.PersistentPreRun")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("migrate.down.PersistentPostRun")
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
slog.Debug("migrate.down.Run")
yes, _ := cmd.Flags().GetBool("yes")
steps, _ := cmd.Flags().GetUint("steps")
m, db, err := openMigrator(cmd, opts, false, false)
if err != nil {
return err
}
defer closeDB(db, &err)
results, err := store.StepDown(cmd.Context(), m, int(steps), yes)
if len(results) == 0 && err == nil {
fmt.Println("No changes made to database (empty)")
return nil
}
if err == store.ErrWouldEmptyDatabase {
return fmt.Errorf(
"%w\n\nTo confirm you wish to proceed, pass the --yes flag", err,
)
}
if printErr := printResults(cmd.OutOrStdout(), results); printErr != nil {
return errors.Join(err, printErr)
}
if err != nil {
return failure(cmd.OutOrStdout(), err)
}
return printVersion(cmd.Context(), cmd.OutOrStdout(), m)
},
}
cmd.Flags().Uint(
"steps",
0,
"number of migrations to roll back. 0 = all (deletes all data)",
)
if err := cmd.MarkFlagRequired("steps"); err != nil {
panic(err) // only fails if the flag isn't defined
}
cmd.Flags().BoolP(
"yes", "y",
false,
"confirm you are intending to roll back every migration",
)
return cmd
}