feat(upgrade): support upgrade from specific version number manually

This commit is contained in:
LinkinStars 2023-05-11 11:55:39 +08:00
parent ea54aa75db
commit 2ca32147b4
2 changed files with 12 additions and 2 deletions

View File

@ -22,6 +22,10 @@ var (
buildWithPlugins []string
// build output path
buildOutput string
// This config is used to upgrade the database from a specific version number manually.
// The version number is calculated with `migrations.go` migrations length.
// If you want to upgrade the database from version 1 to version 2, you can use `answer upgrade -f 1`.
upgradeFromVersionNumber int
)
func init() {
@ -35,6 +39,8 @@ func init() {
buildCmd.Flags().StringVarP(&buildOutput, "output", "o", "", "build output path")
upgradeCmd.Flags().IntVarP(&upgradeFromVersionNumber, "from", "f", 0, "upgrade from specific version number that in database version table")
for _, cmd := range []*cobra.Command{initCmd, checkCmd, runCmd, dumpCmd, upgradeCmd, buildCmd, pluginCmd} {
rootCmd.AddCommand(cmd)
}
@ -107,7 +113,7 @@ To run answer, use:
fmt.Println("read config failed: ", err.Error())
return
}
if err = migrations.Migrate(c.Data.Database, c.Data.Cache); err != nil {
if err = migrations.Migrate(c.Data.Database, c.Data.Cache, upgradeFromVersionNumber); err != nil {
fmt.Println("migrate failed: ", err.Error())
return
}

View File

@ -91,7 +91,7 @@ func ExpectedVersion() int64 {
}
// Migrate database to current version
func Migrate(dbConf *data.Database, cacheConf *data.CacheConf) error {
func Migrate(dbConf *data.Database, cacheConf *data.CacheConf, upgradeFromVersionNumber int) error {
cache, cacheCleanup, err := data.NewCache(cacheConf)
if err != nil {
fmt.Println("new check failed:", err.Error())
@ -107,6 +107,10 @@ func Migrate(dbConf *data.Database, cacheConf *data.CacheConf) error {
return err
}
expectedVersion := ExpectedVersion()
if upgradeFromVersionNumber > 0 {
fmt.Printf("[migrate] user set upgrade from version number %d\n", upgradeFromVersionNumber)
currentDBVersion = int64(upgradeFromVersionNumber)
}
for currentDBVersion < expectedVersion {
fmt.Printf("[migrate] current db version is %d, try to migrate version %d, latest version is %d\n",