The data folder had no record of which layout wrote it, so a new release could not tell old data from current data. <DataRoot>\.reims-go-data.json now records data_version and the app version that last raised it. It is kept out of reims-go.json, which is rewritten whole on every save and would drop the field under an older build. Each build carries every step from older layouts to its own; the GUI and CLI run them in order before reading the folder, so users can jump straight from any old version. A failed step leaves the version at the last success and resumes next launch; a folder from a newer build is left untouched. Step v1 writes the driver ownership marker into every reims-ctl VM. driver.RenamedDriverIDs maps old driver IDs to new ones for when a driver moves to another plugin; markers are not rewritten, so downgrades still resolve. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
4.3 KiB
Go
147 lines
4.3 KiB
Go
// Package datamigrate versions the data folder and upgrades it in place.
|
|
//
|
|
// The data folder records the data layout version it is at in StampFile.
|
|
// Every release carries the steps from older layouts to its own and runs
|
|
// them on startup, so users can jump straight from an old release to a new
|
|
// one. A release that has dropped the steps for very old layouts can tell
|
|
// from the stamp that an intermediate release is needed.
|
|
//
|
|
// The stamp lives in its own file, not in reims-go.json, because
|
|
// config.Save rewrites that file from its struct and an older release
|
|
// would drop a field it does not know.
|
|
package datamigrate
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
// StampFile sits at the root of the data folder.
|
|
const StampFile = ".reims-go-data.json"
|
|
|
|
// CurrentVersion is the data layout this build writes. Bump it together
|
|
// with a new Step.
|
|
const CurrentVersion = 1
|
|
|
|
// Stamp is the content of StampFile.
|
|
type Stamp struct {
|
|
DataVersion int `json:"data_version"`
|
|
WrittenBy string `json:"written_by,omitempty"` // app version that last raised DataVersion
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// Step upgrades the data folder to layout To. Steps must be idempotent: a
|
|
// step that fails part way is run again on the next start.
|
|
type Step struct {
|
|
To int
|
|
Name string
|
|
Run func(dataRoot string) error
|
|
}
|
|
|
|
// steps lists every upgrade in ascending To order.
|
|
var steps = []Step{
|
|
{To: 1, Name: "vm-owner-markers", Run: backfillOwnerMarkers},
|
|
}
|
|
|
|
// Result reports what Run did.
|
|
type Result struct {
|
|
From int // version found on disk (0 when unstamped)
|
|
To int // version on disk afterwards
|
|
Applied []string // names of the steps that ran
|
|
// Newer is true when the folder was written by a newer release. Nothing
|
|
// is changed then; the newer release owns the layout.
|
|
Newer bool
|
|
}
|
|
|
|
// Run brings the data folder at dataRoot up to CurrentVersion. A missing or
|
|
// empty dataRoot is not an error: there is nothing to migrate yet.
|
|
func Run(dataRoot, appVersion string) (Result, error) {
|
|
return run(dataRoot, appVersion, steps, CurrentVersion, time.Now)
|
|
}
|
|
|
|
func run(dataRoot, appVersion string, steps []Step, current int, now func() time.Time) (Result, error) {
|
|
if dataRoot == "" {
|
|
return Result{}, nil
|
|
}
|
|
if st, err := os.Stat(dataRoot); err != nil || !st.IsDir() {
|
|
return Result{}, nil
|
|
}
|
|
|
|
stamp, readErr := ReadStamp(dataRoot)
|
|
if readErr != nil {
|
|
// Steps are idempotent, so a damaged stamp is safe to rebuild from 0.
|
|
stamp = Stamp{}
|
|
}
|
|
res := Result{From: stamp.DataVersion, To: stamp.DataVersion}
|
|
if stamp.DataVersion > current {
|
|
res.Newer = true
|
|
return res, nil
|
|
}
|
|
|
|
for _, s := range steps {
|
|
if s.To <= res.To || s.To > current {
|
|
continue
|
|
}
|
|
if err := s.Run(dataRoot); err != nil {
|
|
if res.To > res.From {
|
|
_ = writeStamp(dataRoot, Stamp{DataVersion: res.To, WrittenBy: appVersion, UpdatedAt: now()})
|
|
}
|
|
return res, fmt.Errorf("data migration %q (to v%d): %w", s.Name, s.To, err)
|
|
}
|
|
res.To = s.To
|
|
res.Applied = append(res.Applied, s.Name)
|
|
}
|
|
|
|
// A folder already stamped at this layout is left untouched; anything
|
|
// else (steps ran, no stamp yet, damaged stamp) gets a fresh stamp.
|
|
if stamp.DataVersion == current && readErr == nil && stampExists(dataRoot) {
|
|
return res, nil
|
|
}
|
|
res.To = current
|
|
if err := writeStamp(dataRoot, Stamp{DataVersion: current, WrittenBy: appVersion, UpdatedAt: now()}); err != nil {
|
|
return res, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// ReadStamp reads StampFile. A folder without one is at version 0.
|
|
func ReadStamp(dataRoot string) (Stamp, error) {
|
|
raw, err := os.ReadFile(filepath.Join(dataRoot, StampFile))
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return Stamp{}, nil
|
|
}
|
|
if err != nil {
|
|
return Stamp{}, err
|
|
}
|
|
var s Stamp
|
|
if err := json.Unmarshal(raw, &s); err != nil {
|
|
return Stamp{}, fmt.Errorf("parse %s: %w", StampFile, err)
|
|
}
|
|
if s.DataVersion < 0 {
|
|
return Stamp{}, fmt.Errorf("%s: negative data_version", StampFile)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func stampExists(dataRoot string) bool {
|
|
_, err := os.Stat(filepath.Join(dataRoot, StampFile))
|
|
return err == nil
|
|
}
|
|
|
|
func writeStamp(dataRoot string, s Stamp) error {
|
|
raw, err := json.MarshalIndent(s, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
path := filepath.Join(dataRoot, StampFile)
|
|
tmp := path + ".tmp"
|
|
if err := os.WriteFile(tmp, append(raw, '\n'), 0o644); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp, path)
|
|
}
|