migration

package
v0.0.6-beta Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT, MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCollectionNameRequired = errors.New("collection name is required")
	ErrCreateCollectionFailed = errors.New("create collection failed")
	ErrListCollectionsFailed  = errors.New("list collections failed")
)
View Source
var (
	ErrFailedToLock     = errors.New("could not acquire migration lock; another process may be running")
	ErrChecksumMismatch = errors.New("migration checksum mismatch")
	ErrUnknownMigration = errors.New("migration not registered")
)
View Source
var (
	ErrFailedToCreateFile      = errors.New("failed to create file")
	ErrFailedToParseTemplate   = errors.New("failed to parse template")
	ErrFailedToExecuteTemplate = errors.New("failed to execute template")
)
View Source
var (
	ErrCreateIndexesFailed = errors.New("create indexes failed")
	ErrDropIndexFailed     = errors.New("drop index failed")
	ErrIndexMustDefineKey  = errors.New("index must define at least one key")
)
View Source
var (
	ErrMigrationNil        = errors.New("migration must not be nil")
	ErrInvalidVersionFmt   = errors.New("invalid version format: expected YYYYMMDD[_slug]")
	ErrMigrationRegistered = errors.New("migration already registered")
)

Functions

func CreateIndexes

func CreateIndexes(ctx context.Context, coll *mongo.Collection, models ...mongo.IndexModel) error

func CreateIndexesWithOptions

func CreateIndexesWithOptions(
	ctx context.Context, coll *mongo.Collection, opts []IndexCreateOption, indexes ...*IndexBuilder,
) error

func DropIndexes

func DropIndexes(ctx context.Context, coll *mongo.Collection, names ...string) error

func EnsureCollection

func EnsureCollection(ctx context.Context, db *mongo.Database, name string,
	opts ...CollectionOption) (*mongo.Collection, error)

func FormatStatusTable

func FormatStatusTable(status []MigrationStatus) string

func GetMigrations

func GetMigrations(filters ...MigrationFilter) map[string]Migration

func MustRegister

func MustRegister(ms ...Migration)

func Register

func Register(m Migration) error

func RegisteredMigrations

func RegisteredMigrations() map[string]Migration

Types

type ChecksumDrift

type ChecksumDrift struct {
	Version       string `json:"version"`
	Stored        string `json:"stored"`
	Local         string `json:"local"`
	DriftDetected bool   `json:"drift_detected"`
}

type CollectionOption

type CollectionOption func(*options.CreateCollectionOptionsBuilder)

func WithCapped

func WithCapped(maxBytes int64, maxDocs int64) CollectionOption

func WithTimeSeries

func WithTimeSeries(timeField string, metaField string, granularity string) CollectionOption

func WithValidationAction

func WithValidationAction(action string) CollectionOption

func WithValidationLevel

func WithValidationLevel(level string) CollectionOption

func WithValidator

func WithValidator(validator interface{}) CollectionOption

type Direction

type Direction int
const (
	DirectionUp Direction = iota
	DirectionDown
)

func (Direction) String

func (d Direction) String() string

type Engine

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

func NewEngine

func NewEngine(db *mongo.Database, collection string) *Engine

func NewEngineWithMigrations

func NewEngineWithMigrations(db *mongo.Database, collection string, migrations map[string]Migration) *Engine

func (*Engine) ChecksumDrifts

func (e *Engine) ChecksumDrifts(ctx context.Context) ([]ChecksumDrift, error)

func (*Engine) Down

func (e *Engine) Down(ctx context.Context, target string) error

func (*Engine) Force

func (e *Engine) Force(ctx context.Context, version string) error

func (*Engine) ForceUnlock

func (e *Engine) ForceUnlock(ctx context.Context) error

func (*Engine) GetLockInfo

func (e *Engine) GetLockInfo(ctx context.Context) (LockInfo, error)

func (*Engine) GetStatus

func (e *Engine) GetStatus(ctx context.Context) ([]MigrationStatus, error)

GetStatus returns a full snapshot of registered migrations and their status.

func (*Engine) ListApplied

func (e *Engine) ListApplied(ctx context.Context) ([]MigrationRecord, error)

func (*Engine) Plan

func (e *Engine) Plan(ctx context.Context, direction Direction, target string) ([]string, error)

func (*Engine) SetLogger

func (e *Engine) SetLogger(logger *slog.Logger)

func (*Engine) Up

func (e *Engine) Up(ctx context.Context, target string) error

type ErrNotSupported

type ErrNotSupported struct {
	Operation string
}

func (ErrNotSupported) Error

func (e ErrNotSupported) Error() string

