git-project-manager/internal/cache/cache_migrations.go

58 lines
1.1 KiB
Go
Raw Normal View History

2024-12-31 22:45:45 +00:00
package cache
import (
"errors"
"fmt"
"golang.org/x/mod/semver"
)
type migrationFunc func(c *Cache) error
// Registry of migrations by version
var migrations = map[string]map[string]migrationFunc{
"v0.1.0": {
"Make Aliases Unique": v010_aliases,
},
}
// Performs any required updates based on version
// of cache read from disk.
// Does not check to ensure migrations were successful,
// only checks if a version has been achieved
func (c *Cache) DoMigrations() error {
c.lock.Lock()
defer c.lock.Unlock()
return c.doMigrations()
}
func (c *Cache) doMigrations() error {
var errs error
for version, migrationFuncs := range migrations {
if semver.Compare(c.CacheVersion, version) < 0 {
for name, migration := range migrationFuncs {
err := migration(c)
if err != nil {
errs = errors.Join(
errs,
fmt.Errorf("%s - %s: %w", version, name, err),
)
}
}
// We've reached a cache version, update the CacheVersion
// and write to disk
if errs == nil {
c.CacheVersion = version
c.write()
}
}
}
return errs
}
func v010_aliases(c *Cache) error {
return errors.New("unimplemented migration")
}