Documentation
¶
Index ¶
Constants ¶
const DefaultTrackingTable = "libschema.migration_status"
Variables ¶
var EverythingSynchronous = flag.Bool("migrate-all-synchronously", false, "Run async migrations synchronously")
TreateAsyncAsRequired command line flag causes asynchronous migrations to be treated like regular migrations from the point of view of --migrate-only, --no-migrate, and --exit-if-migrate-needed.
var ExitIfMigrateNeeded = flag.Bool("exit-if-migrate-needed", false, "Return error if migrations are not current")
ExitIfMigrateNeeded command line flag causes the program to exit instead of running required migrations. Asynchronous migrations do not count as required.
var MigrateDSN = flag.String("migrate-dsn", "", "Override *sql.DB")
MigrateDSN overrides the data source name for a single database. It must be used in conjunction with MigrateDatabase.
var MigrateDatabase = flag.String("migrate-database", "", "Migrate only the database named by NewDatabase")
MigrateDatabase command line flag specifies that only a specific database should be migrated. The name corresponds to the name provided with the schema.NewDatabase() call
var MigrateOnly = flag.Bool("migrate-only", false, "Call os.Exit() after completing migrations")
MigrateOnly command line flag causes the program to exit when migrations are complete. Asynchronous migrations may be skipped.
var NoMigrate = flag.Bool("no-migrate", false, "Skip all migrations (except async)")
NoMigrate command line flag skips all migrations
Functions ¶
Types ¶
type Database ¶
type Database struct {
Options Options
Context context.Context
// contains filtered or unexported fields
}
Database tracks all of the migrations for a specific database.
func (*Database) Migrations ¶
Migrations specifies the migrations needed for a library. By default, each migration is dependent upon the prior migration and they'll run in the order given. By default, all the migrations for a library will run in the order in which the library migrations are defined.
type Driver ¶
type Driver interface {
CreateSchemaTableIfNotExists(context.Context, MyLogger, *Database) error
LockMigrationsTable(context.Context, MyLogger, *Database) error
UnlockMigrationsTable(MyLogger) error
// DoOneMigration must update the both the migration status in
// the Database object and it must persist the migration status
// in the tracking table. It also does the migration.
DoOneMigration(context.Context, MyLogger, *Database, Migration) error
// IsMigrationSupported exists to guard against additional migration
// options and features. It should return nil except if there are new
// migration features added that haven't been included in all support
// libraries.
IsMigrationSupported(*Database, MyLogger, Migration) error
LoadStatus(context.Context, MyLogger, *Database) ([]MigrationName, error)
}
Driver interface is what's required to use libschema with a new database.
type Migration ¶
type Migration interface {
Base() *MigrationBase
Copy() Migration
}
MigrationBase is a workaround for lacking object inheritance.
type MigrationBase ¶
type MigrationBase struct {
Name MigrationName
// contains filtered or unexported fields
}
Migration defines a single database defintion update.
func (MigrationBase) Copy ¶
func (m MigrationBase) Copy() MigrationBase
func (*MigrationBase) HasSkipIf ¶
func (m *MigrationBase) HasSkipIf() bool
func (*MigrationBase) SetStatus ¶
func (m *MigrationBase) SetStatus(status MigrationStatus)
func (*MigrationBase) Status ¶
func (m *MigrationBase) Status() MigrationStatus
type MigrationName ¶
MigrationName holds both the name of the specific migration and the library to which it belongs.
func (MigrationName) String ¶
func (n MigrationName) String() string
type MigrationOption ¶
type MigrationOption func(Migration)
MigrationOption modifies a migration to set additional parameters
func After ¶
func After(lib, migration string) MigrationOption
After sets up a dependency between one migration and another. This can be across library boundaries. By default, migrations are dependent on the prior migration defined. After specifies that the current migration must run after the named migration.
func Asyncrhronous ¶
func Asyncrhronous() MigrationOption
Asyncrhronous marks a migration is okay to run asynchronously. If all of the remaining migrations can be asynchronous, then schema.Migrate() will return while the remaining migrations run.
func SkipIf ¶
func SkipIf(pred func() (bool, error)) MigrationOption
func SkipRemainingIf ¶
func SkipRemainingIf(pred func() (bool, error)) MigrationOption
type MigrationStatus ¶
type MigrationStatus struct {
Done bool
Error string // If an attempt was made but failed, this will be set
}
MigrationStatus tracks if a migration is complete or not.
type MyLogger ¶
type MyLogger interface {
Trace(msg string, fields ...map[string]interface{})
Debug(msg string, fields ...map[string]interface{})
Info(msg string, fields ...map[string]interface{})
Warn(msg string, fields ...map[string]interface{})
Error(msg string, fields ...map[string]interface{})
}
See https://github.com/logur/logur#readme This interface definition will not
type Options ¶
type Options struct {
// TrackingTable is the name of the table used to track which migrations
// have been applied
TrackingTable string
// SchemaOverride is used to override the default schema. This is most useful
// for testing schema migrations
SchemaOverride string
// These TxOptions will be used for all migration transactions.
MigrationTxOptions *sql.TxOptions
ErrorOnUnknownMigrations bool
// OnMigrationFailure is only called when there is a failure
// of a specific migration. OnMigrationsComplete will also
// be called.
OnMigrationFailure func(n MigrationName, err error)
// OnMigrationsStarted is only called if migrations are needed
OnMigrationsStarted func()
// OnMigrationsComplete called even if no migrations are needed. It
// will be called when async migrations finish even if they finish
// with an error.
OnMigrationsComplete func(error)
// DebugLogging turns on extra debug logging
DebugLogging bool
}
Options operate at the Database level but are specified at the Schema level at least initially.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema tracks all the migrations
func (*Schema) Migrate ¶
Migrate runs pending migrations that have been registered as long as the command line flags support doing migrations. We all remaining migrations are asynchronous then the remaining migrations will be run in the background.
A lock is held while migrations are in progress so that there is no chance of double migrations.
