[MM-41576] Revamp database schema version (#19586)

* revamp db version and add applied migrations endpoint

* replace old schema version with new

* add db version subcommand

* add to local api

* reflect review comments

* log errors

* remove setting the version from model.CurrentVersion

* fix a test
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-03-08 18:13:37 +03:00
коммит произвёл GitHub
родитель 05503440a6
Коммит 645fee3fe3
37 изменённых файлов: 379 добавлений и 46 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package commands
import (
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -51,13 +52,21 @@ var MigrateCmd = &cobra.Command{
RunE: migrateCmdF,
}
var DBVersionCmd = &cobra.Command{
Use: "version",
Short: "Returns the recent applied version number",
RunE: dbVersionCmdF,
}
func init() {
ResetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.")
DBVersionCmd.Flags().Bool("all", false, "Returns all applied migrations")
DbCmd.AddCommand(
InitDbCmd,
ResetCmd,
MigrateCmd,
DBVersionCmd,
)
RootCmd.AddCommand(
@@ -137,3 +146,35 @@ func migrateCmdF(command *cobra.Command, args []string) error {
return nil
}
func dbVersionCmdF(command *cobra.Command, args []string) error {
cfgDSN := getConfigDSN(command, config.GetEnvironment())
cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true)
if err != nil {
return errors.Wrap(err, "failed to load configuration")
}
config := cfgStore.Get()
store := sqlstore.New(config.SqlSettings, nil)
defer store.Close()
allFlag, _ := command.Flags().GetBool("all")
if allFlag {
applied, err2 := store.GetAppliedMigrations()
if err2 != nil {
return errors.Wrap(err2, "failed to get applied migrations")
}
for _, migration := range applied {
CommandPrettyPrintln(fmt.Sprintf("Varsion: %d, Name: %s", migration.Version, migration.Name))
}
return nil
}
v, err := store.GetDBSchemaVersion()
if err != nil {
return errors.Wrap(err, "failed to get schema version")
}
CommandPrettyPrintln("Current database schema version is: " + strconv.Itoa(v))
return nil
}