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"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/pkg"
)
var (
HomepageJSONConfigPathOld = filepath.Join(common.ConfigDir, ".homepage.json")
IconListCachePathOld = filepath.Join(common.ConfigDir, ".icon_list_cache.json")
IconCachePathOld = filepath.Join(common.ConfigDir, ".icon_cache.json")
homepageJSONConfigPathOld = filepath.Join(common.ConfigDir, ".homepage.json")
iconListCachePathOld = filepath.Join(common.ConfigDir, ".icon_list_cache.json")
iconCachePathOld = filepath.Join(common.ConfigDir, ".icon_cache.json")
)
func m001_move_json_data() error {
if version.IsOlderThan(pkg.Version{Major: 0, Minor: 11, Patch: 0}) {
return errors.Join(
mv(HomepageJSONConfigPathOld, common.HomepageJSONConfigPath),
mv(IconListCachePathOld, common.IconListCachePath),
mv(IconCachePathOld, common.IconCachePath),
)
}
return nil
return errors.Join(
mv(homepageJSONConfigPathOld, common.HomepageJSONConfigPath),
mv(iconListCachePathOld, common.IconListCachePath),
mv(iconCachePathOld, common.IconCachePath),
)
}

View File

@@ -2,20 +2,29 @@ package migrations
import (
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/pkg"
)
func RunMigrations() error {
if currentVersion.IsEqual(lastVersion) {
return nil
}
logging.Info().Msg("running migrations...")
errs := gperr.NewBuilder("migration error")
for _, migration := range migrations {
if err := migration(); err != nil {
errs.Add(err)
for _, m := range migrations {
if !currentVersion.IsOlderThan(m.Since) {
continue
}
if err := m.Run(); err != nil {
errs.Add(gperr.PrependSubject(m.Name, err))
}
}
return errs.Error()
}
var version = pkg.GetVersion()
var migrations = []func() error{
m001_move_json_data,
var currentVersion = pkg.GetVersion()
var lastVersion = pkg.GetLastVersion()
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 {
if _, err := os.Stat(old); os.IsNotExist(err) {
_, err := os.Stat(old)
if err != nil && os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(new), 0o755); err != nil {
return err
}