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: 10 Imported by: 0

README

Scenario: Enrich products with external catalog data

This scenario demonstrates how to run an idempotent, checkpointed data backfill that calls an external API and updates MongoDB in batches.

Migration sequence

  1. 20240405_create_backfill_metadata.go – Adds a backfill_metadata collection used to track job state and throttle concurrency.
  2. 20240406_backfill_products_from_catalog.go – Streams product IDs in batches of 500, fetches supplemental data from a mock CatalogClient, and writes it back using $set operators so reruns are safe.
  3. 20240407_enforce_catalog_fields.go – Locks in the new schema by requiring the enriched fields in validators and indexes.

Key techniques

  • Resume-ability: Each batch persists the last processed _id plus a hash of the payload that was written.
  • Idempotency: Updates are applied with $set only on fields that differ from the incoming payload.
  • Rate limiting: The backfill sleeps between API calls to avoid overloading provider services.

The Go code uses interfaces so you can swap the CatalogClient implementation during tests.

Documentation

Index

Constants

This section is empty.

Variables

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 Asc

func Asc(field string) bson.D

func CreateIndexes

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

func Desc

func Desc(field string) bson.D

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 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

func Text

func Text(field string) bson.D

Types

type CollectionOption

type CollectionOption func(*options.CreateCollectionOptionsBuilder)

func WithValidationLevel

func WithValidationLevel(level string) CollectionOption

func WithValidator

func WithValidator(schema bson.M) CollectionOption

type IndexBuilder

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

func Index

func Index(keys any) *IndexBuilder

func (*IndexBuilder) Build

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

func (*IndexBuilder) Name

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

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 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, value 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