type Generator

type Generator struct {
	OutputPath string
}

func (*Generator) Create

func (g *Generator) Create(name string) (string, string, error)

type IndexBuilder

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

func Index

func Index(keys ...IndexKey) *IndexBuilder

func (*IndexBuilder) Key

func (b *IndexBuilder) Key(field string, order interface{}) *IndexBuilder

func (*IndexBuilder) Model

func (b *IndexBuilder) Model() mongo.IndexModel

func (*IndexBuilder) Name

func (b *IndexBuilder) Name(name string) *IndexBuilder

func (*IndexBuilder) Partial

func (b *IndexBuilder) Partial(expr interface{}) *IndexBuilder

func (*IndexBuilder) Sparse

func (b *IndexBuilder) Sparse() *IndexBuilder

func (*IndexBuilder) TTL

func (b *IndexBuilder) TTL(seconds int32) *IndexBuilder

func (*IndexBuilder) Unique

func (b *IndexBuilder) Unique() *IndexBuilder

type IndexCreateOption

type IndexCreateOption func(*indexCreateConfig)

func WithIndexNameOverride

func WithIndexNameOverride(fn func(string) string) IndexCreateOption

func WithIndexNamePrefix

func WithIndexNamePrefix(prefix string) IndexCreateOption

func WithIndexNameSuffix

func WithIndexNameSuffix(suffix string) IndexCreateOption

type IndexKey

type IndexKey struct {
	Field string
	Order interface{}
}

func Asc

func Asc(field string) IndexKey

func Desc

func Desc(field string) IndexKey

func Text

func Text(field string) IndexKey

type LockInfo

type LockInfo struct {
	Active     bool      `json:"active"`
	LockID     string    `json:"lock_id,omitempty" bson:"lock_id,omitempty"`
	Host       string    `json:"host,omitempty" bson:"host,omitempty"`
	PID        int       `json:"pid,omitempty" bson:"pid,omitempty"`
	AcquiredAt time.Time `json:"acquired_at,omitempty" bson:"acquired_at,omitempty"`
}

type Migration

type Migration interface {
	Version() string
	Description() string
	Up(ctx context.Context, db *mongo.Database) error
	Down(ctx context.Context, db *mongo.Database) error
}

type MigrationFilter

type MigrationFilter func(version string, m Migration) bool

type MigrationMetadata

type MigrationMetadata struct {
	ExecutionTime time.Duration `json:"execution_time" bson:"execution_time"`
	Error         string        `json:"error,omitempty" bson:"error,omitempty"`
}

type MigrationRecord

type MigrationRecord struct {
	Version     string    `json:"version" bson:"version"`
	Description string    `json:"description" bson:"description"`
	AppliedAt   time.Time `json:"applied_at" bson:"applied_at"`
	Checksum    string    `json:"checksum" bson:"checksum"`
}

MigrationRecord captures an applied migration entry for opslog output.

type MigrationStatus

type MigrationStatus struct {
	Version     string     `json:"version" bson:"version"`
	Description string     `json:"description" bson:"description"`
	Applied     bool       `json:"applied" bson:"applied"`
	AppliedAt   *time.Time `json:"applied_at,omitempty" bson:"applied_at,omitempty"`
}

type SchemaBuilder

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

func Schema

func Schema() *SchemaBuilder

func (*SchemaBuilder) Array

func (s *SchemaBuilder) Array(items any) bson.M

func (*SchemaBuilder) Bool

func (s *SchemaBuilder) Bool() bson.M

func (*SchemaBuilder) BsonType

func (s *SchemaBuilder) BsonType(t string) *SchemaBuilder

func (*SchemaBuilder) Build

func (s *SchemaBuilder) Build() bson.M

func (*SchemaBuilder) Date

func (s *SchemaBuilder) Date() bson.M

func (*SchemaBuilder) Field

func (s *SchemaBuilder) Field(name string, props any) *SchemaBuilder

func (*SchemaBuilder) Int

func (s *SchemaBuilder) Int() bson.M

func (*SchemaBuilder) Long

func (s *SchemaBuilder) Long() bson.M

func (*SchemaBuilder) Object

func (s *SchemaBuilder) Object(props bson.M) bson.M

func (*SchemaBuilder) Properties

func (s *SchemaBuilder) Properties(props bson.M) *SchemaBuilder

func (*SchemaBuilder) Required

func (s *SchemaBuilder) Required(fields ...string) *SchemaBuilder

func (*SchemaBuilder) String

func (s *SchemaBuilder) String() bson.M

Jump to

Keyboard shortcuts

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