migrate

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package migrate applies versioned upgrades to ctm's JSON state files.

Each ctm-owned state file (config.json, sessions.json) carries a "schema_version" integer at its top level. On startup, ctm runs a Plan against each file: if the file is below the current version, it applies the pending Steps in order, stamps the new version, and atomically writes the result. The original bytes are saved to a timestamped sibling (".bak.<unix-nano>") before any destructive write so downgrade/recovery remains possible.

The runner is intentionally conservative:

  • It never creates a missing file (the owning package decides lifecycle).
  • It preserves the existing file mode on write.
  • It refuses to touch a file whose schema_version exceeds the current target, so an older ctm binary won't silently mangle state written by a newer binary (downgrade guard).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Plan

type Plan struct {
	Name           string // used in error messages only
	CurrentVersion int
	Steps          []Step
}

Plan describes the target version and the Steps to reach it from v0. len(Steps) must equal CurrentVersion: Steps[i] migrates vi → v(i+1).

type Result

type Result struct {
	Before int
	After  int
	Backup string
}

Result records what Run observed and did.

Before: the schema_version read from the file. -1 means the file did not exist. 0 means the file existed but lacked a schema_version key. After: the schema_version written. -1 when no write occurred (either because the file was absent or because it was already at CurrentVersion). Backup: path to the pre-migration backup, or "" when none was written.

func Run

func Run(path string, p Plan) (Result, error)

Run applies p to the file at path. See package doc for semantics.

type Step

type Step func(obj map[string]json.RawMessage) error

Step migrates a JSON object in-place from version v to v+1, where v is the index of the Step in Plan.Steps. A nil Step is a no-op (useful for version bumps that only need the stamp).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL