schema

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const CollectionName = "cis:schema"

CollectionName is the name of the mongo-db collection used to store schema migrations.

Variables

View Source
var Default = NewRegistry()

Default is the default schema registry and used by package level functions Add and ApplyMigrations.

View Source
var (
	ErrNotFound = errors.New("not found")
)

Common error messages when working with the schema package:.

Functions

func Add

func Add(m ...Migration)

Add adds m to the default schema registry.

func ApplyMigrations

func ApplyMigrations(ctx context.Context, db Database, cli *mongo.Database) (bool, error)

ApplyMigrations applies all migrations added to the default schema registry. See SchemaRegistry.ApplyMigrations for more information.

Types

type Database

type Database interface {
	// Load should return the migration record stored at key.
	Load(ctx context.Context, key string) (*MigrationRecord, error)
	// Save should create a new migration record for key and version
	// overwritting any previously created migration record with the
	// same key.
	Save(ctx context.Context, key, version string) error
}

Database stores schema and applied migrations.

func NewDatabaseFromClient

func NewDatabaseFromClient(ctx context.Context, dbName string, cli *mongo.Client) (Database, error)

NewDatabaseFromClient returns a new schema database.

type MigrateFunc

type MigrateFunc func(ctx context.Context, from, to *version.Version, cli *mongo.Database) error

MigrateFunc is the function called to perform the migration between the versions from and to.

type Migration

type Migration struct {
	// Description provides a short description of the migration.
	Description string
	// Key identifies the subsystem / migratable part this migration is
	// for.
	Key string
	// Version is the semver that requires this migration to be run.
	Version string
	// MigrateFunc is called when the migration is applied.
	MigrateFunc MigrateFunc
	// BackupCollections can be set to a list of collection names
	// that should be dumped to disk prior to calling MigrateFunc.
	// In case an error is returned by MigrateFunc, all collections
	// listed here are dropped and re-created from the saved dump.
	BackupCollections []string
}

type MigrationRecord

type MigrationRecord struct {
	// Key is the key of the subsystem the migration applies to.
	Key string `json:"key" bson:"key"`
	// Version is the version of the subsystem after this migration
	// executed.
	Version string `json:"version" bson:"version"`
	// MigratedAt holds the time the migration was applied.
	MigratedAt time.Time `json:"migratedAt" bson:"migratedAt"`
}

MigrationRecord defines the record stored in the mongodb schema collection and represents a successfully applied migration.

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Add

func (sr *Registry) Add(migration ...Migration)

func (*Registry) ApplyMigrations

func (sr *Registry) ApplyMigrations(ctx context.Context, db Database, cli *mongo.Database) (bool, error)

Jump to

Keyboard shortcuts

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