migration

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package migration lets FastConf rewrite the merged map from one schema version to another before it is decoded into the strongly typed snapshot. It addresses the "long-lived config + evolving struct" pain point: instead of forcing every operator to hand-edit every overlay file, a Migration shifts ageing keys (e.g. db.url -> db.dsn) on the fly during reload.

Migrations form an explicit chain keyed by _meta.schemaVersion. The chain runs after merge but before transform/decode; the final version is written back into _meta.schemaVersion so subsequent reloads fast-skip already-migrated input.

Index

Constants

View Source
const FieldKey = "schemaVersion"

FieldKey is the field inside MetaKey holding an int.

View Source
const MetaKey = "_meta"

MetaKey is the conventional path inside the merged map storing the current schema version: _meta.schemaVersion.

Variables

This section is empty.

Functions

func CurrentVersion

func CurrentVersion(m map[string]any) int

CurrentVersion reads m._meta.schemaVersion. Missing or non-int values are treated as version 0 (assume the oldest schema).

func SetVersion

func SetVersion(m map[string]any, v int)

SetVersion writes m._meta.schemaVersion = v, creating _meta if absent.

Types

type Chain

type Chain struct {
	// contains filtered or unexported fields
}

Chain is an ordered, validated set of Migrations that can be applied in sequence. Use New(...) to construct one.

func New

func New(target int, migrations ...Migration) (*Chain, error)

New builds a Chain ensuring every migration's From == previous.To (gap- and dup-free). target is the highest schemaVersion the chain can reach; clients should set it to the latest version their typed struct understands.

func (*Chain) Run

func (c *Chain) Run(m map[string]any) (int, error)

Run upgrades m to c.target, returning the final version. It is a no-op when m is already at or above target. Returns an error if the chain has a gap or any Apply fails (m may be partially mutated; the caller should discard m and roll back on error).

func (*Chain) Target

func (c *Chain) Target() int

Target returns the highest version the chain can reach.

type Migration

type Migration struct {
	From  int
	To    int
	Apply func(m map[string]any) error
}

Migration upgrades a merged configuration map from From to To.

Jump to

Keyboard shortcuts

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