Documentation
¶
Index ¶
- Variables
- func NewQueryError(err error, query string) error
- type Action
- type ActionType
- type BaseMigration
- func (migration *BaseMigration) CanUndo() bool
- func (migration *BaseMigration) Description() string
- func (migration *BaseMigration) Do(ctx context.Context) error
- func (migration *BaseMigration) ID() string
- func (migration *BaseMigration) Next() Migration
- func (migration *BaseMigration) Previous() Migration
- func (migration *BaseMigration) SetNext(value Migration) Migration
- func (migration *BaseMigration) SetPrevious(value Migration) Migration
- func (migration *BaseMigration) String() string
- func (migration *BaseMigration) Undo(ctx context.Context) error
- type ExecutionStats
- type ListSource
- type Migration
- type MigrationError
- type MigrationIDError
- type MigrationsError
- type Plan
- type Planner
- type PlannerFunc
- type ProgressReporter
- type QueryError
- type Runner
- type RunnerOption
- type RunnerReporter
- type Source
- type Target
- type TargetLocker
- type Unlocker
Constants ¶
This section is empty.
Variables ¶
var ( ErrNonUniqueMigrationID = errors.New("migration id is not unique") ErrMigrationNotFound = errors.New("migration not found") ErrNoCurrentMigration = errors.New("no current migration") ErrCurrentMigrationNotFound = errors.New("current migration not found in the list") ErrCurrentMigrationMoreRecent = errors.New("current migration is more recent than target migration") ErrNoMigrationsAvailable = errors.New("no migrations available") ErrMigrationNotUndoable = errors.New("migration cannot be undone") // ErrStepOutOfIndex is returned when a `StepResolver` cannot resolve a // migration due to the resolved index be outside of the migration list. ErrStepOutOfIndex = errors.New("step out of bounds") // ErrMigrationNotListed is returned when a migration is not found in the // `Source` list. ErrMigrationNotListed = errors.New("migration not in the source list") // ErrStaleMigrationDetected is returned when a migration with an ID eariler of the current applied migration is // detected. ErrStaleMigrationDetected = errors.New("stale migration detected") // ErrInvalidAction is returned when, while executing, the `Action.Action` // has an invalid value. ErrInvalidAction = errors.New("undefined action") )
var ( ErrInvalidFilename = errors.New("invalid filename") ErrInvalidMigrationID = errors.New("invalid migration ID") )
var (
// DefaultMigrationIDFormat is the default format for the migrations ID.
DefaultMigrationIDFormat = "20060102150405"
)
var DefaultSource = NewSource()
DefaultSource is the default source instance.
var (
MigrationIDNone = time.Unix(0, 0)
)
Functions ¶
func NewQueryError ¶
Types ¶
type Action ¶
type Action struct { Action ActionType Migration Migration }
type ActionType ¶
type ActionType string
const ( ActionTypeDo ActionType = "do" ActionTypeUndo ActionType = "undo" )
type BaseMigration ¶
type BaseMigration struct {
// contains filtered or unexported fields
}
func NewMigration ¶
func NewMigration(id, description string, do, undo migrationFunc) *BaseMigration
func (*BaseMigration) CanUndo ¶
func (migration *BaseMigration) CanUndo() bool
CanUndo is a flag that mark this flag as undoable.
func (*BaseMigration) Description ¶
func (migration *BaseMigration) Description() string
Description is the humanized description for the migration.
func (*BaseMigration) Do ¶
func (migration *BaseMigration) Do(ctx context.Context) error
Do will execute the migration.
func (*BaseMigration) ID ¶
func (migration *BaseMigration) ID() string
ID identifies the migration. Through the ID, all the sorting is done.
func (*BaseMigration) Next ¶
func (migration *BaseMigration) Next() Migration
Next will link this migration with the next. This link should be created by the source while it is being loaded.
func (*BaseMigration) Previous ¶
func (migration *BaseMigration) Previous() Migration
Previous will link this migration with the previous. This link should be created by the Source while it is being loaded.
func (*BaseMigration) SetNext ¶
func (migration *BaseMigration) SetNext(value Migration) Migration
SetNext will set the next migration
func (*BaseMigration) SetPrevious ¶
func (migration *BaseMigration) SetPrevious(value Migration) Migration
SetPrevious will set the previous migration
func (*BaseMigration) String ¶
func (migration *BaseMigration) String() string
String will return a representation of the migration into a string format for user identification.
type ExecutionStats ¶
func Migrate ¶
func Migrate(ctx context.Context, runner *Runner, runnerReporter RunnerReporter) (*ExecutionStats, error)
type ListSource ¶
type ListSource struct {
// contains filtered or unexported fields
}
ListSource is the default Source implementation that keep all the migrations on a list.
This source is meant to be used as base implementation for other specific Sources. Check out the sql.Source implementation.
func NewSource ¶
func NewSource() *ListSource
NewSource returns a new instance of the default implementation of the Source.
func (*ListSource) Add ¶
func (source *ListSource) Add(migration Migration) error
Add adds a Migration to this Source. It will
func (*ListSource) List ¶
func (source *ListSource) List() ([]Migration, error)
type Migration ¶
type Migration interface { // ID identifies the migration. Through the ID, all the sorting is done. ID() string // String will return a representation of the migration into a string format // for user identification. String() string // Description is the humanized description for the migration. Description() string // Next will link this migration with the next. This link should be created // by the source while it is being loaded. Next() Migration // SetNext will set the next migration SetNext(Migration) Migration // Previous will link this migration with the previous. This link should be // created by the Source while it is being loaded. Previous() Migration // SetPrevious will set the previous migration SetPrevious(Migration) Migration // Do will execute the migration. Do(ctx context.Context) error // CanUndo is a flag that mark this flag as undoable. CanUndo() bool // Undo will undo the migration. Undo(ctx context.Context) error }
Migration is the abstraction that defines the migration contract.
Migrations, by default, cannot be undone. But, if the final migration implement the `MigrationUndoable` interface, the system will let it be undone.
type MigrationError ¶
MigrationError wraps an error with a migration property.
func WrapMigration ¶
func WrapMigration(err error, migration Migration) MigrationError
WrapMigration creates a `MigrationError` based on an existing error.
type MigrationIDError ¶
MigrationCodeError wraps an error with a migration ID.
func WrapMigrationID ¶
func WrapMigrationID(err error, migrationID string) MigrationIDError
WrapMigrationID creates a `MigrationCodeError` based on an existing error.
type MigrationsError ¶
MigrationsError wraps an error with a list of migrations.
func WrapMigrations ¶
func WrapMigrations(err error, migrations ...Migration) MigrationsError
type PlannerFunc ¶
func StepPlanner ¶
func StepPlanner(step int) PlannerFunc
type ProgressReporter ¶
type QueryError ¶
type QueryError interface {
Query() string
}
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner will receive the `Plan` from the `Planner` and execute it.
func (*Runner) Execute ¶
func (runner *Runner) Execute(ctx context.Context, plan Plan, reporter RunnerReporter) (*ExecutionStats, error)
Execute performs a plan, running all actions migration by migration.
Before running, Execute will check for Undo actions that cannot be performmed into undoable migrations. If that happens, an `ErrMigrationNotUndoable` will be returned and nothing will be executed.
For each migration executed, the system will move the cursor to that point. So that, if any error happens during the migration execution (do or undo), the execution will be stopped and the error will be returned. All performed actions WILL NOT be rolled back.
type RunnerOption ¶
type RunnerOption func(*Runner)
type RunnerReporter ¶
type RunnerReporter interface { BeforeExecute(ctx context.Context, plan Plan) BeforeExecuteMigration(ctx context.Context, actionType ActionType, migration Migration) AfterExecuteMigration(ctx context.Context, actionType ActionType, migration Migration, err error) AfterExecute(ctx context.Context, plan Plan, stats *ExecutionStats, err error) }
type Source ¶
type Source interface { // ByID will return the Migration reference given the ID. // // If the migration id cannot be found, this function should return ErrMigrationNotFound. ByID(string) (Migration, error) // List will return the list of available migrations, sorted by ID (the older first, the newest last). // // If there is no migrations available, an ErrNoMigrationsAvailable should // be returned. List() ([]Migration, error) }
Source is responsible to list all migrations available to run.
Migrations can be stored into many medias, from Go source code files, plain SQL files, go:embed. So, this interface is responsible for abstracting how this system accepts any media to list the
type Target ¶
type Target interface { // Current returns the reference to the most recent migration applied to the system. // // If there is no migration run, the system will return an ErrNoCurrentMigration error. Current() (Migration, error) // Create ensures the creation of the list of migrations is done successfully. As an example, if this was an SQL // database implementation, this method would create the `_migrations` table. Create() error // Destroy removes the list of the applied migrations. As an example, if this was an SQL database implementation, // this would drop the `_migrations` table. Destroy() error // Done list all the migrations that were successfully applied. Done() ([]Migration, error) // Add adds a migration to the list of successful migrations. Add(Migration) error // Remove removes a migration from the list of successful migrations. Remove(Migration) error }
Target is responsible for managing the state of the migration system. This interface abstracts the operations needed to list what migrations were successfully executed, what is the current migration, etc.
type TargetLocker ¶ added in v0.2.0
type TargetLocker interface { // Lock will try locking the migration system in such way no other instance of the process can run the migrations. Lock() (Unlocker, error) }
TargetLocker abstracts the locking mechanism for specific target implementations.