migrations

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the global migration registry.

Functions

func ComputeSchemaHash

func ComputeSchemaHash(cfg config.Config) string

ComputeSchemaHash computes a deterministic SHA-256 hash of the schema configuration. It includes CustomBlockTypes and SchemaRules, using sorted keys for determinism. Returns the first 16 hex characters (64 bits) for readability. Returns empty string if no schema constraints are defined.

func ListBackups

func ListBackups(rootDir string) ([]string, error)

ListBackups returns a list of available backup directories.

func NeedsMigration

func NeedsMigration(targetDir string) (bool, string, string, error)

NeedsMigration checks if the project needs schema migration.

func RestoreBackup

func RestoreBackup(rootDir, backupDir string) error

RestoreBackup restores a project from a backup directory. It replaces the current config.yaml and nodes/ with the backup contents.

func SchemaVersionMatches

func SchemaVersionMatches(cfg config.Config) bool

SchemaVersionMatches checks if the config's SchemaVersion matches the computed hash. Returns true if they match or if there are no schema constraints.

Types

type BackupResult

type BackupResult struct {
	// BackupDir is the full path to the backup directory.
	BackupDir string
	// Timestamp is when the backup was created.
	Timestamp time.Time
	// NodeCount is the number of node files backed up.
	NodeCount int
}

BackupResult contains information about a created backup.

func CreateBackup

func CreateBackup(rootDir string) (*BackupResult, error)

CreateBackup creates a backup of the current project state. It creates a .deco/backup-<timestamp>/ directory containing: - config.yaml (copy of current config) - nodes/ (copy of all node files)

type Executor

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

Executor performs schema migrations.

func NewExecutor

func NewExecutor(opts ExecutorOptions, registry *Registry) *Executor

NewExecutor creates a new migration executor.

func (*Executor) Execute

func (e *Executor) Execute() (*ExecutorResult, error)

Execute runs the migration from current schema to target schema.

type ExecutorOptions

type ExecutorOptions struct {
	// DryRun if true, shows what would change without making changes.
	DryRun bool
	// NoBackup if true, skips creating a backup before migration.
	NoBackup bool
	// Quiet if true, suppresses non-essential output.
	Quiet bool
	// TargetDir is the project root directory.
	TargetDir string
}

ExecutorOptions configures the migration executor.

type ExecutorResult

type ExecutorResult struct {
	// NodesProcessed is the number of nodes processed.
	NodesProcessed int
	// NodesModified is the number of nodes that were actually changed.
	NodesModified int
	// BackupDir is the path to the backup (if created).
	BackupDir string
	// SourceHash is the schema hash before migration.
	SourceHash string
	// TargetHash is the schema hash after migration.
	TargetHash string
	// DryRun indicates if this was a dry run.
	DryRun bool
}

ExecutorResult contains the result of a migration execution.

type Migration

type Migration struct {
	// Name is a unique identifier for this migration.
	Name string
	// Description explains what this migration does.
	Description string
	// SourceHash is the schema hash this migration applies to.
	// Empty string matches any source (useful for initial migrations).
	SourceHash string
	// TargetHash is the schema hash after this migration.
	TargetHash string
	// Transform is the function that transforms each node.
	// If nil, this is an identity migration (updates schema version only).
	Transform TransformFunc
}

Migration represents a single schema migration.

func IdentityMigration

func IdentityMigration(name, description, sourceHash, targetHash string) Migration

IdentityMigration creates a migration that updates schema version without transforming nodes. Useful when schema changes are additive (new optional fields) and nodes don't need modification.

type Registry

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

Registry manages available migrations.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new migration registry.

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all migrations from the registry. Primarily useful for testing.

func (*Registry) Find

func (r *Registry) Find(sourceHash, targetHash string) *Migration

Find returns a migration that transforms from sourceHash to targetHash. Returns nil if no direct migration exists.

func (*Registry) FindPath

func (r *Registry) FindPath(sourceHash, targetHash string) []Migration

FindPath returns a sequence of migrations to get from sourceHash to targetHash. Uses breadth-first search to find the shortest path. Returns nil if no path exists.

func (*Registry) List

func (r *Registry) List() []Migration

List returns all registered migrations.

func (*Registry) Register

func (r *Registry) Register(m Migration) error

Register adds a migration to the registry.

type TransformFunc

type TransformFunc func(node domain.Node) (domain.Node, error)

TransformFunc is a function that transforms a node during migration. It receives the node and returns the transformed node (or error). The function should not modify the input node directly; return a new copy.

Jump to

Keyboard shortcuts

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