This commit is contained in:
yusing 2025-04-14 15:36:24 +08:00
parent 82e2705f44
commit 69bc3acf15
5 changed files with 79 additions and 27 deletions

View file

@ -5,22 +5,18 @@ import (
"path/filepath" "path/filepath"
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/pkg"
) )
var ( var (
HomepageJSONConfigPathOld = filepath.Join(common.ConfigDir, ".homepage.json") homepageJSONConfigPathOld = filepath.Join(common.ConfigDir, ".homepage.json")
IconListCachePathOld = filepath.Join(common.ConfigDir, ".icon_list_cache.json") iconListCachePathOld = filepath.Join(common.ConfigDir, ".icon_list_cache.json")
IconCachePathOld = filepath.Join(common.ConfigDir, ".icon_cache.json") iconCachePathOld = filepath.Join(common.ConfigDir, ".icon_cache.json")
) )
func m001_move_json_data() error { func m001_move_json_data() error {
if version.IsOlderThan(pkg.Version{Major: 0, Minor: 11, Patch: 0}) { return errors.Join(
return errors.Join( mv(homepageJSONConfigPathOld, common.HomepageJSONConfigPath),
mv(HomepageJSONConfigPathOld, common.HomepageJSONConfigPath), mv(iconListCachePathOld, common.IconListCachePath),
mv(IconListCachePathOld, common.IconListCachePath), mv(iconCachePathOld, common.IconCachePath),
mv(IconCachePathOld, common.IconCachePath), )
)
}
return nil
} }

View file

@ -2,20 +2,29 @@ package migrations
import ( import (
"github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/pkg" "github.com/yusing/go-proxy/pkg"
) )
func RunMigrations() error { func RunMigrations() error {
if currentVersion.IsEqual(lastVersion) {
return nil
}
logging.Info().Msg("running migrations...")
errs := gperr.NewBuilder("migration error") errs := gperr.NewBuilder("migration error")
for _, migration := range migrations { for _, m := range migrations {
if err := migration(); err != nil { if !currentVersion.IsOlderThan(m.Since) {
errs.Add(err) continue
}
if err := m.Run(); err != nil {
errs.Add(gperr.PrependSubject(m.Name, err))
} }
} }
return errs.Error() return errs.Error()
} }
var version = pkg.GetVersion() var currentVersion = pkg.GetVersion()
var migrations = []func() error{ var lastVersion = pkg.GetLastVersion()
m001_move_json_data, var migrations = []migration{
{"move json data", m001_move_json_data, pkg.Ver(0, 11, 0)},
} }

9
migrations/migration.go Normal file
View file

@ -0,0 +1,9 @@
package migrations
import "github.com/yusing/go-proxy/pkg"
type migration struct {
Name string
Run func() error
Since pkg.Version
}

View file

@ -6,9 +6,13 @@ import (
) )
func mv(old, new string) error { func mv(old, new string) error {
if _, err := os.Stat(old); os.IsNotExist(err) { _, err := os.Stat(old)
if err != nil && os.IsNotExist(err) {
return nil return nil
} }
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(new), 0o755); err != nil { if err := os.MkdirAll(filepath.Dir(new), 0o755); err != nil {
return err return err
} }

View file

@ -2,20 +2,49 @@ package pkg
import ( import (
"fmt" "fmt"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/logging"
) )
func GetVersion() Version { func GetVersion() Version {
return Version{Major: major, Minor: minor, Patch: patch} return currentVersion
}
func GetLastVersion() Version {
return lastVersion
} }
func init() { func init() {
major, minor, patch = parseVersion(version) currentVersion = parseVersion(version)
// ignore errors
versionFile := filepath.Join(common.DataDir, "version")
var lastVersionStr string
f, err := os.OpenFile(versionFile, os.O_RDWR|os.O_CREATE, 0o644)
if err == nil {
_, err = fmt.Fscanf(f, "%s", &lastVersionStr)
lastVersion = parseVersion(lastVersionStr)
}
if err != nil && !os.IsNotExist(err) {
logging.Warn().Err(err).Msg("failed to read version file")
}
_, err = f.WriteString(version)
if err != nil {
logging.Warn().Err(err).Msg("failed to save version file")
}
} }
type Version struct{ Major, Minor, Patch int } type Version struct{ Major, Minor, Patch int }
func Ver(major, minor, patch int) Version {
return Version{major, minor, patch}
}
func (v Version) String() string { func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
} }
@ -49,11 +78,16 @@ func (v Version) IsEqual(other Version) bool {
} }
var ( var (
version = "unset" version = "unset"
major, minor, patch int currentVersion Version
lastVersion Version
) )
func parseVersion(v string) (major, minor, patch int) { func parseVersion(v string) (ver Version) {
if v == "" {
return
}
v = strings.Split(v, "-")[0] v = strings.Split(v, "-")[0]
v = strings.TrimPrefix(v, "v") v = strings.TrimPrefix(v, "v")
parts := strings.Split(v, ".") parts := strings.Split(v, ".")
@ -64,13 +98,13 @@ func parseVersion(v string) (major, minor, patch int) {
if err != nil { if err != nil {
return return
} }
minor, err = strconv.Atoi(parts[1]) minor, err := strconv.Atoi(parts[1])
if err != nil { if err != nil {
return return
} }
patch, err = strconv.Atoi(parts[2]) patch, err := strconv.Atoi(parts[2])
if err != nil { if err != nil {
return return
} }
return return Ver(major, minor, patch)
} }