Short and single-letter names in application code become full words (tests keep theirs). Wrapped calls, signatures and composite literals put one item per line, and one-line function bodies are expanded. Exported names and groups of constants get doc comments, and comments refer to the renamed identifiers.
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package migrate
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/cli"
|
|
"git.avsdev.uk/Go/job-search/internal/store"
|
|
)
|
|
|
|
// runStepUp applies steps pending migrations (0 means all), printing each one
|
|
// and the resulting version. create says the user already agreed to create a
|
|
// missing database file; otherwise they are asked.
|
|
func runStepUp(
|
|
cmd *cobra.Command, opts *cli.Options, steps int, create bool,
|
|
) (err error) {
|
|
migrator, db, err := openMigrator(cmd, opts, true, create)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer closeDB(db, &err)
|
|
|
|
results, err := store.StepUp(cmd.Context(), migrator, int(steps))
|
|
|
|
if len(results) == 0 && err == nil {
|
|
_, err := fmt.Fprintln(cmd.OutOrStdout(), "No changes made to database (up-to-date)")
|
|
return err
|
|
}
|
|
|
|
if err := printResults(cmd.OutOrStdout(), results); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err != nil {
|
|
return failure(cmd.OutOrStdout(), err)
|
|
}
|
|
|
|
return printVersion(cmd.Context(), cmd.OutOrStdout(), migrator)
|
|
}
|
|
|
|
func commandUp(opts *cli.Options) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "up",
|
|
Short: "Migrate SQLite database to the latest version, or by a number of steps",
|
|
Args: cobra.NoArgs,
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
slog.Debug("command persistent pre-run", "command", "migrate.up")
|
|
},
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
slog.Debug("command persistent post-run", "command", "migrate.up")
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
slog.Debug("command run", "command", "migrate.up")
|
|
|
|
steps, _ := cmd.Flags().GetUint("steps")
|
|
create, _ := cmd.Flags().GetBool("create")
|
|
|
|
return runStepUp(cmd, opts, int(steps), create)
|
|
},
|
|
}
|
|
cmd.Flags().Uint(
|
|
"steps",
|
|
0,
|
|
"number of migrations to apply. 0 = all pending",
|
|
)
|
|
cmd.Flags().Bool(
|
|
"create",
|
|
false,
|
|
"create the database file if it does not already exist",
|
|
)
|
|
return cmd
|
|
}
|