migrations

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(m Migration)

Register adds a migration to the registry. This should be called from init() functions in migration files. Panics if an error occurs.

Types

type Migration

type Migration interface {
	// Name returns a unique identifier for this migration.
	// This identifier is used to track whether the migration has been applied.
	Name() string

	// Description returns a human-readable description of what this migration does.
	Description() string

	// FromVersion returns the provider version this migration applies from.
	// Migrations will only run when upgrading from a version < FromVersion.
	// For fresh installs (no previous version), migrations are skipped.
	// Return empty string to indicate this migration should always run (not recommended).
	FromVersion() string

	// Run executes the migration. This method must be idempotent.
	// If the migration has already been applied, this should be a no-op.
	// The context provides access to Kubernetes clients and other resources.
	// The registry tracks applied migrations via the state file, so Run()
	// should be safe to call multiple times.
	Run(ctx context.Context) error
}

Migration represents a single migration that can be executed. All migrations must be idempotent, running them multiple times should have the same effect as running them once.

func Get

func Get(name string) Migration

Get returns a migration by name, or nil if not found.

func GetAll

func GetAll() []Migration

GetAll returns all registered migrations.

type Registry

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

Registry manages migration discovery and execution.

func NewRegistry

func NewRegistry(stateManager *StateManager) *Registry

NewRegistry creates a new migration registry.

func (*Registry) GetPending

func (r *Registry) GetPending(ctx context.Context) ([]Migration, error)

GetPending returns all migrations that have not been applied yet. Migrations are only included if: 1. They haven't been applied (not in state file) 2. The previous provider version is less than or equal to the migration's FromVersion 3. For fresh installs (no previous version), all migrations are marked as applied and skipped

func (*Registry) RunMigrations

func (r *Registry) RunMigrations(ctx context.Context) (int, []error)

RunMigrations executes all pending migrations. Returns the number of migrations that were successfully executed. After successful execution, updates the provider version and applied migrations in state.

func (*Registry) SetCurrentVersion

func (r *Registry) SetCurrentVersion(version string)

SetCurrentVersion sets the current provider version. This should be called before GetPending or RunMigrations.

type State

type State struct {
	Applied         []string  `json:"applied"`
	LastRun         time.Time `json:"last_run"`
	ProviderVersion string    `json:"provider_version,omitempty"`
}

State represents the state of executed migrations.

type StateManager

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

StateManager handles reading and writing migration state to a file.

func NewStateManager

func NewStateManager(statePath string) *StateManager

NewStateManager creates a new StateManager with the given state file path.

func (*StateManager) GetProviderVersion

func (sm *StateManager) GetProviderVersion() (string, error)

GetProviderVersion returns the provider version from the state.

func (*StateManager) IsApplied

func (sm *StateManager) IsApplied(name string) (bool, error)

IsApplied checks if a migration with the given name has been applied.

func (*StateManager) Load

func (sm *StateManager) Load() (*State, error)

Load reads the migration state from the file. If the file doesn't exist, returns an empty state.

func (*StateManager) MarkApplied

func (sm *StateManager) MarkApplied(name string) error

MarkApplied adds a migration name to the applied list and saves the state.

func (*StateManager) Save

func (sm *StateManager) Save(state *State) error

Save writes the migration state to the file. Creates the directory if it doesn't exist.

func (*StateManager) SetProviderVersion

func (sm *StateManager) SetProviderVersion(version string) error

SetProviderVersion sets the provider version in the state.

Jump to

Keyboard shortcuts

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