revision

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package revision defines the persisted configuration revision model and the Store boundary. A revision holds one complete, normalized configuration payload; content is immutable once written and only the state and publish time change. At most one revision is published at a time.

Index

Constants

View Source
const HashPrefix = "sha256:"

HashPrefix prefixes every content hash.

Variables

View Source
var (
	ErrNotFound     = errors.New("revision: not found")
	ErrNoPublished  = errors.New("revision: no published revision")
	ErrConflict     = errors.New("revision: published revision changed concurrently")
	ErrInvalidState = errors.New("revision: invalid state transition")
	ErrHashMismatch = errors.New("revision: content hash mismatch")
	ErrNoRollback   = errors.New("revision: no earlier published revision to roll back to")
	ErrNotEmpty     = errors.New("revision: store is not empty")
)

Store errors.

Functions

func Hash

func Hash(payload []byte) string

Hash returns the content hash of a payload.

func ValidateHistory

func ValidateHistory(revs []Revision) error

ValidateHistory checks revisions for ImportHistory: unique positive IDs, known states, matching hashes, and at most one published revision.

Types

type Draft

type Draft struct {
	ParentID int64
	Payload  []byte
	Author   string
	Comment  string
}

Draft is the input for a new draft revision.

type MemoryStore

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

MemoryStore is an in-process Store. It is the reference implementation of the state machine and backs tests; it does not persist across restarts.

func NewMemoryStore

func NewMemoryStore(now func() time.Time) *MemoryStore

NewMemoryStore returns an empty MemoryStore. A nil now uses time.Now.

func (*MemoryStore) Close

func (m *MemoryStore) Close() error

Close implements Store.

func (*MemoryStore) Create

func (m *MemoryStore) Create(_ context.Context, d Draft) (Revision, error)

Create implements Store.

func (*MemoryStore) Get

func (m *MemoryStore) Get(_ context.Context, id int64) (Revision, error)

Get implements Store.

func (*MemoryStore) ImportHistory

func (m *MemoryStore) ImportHistory(_ context.Context, revs []Revision) error

ImportHistory implements Store.

func (*MemoryStore) List

func (m *MemoryStore) List(_ context.Context, limit int) ([]Revision, error)

List implements Store.

func (*MemoryStore) Publish

func (m *MemoryStore) Publish(_ context.Context, id, expectedCurrent int64, _ Meta) (Revision, error)

Publish implements Store.

func (*MemoryStore) Published

func (m *MemoryStore) Published(_ context.Context) (Revision, error)

Published implements Store.

func (*MemoryStore) Rollback

func (m *MemoryStore) Rollback(_ context.Context, expectedCurrent, target int64, meta Meta) (Revision, error)

Rollback implements Store.

type Meta

type Meta struct {
	Author  string
	Comment string
}

Meta records who performed a publish or rollback and why.

type Revision

type Revision struct {
	ID          int64
	ParentID    int64
	ContentHash string
	Payload     []byte
	State       State
	Author      string
	Comment     string
	CreatedAt   time.Time
	PublishedAt time.Time
}

Revision is one persisted configuration revision. ParentID 0 means none; a zero PublishedAt means the revision has never been published.

func RollbackTarget

func RollbackTarget(revs []Revision, current Revision, target int64) (Revision, error)

RollbackTarget selects the rollback target among revs for the published revision current: an explicit target must exist and not be a draft; otherwise the newest superseded revision whose content differs is chosen.

func (Revision) Summary

func (r Revision) Summary() Revision

Summary returns r without its payload, as listed by Store.List.

func (Revision) Verify

func (r Revision) Verify() error

Verify recomputes the content hash and fails closed on a mismatch.

type State

type State string

State is a revision lifecycle state.

const (
	StateDraft      State = "draft"
	StatePublished  State = "published"
	StateSuperseded State = "superseded"
	StateRolledBack State = "rolled-back"
)

Revision states.

func (State) Valid

func (s State) Valid() bool

Valid reports whether s is a known state.

type Store

type Store interface {
	// Create writes a new draft and returns it with its ID and hash.
	Create(ctx context.Context, d Draft) (Revision, error)
	// Get returns one revision with its payload.
	Get(ctx context.Context, id int64) (Revision, error)
	// List returns revision summaries, newest first; limit <= 0 means all.
	List(ctx context.Context, limit int) ([]Revision, error)
	// Published returns the published revision, or ErrNoPublished.
	Published(ctx context.Context) (Revision, error)
	// Publish publishes draft id. expectedCurrent is the caller's view of the
	// published ID (0 for none); a mismatch fails with ErrConflict.
	Publish(ctx context.Context, id, expectedCurrent int64, m Meta) (Revision, error)
	// Rollback marks the published revision rolled-back and publishes a new
	// revision copying target's payload. Target 0 selects the newest
	// superseded revision whose content differs from the published one.
	Rollback(ctx context.Context, expectedCurrent, target int64, m Meta) (Revision, error)
	// ImportHistory copies revisions verbatim into an empty store, keeping
	// IDs, states, authors and timestamps. It is used by migrate --history.
	ImportHistory(ctx context.Context, revs []Revision) error
	Close() error
}

Store persists revisions. Implementations must be safe for concurrent use and perform each state transition atomically.

Directories

Path Synopsis
Package revisiontest provides the conformance suite every revision.Store implementation must pass, so SQL stores keep the MemoryStore semantics.
Package revisiontest provides the conformance suite every revision.Store implementation must pass, so SQL stores keep the MemoryStore semantics.

Jump to

Keyboard shortcuts

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