Documentation
¶
Index ¶
- Constants
- Variables
- func IncrementingCounter(ins *FOFM) error
- func MigrationFileNameTime(name string) (timestamp time.Time, err error)
- func MigrationNameParts(name string) (timestamp time.Time, direction string, err error)
- func UnixTimestamps(ins *FOFM) error
- type FOFM
- func (m *FOFM) ClearStore() error
- func (m *FOFM) Down(name string) error
- func (m *FOFM) DownContext(ctx context.Context, name string) error
- func (m *FOFM) Latest() error
- func (m *FOFM) LatestContext(ctx context.Context) error
- func (m *FOFM) Pending() (MigrationStack, error)
- func (m *FOFM) Status() (MigrationSetStatus, error)
- func (m *FOFM) Up(name string) error
- func (m *FOFM) UpContext(ctx context.Context, name string) error
- type Locker
- type Migration
- type MigrationManager
- type MigrationSet
- type MigrationSetStatus
- type MigrationStack
- func (m *MigrationStack) Add(name, direction string, timestamp time.Time) error
- func (m MigrationStack) After(after *Migration) MigrationStack
- func (m MigrationStack) BeforeName(name string) MigrationStack
- func (m MigrationStack) Find(name string) *Migration
- func (m MigrationStack) First() *Migration
- func (m MigrationStack) From(from *Migration) MigrationStack
- func (m MigrationStack) Last() *Migration
- func (m MigrationStack) Names() []string
- func (m MigrationStack) Order()
- func (m MigrationStack) Reverse()
- type Migrations
- type NoResultsError
- type Numbering
- type Run
- type SQLite
- func (s *SQLite) ClearStore() error
- func (s *SQLite) Close() error
- func (s *SQLite) Connect() error
- func (s *SQLite) CreateStore() error
- func (s *SQLite) GetAllByName(name string) (MigrationSet, error)
- func (s *SQLite) LastRun() (*Migration, error)
- func (s *SQLite) LastRunByName(name string) (*Migration, error)
- func (s *SQLite) LastStatusRun(status string) (*Migration, error)
- func (s *SQLite) List() (MigrationSet, error)
- func (s *SQLite) Lock(ctx context.Context) (func() error, error)
- func (s *SQLite) Save(current Migration, err error) error
- func (s *SQLite) SetBusyTimeout(timeout time.Duration) error
- func (s *SQLite) SetLockTTL(ttl time.Duration)
- type Setting
- type Status
- type Store
Constants ¶
const ( STATUS_SUCCESS = "success" STATUS_FAILURE = "failure" )
const ( // DefaultLockTTL is how long a lock is held without being renewed before another // process may take it over DefaultLockTTL = 5 * time.Minute )
Variables ¶
var CounterNumbering = Numbering{ Next: func(taken []int64) int64 { var highest int64 for _, number := range taken { if number > highest { highest = number } } return highest + 1 }, Format: func(number int64) string { return fmt.Sprintf("%0*d", counterWidth, number) }, }
CounterNumbering numbers migrations 1, 2, 3, and so on, written with leading zeros so that they are five digits wide, like Migration_00001_up. The next number is one more than the highest one that exists. Past 99999 the number is simply wider
var DefaultSettings = []Setting{ UnixTimestamps, }
var UnixNumbering = Numbering{ Next: func(taken []int64) int64 { used := make(map[int64]bool, len(taken)) for _, number := range taken { used[number] = true } now := time.Now().Unix() for used[now] { now++ } return now }, Format: formatDecimal, }
UnixNumbering numbers migrations with the current unix time
Functions ¶
func IncrementingCounter ¶
IncrementingCounter names new migrations with a counter that is padded to five digits instead of a time
func MigrationFileNameTime ¶
func MigrationNameParts ¶
func UnixTimestamps ¶
UnixTimestamps is the default. New migrations are named with the current unix time
Types ¶
type FOFM ¶
type FOFM struct {
DB Store
Migration MigrationManager
UpMigrations MigrationStack
DownMigrations MigrationStack
// FS is where new migrations are written. Unless a Setting sets it, it is the directory
// that the migration manager is defined in
FS WriteFS
// PackageName is the package that new migrations are in. Unless a Setting sets it, it
// is read from the Go files that are beside the new migration
PackageName string
// Numbering is how new migrations are numbered. It is UnixTimestamps unless a Setting
// changes it
Numbering Numbering
// contains filtered or unexported fields
}
func New ¶
func New(db Store, migrationInstance MigrationManager, settings ...Setting) (*FOFM, error)
New will creae a new instance of FOFM. It will apply the DefaultSettings which can be overwritten by passing in settings.
The migration instance is a struct that has Migrations embedded in it and the migration methods on it. It can be a value or a pointer. Pass a pointer if the migration methods are defined with pointer receivers. It does not need any other methods, new migrations are written to the package that it is defined in.
func (*FOFM) ClearStore ¶
func (*FOFM) DownContext ¶
DownContext will run all migrations, in reverse order, up to and including the named one passed in. The name can be the full migration name (Migration_1_down), a partial name (Migration_1), or just the integer (1). Only migrations that are currently applied are run, so one that was never run, or has already been downed, is skipped. An error is returned if there is no down migration with that name. See LatestContext for how the context is used
func (*FOFM) LatestContext ¶
LatestContext will run every up migration that has not been run yet. Where it picks up from depends on the last successful run:
- nothing has been run: every up migration is run
- an up migration was the last: everything after it is run, so nothing happens if it was the latest migration
- a down migration was the last: it and everything after it is run again
A migration that failed is not a successful run, so it is attempted again. Pending returns the migrations that this will run.
The context is checked before each migration is started and it is passed to the migrations that accept one. If it is cancelled, the remaining migrations are not run and the context's error is returned. A migration that is already running is never interrupted by fofm, it is up to the migration to watch its context.
func (*FOFM) Pending ¶
func (m *FOFM) Pending() (MigrationStack, error)
Pending returns the up migrations that Latest would run right now, in the order that it would run them. Nothing is run
func (*FOFM) Status ¶
func (m *FOFM) Status() (MigrationSetStatus, error)
Status returns a list of all migrations and all of the times when they've been run since migrations can be run mulitple times
func (*FOFM) UpContext ¶
UpContext will run all migrations, in order, up to and inclduing the named one passed in. The name can be the full migration name (Migration_1_up), a partial name (Migration_1), or just the integer (1). Migrations that are already applied are skipped. An error is returned if there is no up migration with that name. See LatestContext for how the context is used
type Locker ¶
type Locker interface {
// Lock waits until it has the lock or the context is done. The returned function
// releases the lock. A lock that is never released, because its owner crashed, must
// eventually expire so that it does not block migrations forever
Lock(ctx context.Context) (unlock func() error, err error)
}
Locker is an optional interface for a Store. If the store has a lock, fofm holds it while it runs migrations so that only one copy of an app is migrating at a time. The store is still usable without one, but then it is up to you to make sure that two copies of an app do not migrate at the same time
type Migration ¶
type Migration struct {
ID int `json:"id"`
Name string `json:"name"`
Direction string `json:"direction"`
Status string `json:"status"`
Error string `json:"error"`
Timestamp time.Time `json:"timestamp"`
Created time.Time `json:"created"`
// contains filtered or unexported fields
}
func (Migration) BaseName ¶
BaseName is the migration's name without its direction, so Migration_1_up and Migration_1_down are both Migration_1. It is what ties an up to its down
func (*Migration) CreatedFromString ¶
func (Migration) MigrationTime ¶
MigrationTime is the time that is encoded in the migration's name. It is what migrations are ordered by. A migration that was read from the store has a Timestamp of when it was run, which is a different thing, so the name is preferred
func (*Migration) TimestampFromString ¶
type MigrationManager ¶
type MigrationManager interface {
// contains filtered or unexported methods
}
MigrationManager is a struct that has fofm.Migrations embedded in it. It is what New takes. Anything else, like a database connection that was passed in by mistake, is a compile error instead of a manager that quietly has no migrations.
type MigrationSet ¶
type MigrationSet []Migration
func (MigrationSet) ToRuns ¶
func (m MigrationSet) ToRuns() []Run
type MigrationSetStatus ¶
type MigrationSetStatus struct {
Migrations []Status `json:"migration_statuses"`
// contains filtered or unexported fields
}
type MigrationStack ¶
type MigrationStack []Migration
func (*MigrationStack) Add ¶
func (m *MigrationStack) Add(name, direction string, timestamp time.Time) error
func (MigrationStack) After ¶
func (m MigrationStack) After(after *Migration) MigrationStack
After returns every migration whose time is later than the one passed in. The time is read from the name of the migration that is passed in, because that is what the stack is ordered by. A nil migration returns the whole stack
func (MigrationStack) BeforeName ¶
func (m MigrationStack) BeforeName(name string) MigrationStack
BeforeName returns every migration from the start of the stack up to and including the named one. It returns an empty stack when there isn't a migration with that name
func (MigrationStack) Find ¶
func (m MigrationStack) Find(name string) *Migration
Find returns the migration that the name refers to or nil if there isn't one. The name can be the full migration name, like Migration_1_up, the name without a direction, like Migration_1, or just the integer, like 1. The integer is compared as a number, so 2 finds Migration_00002_up. Direction is decided by the stack, so Migration_1_down will never find anything in a stack of up migrations
func (MigrationStack) First ¶
func (m MigrationStack) First() *Migration
func (MigrationStack) From ¶
func (m MigrationStack) From(from *Migration) MigrationStack
From is like After, but it also includes the migration that is passed in
func (MigrationStack) Last ¶
func (m MigrationStack) Last() *Migration
func (MigrationStack) Names ¶
func (m MigrationStack) Names() []string
func (MigrationStack) Order ¶
func (m MigrationStack) Order()
func (MigrationStack) Reverse ¶
func (m MigrationStack) Reverse()
type Migrations ¶
type Migrations struct{}
Migrations is embedded in the struct that has the migrations on it. It is what marks the struct as a migration manager, so that New will only take a struct that was meant to be one. It has no fields and nothing to set up.
type MyMigrations struct {
fofm.Migrations
DB *sql.DB
}
func (m *MyMigrations) Migration_1_up() error { ... }
func (m *MyMigrations) Migration_1_down() error { ... }
An embedded struct has no way to see the struct that it is in, so it does not find the migrations. New does that by looking at the methods of the struct that it is given.
type NoResultsError ¶
type NoResultsError struct {
OriginalError error
// contains filtered or unexported fields
}
NoResultsError should be used in place of a store's no results error
func (NoResultsError) Error ¶
func (re NoResultsError) Error() string
type Numbering ¶
type Numbering struct {
// Next returns the number for the next migration. taken is the number of every
// migration that already exists, from lowest to highest. The result must not be one of
// them, because two migrations can not share a number
Next func(taken []int64) int64
// Format is how the number is written in the migration's name and in the name of its
// file. It has to be digits, since that is what a migration's name is read as. If it is
// not set, the number is written as it is
Format func(number int64) string
}
Numbering decides the number in the name of a new migration, like the 1658164360 in Migration_1658164360_up. Migrations run in the order of their numbers.
type SQLite ¶
type SQLite struct {
// contains filtered or unexported fields
}
func NewSQLiteWithTableName ¶
func (*SQLite) ClearStore ¶
func (*SQLite) CreateStore ¶
func (*SQLite) GetAllByName ¶
func (s *SQLite) GetAllByName(name string) (MigrationSet, error)
func (*SQLite) List ¶
func (s *SQLite) List() (MigrationSet, error)
func (*SQLite) Lock ¶
Lock takes the migration lock. Each call has its own owner, so it also keeps two callers in the same process apart. The lock is renewed in the background while it is held and a lock that has not been renewed for the TTL is taken over, so a process that crashes while holding it does not block the others forever
func (*SQLite) SetBusyTimeout ¶
SetBusyTimeout changes how long a query waits for another process that has the database file locked before it gives up. The default is five seconds. A context that ends while a query is waiting is not noticed until the wait is over, so this is also the longest that a cancelled Lock can take to return when another process is writing to the file
func (*SQLite) SetLockTTL ¶
SetLockTTL changes how long a lock can go without being renewed before another process takes it over. A lock that is held is renewed three times per TTL. Call this before the store is passed to New
type Setting ¶
func WithMigrationsDir ¶
WithMigrationsDir writes new migrations to a directory. This is not needed in most cases, because the directory is worked out from where the migration manager is defined. It is for when that can not be done: a manager that has no methods yet, or a program that was built with -trimpath. The directory has to be the package of the manager
func WithMigrationsFS ¶
func WithMigrationsFS(fsys WriteFS) Setting
WithMigrationsFS writes new migrations to any WriteFS, like one in memory. The files in it are read to find the package name and the numbers that are already used
func WithNumbering ¶
WithNumbering names new migrations with your own Numbering
func WithPackageName ¶
WithPackageName sets the package for new migrations. Without it, the package is read from the Go files that the migrations are written beside
type Store ¶
type Store interface {
// Connect will connect to the store
// it is automatcically called by FOFM
Connect() error
// Close will close any connections related to the store
Close() error
// CreateStore should do the work of creating
// the storage for the migrations. It
// should be a "create if doesnt exist" type
// operation
CreateStore() error
// ClearStore should reset the store to an empty state
ClearStore() error
// LastRun will return the last run migration
LastRun() (*Migration, error)
// LastStatusRun will get the last run with the
// provided status
LastStatusRun(status string) (*Migration, error)
// LastRunByName will get the last run with the
// provided name
LastRunByName(name string) (*Migration, error)
// GetAllByName will return all migrations with the
// provided name
GetAllByName(name string) (MigrationSet, error)
// List will return a list of all migrations that
// have been saved to the store
List() (MigrationSet, error)
// Save should insert a new record
Save(current Migration, err error) error
}