Documentation
¶
Index ¶
- Variables
- func NewQueryError(err error, query string) error
- func WrapMigration(err error, migration Migration) *migrationError
- func WrapMigrationID(err error, migrationID string) *migrationIDError
- type Action
- type ActionPLanner
- type ActionType
- type AfterExecuteInfo
- type AfterExecuteMigrationInfo
- 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 BeforeExecuteInfo
- type BeforeExecuteMigrationInfo
- type ExecuteRequest
- type ExecutionResponse
- type MigrateOption
- type Migration
- type MigrationError
- type MigrationIDError
- type MigrationsError
- type Plan
- type Planner
- type ProgressReporter
- type QueryError
- type Repository
- type Runner
- type RunnerOption
- type RunnerReporter
- type Source
- type Target
- type Unlocker
Constants ¶
This section is empty.
Variables ¶
var ( 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") ErrDirtyMigration = errors.New("migration was started but not completed and now it is in a dirty state") )
var (
// DefaultMigrationIDFormat is the default format for the migrations ID.
DefaultMigrationIDFormat = "20060102150405"
)
var ErrMigrationAlreadyExists = errors.New("migration already exists")
Functions ¶
func NewQueryError ¶
func WrapMigration ¶
WrapMigration creates a `MigrationError` based on an existing error.
func WrapMigrationID ¶
WrapMigrationID creates a `MigrationCodeError` based on an existing error.
Types ¶
type Action ¶
type Action struct { Action ActionType Migration Migration }
type ActionPLanner ¶
func StepPlanner ¶
func StepPlanner(step int) ActionPLanner
StepPlanner build an ActionPlanner that will step the migrations by the given number. If the given number is positive, then the step will be forward, otherwise, it will be backward.
type ActionType ¶
type ActionType string
const ( ActionTypeDo ActionType = "do" ActionTypeUndo ActionType = "undo" )
type AfterExecuteInfo ¶
type AfterExecuteInfo struct { Plan Plan Stats *ExecutionResponse Err error }
type AfterExecuteMigrationInfo ¶
type AfterExecuteMigrationInfo struct { ActionType ActionType Migration Migration Err error }
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 BeforeExecuteInfo ¶
type BeforeExecuteInfo struct {
Plan Plan
}
type BeforeExecuteMigrationInfo ¶
type BeforeExecuteMigrationInfo struct { ActionType ActionType Migration Migration }
type ExecuteRequest ¶
type ExecuteRequest struct {
Plan Plan
}
type ExecutionResponse ¶
func Migrate ¶
func Migrate(ctx context.Context, source Source, target Target, opts ...MigrateOption) (ExecutionResponse, error)
type MigrateOption ¶
type MigrateOption func(*migrateOptions)
func WithPlanner ¶
func WithPlanner(planner ActionPLanner) MigrateOption
WithPlanner sets the planner to be used by the migration process. Default planner is MigratePlanner.
func WithRunner ¶
func WithRunner(runner *Runner) MigrateOption
WithRunner sets the reporter to be used by the migration process.
func WithRunnerOptions ¶
func WithRunnerOptions(opts ...RunnerOption) MigrateOption
WithRunnerOptions sets the options to be used by the runner created. If a `WithRunner` is used, this is ignored.
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.
type MigrationIDError ¶
type MigrationIDError interface {
MigrationID() string
}
MigrationIDError wraps an error with a migration ID.
type MigrationsError ¶
type MigrationsError interface {
Migrations() []Migration
}
MigrationsError wraps an error with a list of migrations.
func WrapMigrations ¶
func WrapMigrations(err error, migrations ...Migration) MigrationsError
type Planner ¶
func MigratePlanner ¶
MigratePlanner is an ActionPlanner that returns a Planner that plans actions to take the current version of the database to the latest.
func ResetPlanner ¶
ResetPlanner is an ActionPlanner that returns a Planner that plans actions to rewind all migrations and then migrate again to the latest version.
func RewindPlanner ¶
RewindPlanner is a planner that will undo all migrations starting from the current revision.
func UndoPlanner ¶
type ProgressReporter ¶
type QueryError ¶
type QueryError interface {
Query() string
}
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository stores the migrations in memory and provide methods for acessing them.
func (*Repository) Add ¶
func (r *Repository) Add(migration Migration) error
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, req *ExecuteRequest) (ExecutionResponse, error)
Execute performs a plan, running all actions migration by migration.
Before running, Execute will check for Undo actions that cannot be performed 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(*runnerOptions)
func WithReporter ¶
func WithReporter(reporter RunnerReporter) RunnerOption
type RunnerReporter ¶
type RunnerReporter interface { BeforeExecute(ctx context.Context, info *BeforeExecuteInfo) BeforeExecuteMigration(ctx context.Context, info *BeforeExecuteMigrationInfo) AfterExecuteMigration(ctx context.Context, info *AfterExecuteMigrationInfo) AfterExecute(ctx context.Context, info *AfterExecuteInfo) }
type Source ¶
type Source interface { // Add will add a Migration to the source list. // // If the migration id cannot be found, this function should return ErrMigrationAlreadyExists. Add(ctx context.Context, migration Migration) error // Load 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. Load(ctx context.Context) (Repository, 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
func NewMemorySource ¶ added in v2.1.0
func NewMemorySource() Source
NewMemorySource creates a source in which the migrations are stored only in memory. This is useful for `github.com/jamillosantos/migrations/v2/fnc` migrations.
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(ctx context.Context) (string, 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(ctx context.Context) 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(ctx context.Context) error // Done list all the migrations that were successfully applied. Done(ctx context.Context) ([]string, error) // Add adds a migration to the list of successful migrations. Add(ctx context.Context, id string) error // Remove removes a migration from the list of successful migrations. Remove(ctx context.Context, id string) error // FinishMigration will mark the migration as finished. This is only used when the migration is being added. FinishMigration(ctx context.Context, id string) error // StartMigration will mark the migration as dirty. This is only used when the migration is being removed. StartMigration(ctx context.Context, id string) error // Lock will try locking the migration system in such way no other instance of the process can run the migrations. Lock(ctx context.Context) (Unlocker, 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.