Documentation
¶
Index ¶
- Constants
- Variables
- func DeleteIntermediateState(w db.KeyValueWriter, migrationIndex uint8) error
- func GetIntermediateState(r db.KeyValueReader, migrationIndex uint8) ([]byte, error)
- func RunWithServer(log utils.StructuredLogger, host string, port uint16, fn func() error) error
- func WriteIntermediateState(w db.KeyValueWriter, migrationIndex uint8, state []byte) error
- func WriteSchemaMetadata(txn db.KeyValueWriter, sm SchemaMetadata) error
- type Migration
- type MigrationRunner
- type Registry
- func (r *Registry) Count() int
- func (r *Registry) Entries() []Migration
- func (r *Registry) OptionalMigrationFlags() []string
- func (r *Registry) TargetVersion() SchemaVersion
- func (r *Registry) With(m Migration) *Registry
- func (r *Registry) WithOptional(m Migration, enabled bool, name string) *Registry
- type SchemaMetadata
- type SchemaVersion
- func (sv SchemaVersion) Contains(other SchemaVersion) bool
- func (sv SchemaVersion) Difference(other SchemaVersion) SchemaVersion
- func (sv SchemaVersion) Has(index uint8) bool
- func (sv SchemaVersion) HighestBit() int
- func (sv SchemaVersion) Iter() iter.Seq[uint8]
- func (sv SchemaVersion) Len() int
- func (sv *SchemaVersion) Set(index uint8)
- func (sv SchemaVersion) String() string
- func (sv SchemaVersion) Union(other SchemaVersion) SchemaVersion
Constants ¶
const ShutdownTimeout = 5 * time.Second
Variables ¶
var SchemaIntermediateState = typed.NewBucket( db.SchemaIntermediateState, key.Uint8, value.Bytes, )
Schema intermediate state represented as []byte
var SchemaMetadataBucket = typed.NewBucket( db.SchemaMetadata, key.Empty, value.Cbor[SchemaMetadata](), )
Schema metadata represented as SchemaMetadata
Functions ¶
func DeleteIntermediateState ¶ added in v0.15.18
func DeleteIntermediateState(w db.KeyValueWriter, migrationIndex uint8) error
DeleteIntermediateState deletes the intermediate state for a specific migration.
func GetIntermediateState ¶ added in v0.15.18
func GetIntermediateState(r db.KeyValueReader, migrationIndex uint8) ([]byte, error)
GetIntermediateState gets the intermediate state for a specific migration.
func RunWithServer ¶ added in v0.15.18
RunWithServer starts a status HTTP server and runs the provided function. The server exposes health check endpoints:
- /live: Always returns 200 OK (liveness probe)
- /ready: Returns 503 Service Unavailable with message "Database migration in progress." (readiness probe - indicates operation is running)
The server is started in a goroutine, the function is executed, and then the server is shut down. If the function returns an error, it is returned.
func WriteIntermediateState ¶ added in v0.15.18
func WriteIntermediateState(w db.KeyValueWriter, migrationIndex uint8, state []byte) error
WriteIntermediateState writes the intermediate state for a specific migration.
func WriteSchemaMetadata ¶ added in v0.15.18
func WriteSchemaMetadata(txn db.KeyValueWriter, sm SchemaMetadata) error
WriteSchemaMetadata writes the schema metadata to the database.
Types ¶
type Migration ¶ added in v0.5.0
type Migration interface {
// Before restores internal state from a previous migration attempt.
// Called before each execution of Migrate(), including first-time runs.
// intermediateState is nil for first-time execution, non-nil when resuming.
Before(intermediateState []byte) error
// Migrate executes the migration logic.
// Returns (intermediateState, error):
// - (nil, nil): Migration complete
// - (state, nil): Migration in progress, will resume with this state
// - (state, error): Error occurred; state is saved if error is context.Canceled
// - (nil, error): Error occurred; any existing state is cleared
Migrate(
ctx context.Context,
database db.KeyValueStore,
network *utils.Network,
log utils.StructuredLogger,
) ([]byte, error)
}
Migration represents a migration that can be applied to the database.
Execution Flow:
- Before(intermediateState) is called to restore state before each execution attempt
- Migrate() is called to perform the migration work
- Based on Migrate() return values: - (nil, nil): Migration complete, marked as applied - (state, nil): Migration in progress, state saved for resumption - (state, error): Error occurred; if context.Canceled, state is saved if non-nil
Intermediate State:
- Before() receives intermediateState loaded from the database (nil on first run)
- Migrate() returns intermediateState to allow resumable migrations
- If Migrate() returns non-nil state, it's saved and the migration will resume on next run
- If Migrate() returns nil state, the migration is marked complete and state is cleared
- On cancellation, non-nil state is saved; nil state clears any existing state
This two-phase design separates state restoration from migration work, allowing Migrate() to focus solely on doing the work while Before() handles state management.
type MigrationRunner ¶ added in v0.15.18
type MigrationRunner struct {
// contains filtered or unexported fields
}
func NewRunner ¶ added in v0.15.18
func NewRunner( registry *Registry, database db.KeyValueStore, network *utils.Network, log utils.StructuredLogger, ) (*MigrationRunner, error)
NewRunner creates a migration runner that executes pending migrations.
Parameters:
- registry: Migration registry containing all migrations
- database: Database store
- network: Network configuration
- log: Logger for migration progress
type Registry ¶ added in v0.15.18
type Registry struct {
// contains filtered or unexported fields
}
Registry holds all migrations and builds the target version.
func NewRegistry ¶ added in v0.15.18
func NewRegistry() *Registry
NewRegistry creates a new migration registry.
func (*Registry) OptionalMigrationFlags ¶ added in v0.15.18
OptionalMigrationFlags returns the array of optional migration flags. The array is indexed by migration index, and the value is the flag/name used to enable this migration.
func (*Registry) TargetVersion ¶ added in v0.15.18
func (r *Registry) TargetVersion() SchemaVersion
TargetVersion returns the current target version. This is built incrementally as migrations are registered.
func (*Registry) With ¶ added in v0.15.18
With adds a mandatory migration to the registry. Mandatory migrations are automatically included in targetVersion. Returns the registry for method chaining.
func (*Registry) WithOptional ¶ added in v0.15.18
WithOptional adds an optional migration to the registry. If enabled is true, the migration is included in the target version. name is the flag/name used to enable this migration. Returns the registry for method chaining.
type SchemaMetadata ¶ added in v0.8.0
type SchemaMetadata struct {
CurrentVersion SchemaVersion // Applied migrations
LastTargetVersion SchemaVersion // Previous target (for opt-out prevention)
}
SchemaMetadata tracks the migration state of the database.
CurrentVersion: migrations that have been successfully applied to the database. LastTargetVersion: the target version from the previous migration run. This is used to prevent opt-out: users cannot disable migrations that were previously enabled. When a migration run starts, the current targetVersion is saved here to become the "last target" for the next run.
func GetSchemaMetadata ¶ added in v0.15.18
func GetSchemaMetadata(r db.KeyValueReader) (SchemaMetadata, error)
GetSchemaMetadata gets the schema metadata from the database.
type SchemaVersion ¶
type SchemaVersion uint64
SchemaVersion represents a schema version as a bitset. It uses a uint64 where each bit at index i represents whether migration i is included. Bits can only be set (never cleared), ensuring migrations are never "unapplied". Supports up to 64 migrations (bits 0-63).
func (SchemaVersion) Contains ¶ added in v0.15.18
func (sv SchemaVersion) Contains(other SchemaVersion) bool
Contains returns true if sv contains all bits that are set in other.
func (SchemaVersion) Difference ¶ added in v0.15.18
func (sv SchemaVersion) Difference(other SchemaVersion) SchemaVersion
Difference returns a new SchemaVersion with bits that are set in sv but not in other.
func (SchemaVersion) Has ¶ added in v0.15.18
func (sv SchemaVersion) Has(index uint8) bool
Has returns true if the bit at the given index is set.
func (SchemaVersion) HighestBit ¶ added in v0.15.18
func (sv SchemaVersion) HighestBit() int
HighestBit returns the highest set bit index (0-63). Returns -1 if no bits are set.
func (SchemaVersion) Iter ¶ added in v0.15.18
func (sv SchemaVersion) Iter() iter.Seq[uint8]
Iter returns an iterator over all set bit indices in ascending order.
func (SchemaVersion) Len ¶ added in v0.15.18
func (sv SchemaVersion) Len() int
Len returns the number of set bits in the SchemaVersion. This is the cardinality (size) of the set.
func (*SchemaVersion) Set ¶ added in v0.15.18
func (sv *SchemaVersion) Set(index uint8)
Set sets the bit at the given index.
func (SchemaVersion) String ¶ added in v0.15.18
func (sv SchemaVersion) String() string
String returns a string representation of the SchemaVersion.
func (SchemaVersion) Union ¶ added in v0.15.18
func (sv SchemaVersion) Union(other SchemaVersion) SchemaVersion
Union combines two SchemaVersions using bitwise OR.