Documentation ¶
Index ¶
- Constants
- Variables
- func NewMigrationID(str string) time.Time
- func Register(migration Migration)
- type ArgsRunner
- type BaseMigration
- type CodeSource
- type DefaultMigration
- type DefaultReporter
- func (reporter *DefaultReporter) AfterMigrate(migrations []*Summary, err error)
- func (reporter *DefaultReporter) AfterMigration(summary Summary, err error)
- func (reporter *DefaultReporter) AfterReset(rewindSummary []*Summary, migrateSummary []*Summary, err error)
- func (reporter *DefaultReporter) AfterRewind(migrations []*Summary, err error)
- func (reporter *DefaultReporter) BeforeMigrate(migrations []Migration)
- func (reporter *DefaultReporter) BeforeMigration(summary Summary, err error)
- func (reporter *DefaultReporter) BeforeReset()
- func (reporter *DefaultReporter) BeforeRewind(migrations []Migration)
- func (reporter *DefaultReporter) CommandNotFound(command string)
- func (reporter *DefaultReporter) Exit(code int)
- func (reporter *DefaultReporter) Failure(err error)
- func (reporter *DefaultReporter) ListExecuted(migrations []Migration, err error)
- func (reporter *DefaultReporter) ListPending(migrations []Migration, err error)
- func (reporter *DefaultReporter) MigrationSummary(migration *Summary, err error)
- func (reporter *DefaultReporter) MigrationsStarved(migrations []Migration)
- func (reporter *DefaultReporter) NoCommand()
- func (reporter *DefaultReporter) Usage()
- type Direction
- type DirectorySource
- type FileMigration
- func (m *FileMigration) Do(executionContext interface{}) error
- func (m *FileMigration) GetDescription() string
- func (m *FileMigration) GetID() time.Time
- func (m *FileMigration) GetManager() Manager
- func (m *FileMigration) SetManager(manager Manager) Migration
- func (m *FileMigration) Undo(executionContext interface{}) error
- type Handler
- type Manager
- type ManagerDefault
- func (manager *ManagerDefault) Do(reporter Reporter, executionContext interface{}) (*Summary, error)
- func (manager *ManagerDefault) Migrate(reporter Reporter, executionContext interface{}) ([]*Summary, error)
- func (manager *ManagerDefault) MigrationsExecuted() ([]Migration, error)
- func (manager *ManagerDefault) MigrationsPending() ([]Migration, error)
- func (manager *ManagerDefault) Reset(reporter Reporter, executionContext interface{}) ([]*Summary, []*Summary, error)
- func (manager *ManagerDefault) Rewind(reporter Reporter, executionContext interface{}) ([]*Summary, error)
- func (manager *ManagerDefault) Source() Source
- func (manager *ManagerDefault) Target() Target
- func (manager *ManagerDefault) Undo(reporter Reporter, executionContext interface{}) (*Summary, error)
- type Migration
- type MongoDBTarget
- func (t *MongoDBTarget) AddMigration(summary *Summary) error
- func (t *MongoDBTarget) Database() *mgo.Database
- func (t *MongoDBTarget) MigrationsExecuted() ([]time.Time, error)
- func (t *MongoDBTarget) RemoveMigration(summary *Summary) error
- func (t *MongoDBTarget) SetCollectionName(collection string) *MongoDBTarget
- func (t *MongoDBTarget) Version() (migrationID time.Time, err error)
- type MySQLTarget
- type PostgreSQLTarget
- type Reporter
- type Runner
- type Source
- type Summary
- type Target
Constants ¶
const DefaultMigrationTable = "_migrations"
DefaultMigrationTable is the default name of the migrations table.
Variables ¶
var CodeMigrationDateFormat = "20060102150405"
CodeMigrationDateFormat is the format understood by the CodeMigration
var ErrMigrationPanicked = errors.New("migration panicked")
ErrMigrationPanicked is the error returned when a migrations panics.
var ErrMigrationStarved = errors.New("migration starvation")
ErrMigrationStarved is when late migrations are detected.
var NoVersion = time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
NoVersion represents a zero version
Functions ¶
func NewMigrationID ¶
NewMigrationID creates a new ID from a string.
Types ¶
type ArgsRunner ¶
type ArgsRunner struct {
// contains filtered or unexported fields
}
ArgsRunner is the Runner that will provide the default implementation of Runner that captures params from the arguments.
func (*ArgsRunner) Run ¶
func (runner *ArgsRunner) Run(executionContext interface{})
Run performs the actions based on the arguments captured.
type BaseMigration ¶
type BaseMigration struct {
// contains filtered or unexported fields
}
BaseMigration is the default structure that all base migrations returns.
func (*BaseMigration) GetDescription ¶
func (m *BaseMigration) GetDescription() string
GetDescription returns the ID of the migration.
func (*BaseMigration) GetID ¶
func (m *BaseMigration) GetID() time.Time
GetID returns the ID of the migration.
type CodeSource ¶
type CodeSource struct {
// contains filtered or unexported fields
}
CodeSource is migration.Source implementation. It provides the development of migrations using Golang code.
TODO add examples
func DefaultCodeSource ¶
func DefaultCodeSource() *CodeSource
DefaultCodeSource implements a singleton of the default CodeSource. It enable the developer to use the migration.Register without needing to deal with the migration.NewCodeSource.
On normal situations only one CodeSource is enough through the whole system. And many sources are needed, the developer can use multiple instances of the migration.CodeSource and use the migration.CodeSource.Register, instead of using the default implementation migration.Register.
func NewCodeSource ¶
func NewCodeSource() *CodeSource
NewCodeSource returns a new instance of a migration.CodeSource.
func (*CodeSource) List ¶
func (s *CodeSource) List() ([]Migration, error)
List implements the migration.Source.List by listing all the registered migrations of this instance.
func (*CodeSource) Register ¶
func (s *CodeSource) Register(migration Migration)
Register registers the migration for further use.
type DefaultMigration ¶
type DefaultMigration struct { BaseMigration // contains filtered or unexported fields }
DefaultMigration is the default implementation of the migration.Migration.
It is designed to provide a coded implementaiton of a migration. It receives an up and down anonymous methods to be ran while executing the migration.
This implementation is used by the migration.CodeSource implemenation of the migration.Source.
func NewCodeMigration ¶
func NewCodeMigration(handlers ...Handler) *DefaultMigration
NewCodeMigration uses the regex to extract data NewMigration.
It uses the `NewCodeMigrationCustom`.
func NewCodeMigrationCustom ¶ added in v1.2.2
func NewCodeMigrationCustom(skip int, handlers ...Handler) *DefaultMigration
NewCodeMigrationCustom uses the regex to extract data NewMigration.
If a handler is provided it will assigned to the Up method. If a second is provided, it will be assigned to the Down method.
func NewMigration ¶
func NewMigration(id time.Time, description string, handlers ...Handler) *DefaultMigration
NewMigration returns a new instance of migration.Migration with all the required properties initialized.
If a handler is provided it will assigned to the Up method. If a second is provided, it will be assigned to the Down method.
func (*DefaultMigration) Do ¶
func (m *DefaultMigration) Do(executionContext interface{}) error
Do calls the up action of the migration.
func (*DefaultMigration) GetManager ¶
func (m *DefaultMigration) GetManager() Manager
GetManager returns the reference of the manager that is executing the migration.
func (*DefaultMigration) SetManager ¶
func (m *DefaultMigration) SetManager(manager Manager) Migration
SetManager set the reference of the manager that is executing the migration.
It returns itself for sugar syntax.
func (*DefaultMigration) Undo ¶
func (m *DefaultMigration) Undo(executionContext interface{}) error
Undo calls the down action of the migration.
type DefaultReporter ¶
type DefaultReporter struct {
// contains filtered or unexported fields
}
DefaultReporter is the default implementation of a Reporter.
func NewDefaultReporter ¶
func NewDefaultReporter() *DefaultReporter
NewDefaultReporter returns an instance of a DefaultReporter.
func NewDefaultReporterWithParams ¶
func NewDefaultReporterWithParams(w io.Writer, exitFnc func(code int)) *DefaultReporter
NewDefaultReporterWithParams initializes an instance of a DefaultReporter with its params.
func (*DefaultReporter) AfterMigrate ¶
func (reporter *DefaultReporter) AfterMigrate(migrations []*Summary, err error)
AfterMigrate is called right after the process of migration is completed.
func (*DefaultReporter) AfterMigration ¶
func (reporter *DefaultReporter) AfterMigration(summary Summary, err error)
AfterMigration is called by the Manager right after a migrations is ran.
func (*DefaultReporter) AfterReset ¶
func (reporter *DefaultReporter) AfterReset(rewindSummary []*Summary, migrateSummary []*Summary, err error)
AfterReset is called right before the process of reseting is completed.
func (*DefaultReporter) AfterRewind ¶
func (reporter *DefaultReporter) AfterRewind(migrations []*Summary, err error)
AfterRewind is called right after the process of rewinding is completed.
func (*DefaultReporter) BeforeMigrate ¶
func (reporter *DefaultReporter) BeforeMigrate(migrations []Migration)
BeforeMigrate is called right before the process of migration is triggered.
func (*DefaultReporter) BeforeMigration ¶
func (reporter *DefaultReporter) BeforeMigration(summary Summary, err error)
BeforeMigration is called by the Manager right before a migration is ran.
func (*DefaultReporter) BeforeReset ¶
func (reporter *DefaultReporter) BeforeReset()
BeforeReset is called right before the process of reseting is triggered.
func (*DefaultReporter) BeforeRewind ¶
func (reporter *DefaultReporter) BeforeRewind(migrations []Migration)
BeforeRewind is called right before the process of rewinding is triggered.
func (*DefaultReporter) CommandNotFound ¶
func (reporter *DefaultReporter) CommandNotFound(command string)
CommandNotFound reports the command executed by the migration tool was not found.
func (*DefaultReporter) Exit ¶
func (reporter *DefaultReporter) Exit(code int)
Exit calls the exit function of the reporter.
func (*DefaultReporter) Failure ¶
func (reporter *DefaultReporter) Failure(err error)
Failure reports a failure and writes it down.
func (*DefaultReporter) ListExecuted ¶
func (reporter *DefaultReporter) ListExecuted(migrations []Migration, err error)
ListExecuted reports the list of the migrations that were executed.
func (*DefaultReporter) ListPending ¶
func (reporter *DefaultReporter) ListPending(migrations []Migration, err error)
ListPending reports the list of the migrations peding to be executed.
func (*DefaultReporter) MigrationSummary ¶
func (reporter *DefaultReporter) MigrationSummary(migration *Summary, err error)
MigrationSummary prints the summary of the migration.
func (*DefaultReporter) MigrationsStarved ¶
func (reporter *DefaultReporter) MigrationsStarved(migrations []Migration)
MigrationsStarved is called whenever the manager detects a migration will never be executed.
func (*DefaultReporter) NoCommand ¶
func (reporter *DefaultReporter) NoCommand()
NoCommand gets called when no command is provided to the migration tool.
func (*DefaultReporter) Usage ¶
func (reporter *DefaultReporter) Usage()
Usage prints the usage of the migration command.
type Direction ¶
type Direction uint
Direction is the enum that represents the direction of the migration.
Its possible values are `MigrationDirectionDo` and `MigrationDirectionUndo`.
type DirectorySource ¶
type DirectorySource struct { // Directory represents the path that the migrations file will be searched // for. Directory string // Extension represents the file extension of the files. Extension string }
DirectorySource is migration.Source implementation. It provides the development of migrations using SQL files inside of a directory.
The file pattern used to match the migrations is: <DATE>_<DESCRIPTION>.(up|down).(extension)
DATE: Must be in the format YYYYMMDDHHNNSS. For example, 20171005110647 for Oct 05, 2017 11:06:47) DESCRIPTION: Any text but with no dots.
Valid naming examples:
20171025191747_Creates_user_table.down.sql : Drop the user table 20171025191747_Creates_user_table.up.sql : Create user table 20171025191747_Creates customers table.down.sql : Drop the customer table 20171025191747_Creates customers table.up.sql : Create customer table 20171025213303_Drops_the_token_column.up.sql : Drops a column, so is irreversible (there is no .down.sql)
Invalid naming examples:
20171525191747_Creates_user_table.down.sql : Invalid date (month 15?) 20171025191747_Creates_user_table.NNN.sql : Invalid sufix (should be up or down)
func (*DirectorySource) List ¶
func (s *DirectorySource) List() ([]Migration, error)
List implements the migration.Source.List by listing all the files inside the migration.DirectorySource.Directory with the naming convention using the migration.DirectorySource.Extension.
type FileMigration ¶
type FileMigration struct {
// contains filtered or unexported fields
}
FileMigration is the implementation of the migration.Migration that runs SQL files.
It is designed to provide a coded implementaiton of a migration. It receives an up and down anonymous methods to be ran while executing the migration.
It is used by the migration.CodeSource implemenation of the migration.Source.
func (*FileMigration) Do ¶
func (m *FileMigration) Do(executionContext interface{}) error
Do implements the migration.Migration.Up by running all SQLs inside of the [migration.FileMigration.baseFile].up.sql file.
If the file does not exists, it returns an error.
func (*FileMigration) GetDescription ¶
func (m *FileMigration) GetDescription() string
GetDescription implements the migration.Migration.GetDescription by returning the id of this migration.
func (*FileMigration) GetID ¶
func (m *FileMigration) GetID() time.Time
GetID implements the migration.Migration.GetID by returning the id of this migration.
func (*FileMigration) GetManager ¶
func (m *FileMigration) GetManager() Manager
GetManager implements the migration.Migration.GetManager by returning the manager responsible for the execution.
func (*FileMigration) SetManager ¶
func (m *FileMigration) SetManager(manager Manager) Migration
SetManager implements the migration.Migration.SetManager by setting the manager of this instance.
It returns the itself for sugar syntax purposes.
func (*FileMigration) Undo ¶
func (m *FileMigration) Undo(executionContext interface{}) error
Undo implements the migration.Migration.Down by running all SQLs inside of the [migration.FileMigration.baseFile].down.sql file.
If the file does not exists, it returns an error.
type Handler ¶
type Handler func(executionContext interface{}) error
Handler is the signature of the up and down methods that a migration will receive.
type Manager ¶
type Manager interface { Source() Source Target() Target MigrationsExecuted() ([]Migration, error) MigrationsPending() ([]Migration, error) Migrate(listener Reporter, executionContext interface{}) ([]*Summary, error) Rewind(listener Reporter, executionContext interface{}) ([]*Summary, error) Reset(listener Reporter, executionContext interface{}) ([]*Summary, []*Summary, error) Do(listener Reporter, executionContext interface{}) (*Summary, error) Undo(listener Reporter, executionContext interface{}) (*Summary, error) }
Manager is an interface that describe the common behavior of a migration manager.
Any manager is divider in two parts: the `Source` and `Target`. In short, the `Source` is the origin of the migrations (eg. SQL files or Go scripts). A `Target` is database technology that the migrations will be action on.
Integrating the `Source` and the `Target`, the `Manager` is responsible for running migrations with its methods `Migrate`, `Rewind`, `Reset`, `Up` and `Down`.
func NewDefaultManager ¶
NewDefaultManager creates and returns a migration.Manager implementation (`migration.ManagerDefault`) based on a target and source.
type ManagerDefault ¶
type ManagerDefault struct {
// contains filtered or unexported fields
}
ManagerDefault is a default implementation of a Manager. It provides, via migration.NewManager, a way to define what is the source and target of a manager.
func (*ManagerDefault) Do ¶
func (manager *ManagerDefault) Do(reporter Reporter, executionContext interface{}) (*Summary, error)
Do takes a step up on the migrations, bringing the database one step closer to the latest migration.
Before the execution of the migrations, it calls the reporter.Before method. After the migration is executed, if it returns no error, it calls the reporter.After method.
func (*ManagerDefault) Migrate ¶
func (manager *ManagerDefault) Migrate(reporter Reporter, executionContext interface{}) ([]*Summary, error)
Migrate brings the database to the latest migration.
func (*ManagerDefault) MigrationsExecuted ¶
func (manager *ManagerDefault) MigrationsExecuted() ([]Migration, error)
MigrationsExecuted returns a list of migrations that were executed. It uses the migration.Manager.MigrationsAfter passing on the current version from migration.Manager.Target.Version.
func (*ManagerDefault) MigrationsPending ¶
func (manager *ManagerDefault) MigrationsPending() ([]Migration, error)
MigrationsPending returns a list of migrations that were not executed yet. It uses the migration.Manager.MigrationsBefore passing on the current version from migration.Manager.Target.Version.
func (*ManagerDefault) Reset ¶
func (manager *ManagerDefault) Reset(reporter Reporter, executionContext interface{}) ([]*Summary, []*Summary, error)
Reset rewind all the migrations, then migrates to the latest.
func (*ManagerDefault) Rewind ¶
func (manager *ManagerDefault) Rewind(reporter Reporter, executionContext interface{}) ([]*Summary, error)
Rewind removes all migrations previously migrated.
It lists all the executed migrations and executes their migration.Migrate.Down in a inverted order, virtually bringing the database to its original form.
func (*ManagerDefault) Source ¶
func (manager *ManagerDefault) Source() Source
Source returns the migration source used for this manager.
func (*ManagerDefault) Target ¶
func (manager *ManagerDefault) Target() Target
Target returns the migration target used for this manager.
func (*ManagerDefault) Undo ¶
func (manager *ManagerDefault) Undo(reporter Reporter, executionContext interface{}) (*Summary, error)
Undo takes a step up on the migrations, bringing the database one step closer to the latest migration.
Before the execution of the migrations, it calls the reporter.Before method. After the migration is executed, if it returns no error, it calls the reporter.After method.
type Migration ¶
type Migration interface { GetID() time.Time GetDescription() string Do(executionContext interface{}) error Undo(executionContext interface{}) error GetManager() Manager SetManager(manager Manager) Migration }
Migration is the interface that describes the common behavior that a migration should have to be manageable by the migration.Manager.
type MongoDBTarget ¶
type MongoDBTarget struct {
// contains filtered or unexported fields
}
MongoDBTarget implements the migration.Target of the MongoDB.
In order to get access to the MongoDB, migration.MongoDBTarget uses the MGo library (http://github.com/globalsign/mgo).
func NewMongoDB ¶
func NewMongoDB(db *mgo.Database) *MongoDBTarget
NewMongoDB returns a new instance of the migration.MongoDBTarget
func (*MongoDBTarget) AddMigration ¶
func (t *MongoDBTarget) AddMigration(summary *Summary) error
SetVersion implements the migration.Target.SetVersion by storing the passed version on the database.
It returns eny error returned by the MGo.
func (*MongoDBTarget) Database ¶ added in v1.2.0
func (t *MongoDBTarget) Database() *mgo.Database
Database returns the `*mgo.Database` reference of this target.
func (*MongoDBTarget) MigrationsExecuted ¶
func (t *MongoDBTarget) MigrationsExecuted() ([]time.Time, error)
func (*MongoDBTarget) RemoveMigration ¶
func (t *MongoDBTarget) RemoveMigration(summary *Summary) error
RemoveMigration find and removes a migrations from the collection.
func (*MongoDBTarget) SetCollectionName ¶
func (t *MongoDBTarget) SetCollectionName(collection string) *MongoDBTarget
SetCollectionName sets the name of the collection used to store the current version of the database.
func (*MongoDBTarget) Version ¶
func (t *MongoDBTarget) Version() (migrationID time.Time, err error)
Version implements the migration.Target.Version by fetching the current version of the database from the collection defined by migration.MongoDBTarget.SetCollectionName.
It returns the current version of the database.
Any error returned by the MGo, will be passed up to the caller.
type MySQLTarget ¶
type MySQLTarget struct {
// contains filtered or unexported fields
}
MySQLTarget implements the migration.Target of the SQL based databases, using the Golang SQL package.
func NewMySQL ¶
func NewMySQL(conn *sql.DB) *MySQLTarget
NewMySQL returns a new instance of the migration.MySQLTarget
func (*MySQLTarget) Connection ¶
func (target *MySQLTarget) Connection() *sql.DB
Connection returns the mgo.Session reference of this target.
func (*MySQLTarget) SetTableName ¶
func (target *MySQLTarget) SetTableName(collection string) *MySQLTarget
SetTableName sets the name of the table used to store the current migrations version that were executed.
func (*MySQLTarget) SetVersion ¶
func (target *MySQLTarget) SetVersion(id time.Time) error
SetVersion implements the migration.Target.SetVersion by storing the passed version on the database.
It returns any error returned from the database driver.
func (*MySQLTarget) Version ¶
func (target *MySQLTarget) Version() (time.Time, error)
Version implements the migration.Target.Version by fetching the current version of the database from the table defined by migration.MongoDBTarget.SetCollectionName.
It returns the current version of the database.
Any error returned by the driver, will be passed to the caller.
type PostgreSQLTarget ¶ added in v1.3.0
type PostgreSQLTarget struct {
// contains filtered or unexported fields
}
func NewPostgreSQLTarget ¶ added in v1.3.0
func NewPostgreSQLTarget(db *sql.DB) *PostgreSQLTarget
func (*PostgreSQLTarget) AddMigration ¶ added in v1.3.0
func (target *PostgreSQLTarget) AddMigration(summary *Summary) error
func (*PostgreSQLTarget) MigrationsExecuted ¶ added in v1.3.0
func (target *PostgreSQLTarget) MigrationsExecuted() ([]time.Time, error)
func (*PostgreSQLTarget) RemoveMigration ¶ added in v1.3.0
func (target *PostgreSQLTarget) RemoveMigration(summary *Summary) error
type Reporter ¶
type Reporter interface { BeforeMigration(migration Summary, err error) MigrationSummary(migration *Summary, err error) AfterMigration(migration Summary, err error) BeforeMigrate(migrations []Migration) AfterMigrate(migrations []*Summary, err error) BeforeRewind(migrations []Migration) AfterRewind(migrations []*Summary, err error) BeforeReset() AfterReset(rewindSummary []*Summary, migrateSummary []*Summary, err error) ListPending(migrations []Migration, err error) ListExecuted(migrations []Migration, err error) Failure(err error) Exit(code int) MigrationsStarved(migrations []Migration) Usage() CommandNotFound(command string) NoCommand() }
Reporter is a helper method used, mainly, for logging on the `Manager` methods. Before executing any migration (in any direction) the `Manager` calls the listener `Before`. Afterward, if it does not return any error, the listener's `After` is called.
type Runner ¶
type Runner interface {
Run(executionContext interface{})
}
Runner defines the basic contract for a something that might run a migration.
func NewArgsRunner ¶
NewArgsRunner returns a new instance of a ArgsRunner.
type Source ¶
type Source interface { // List lists all migrations available for this migrations.Source. List() ([]Migration, error) }
Source describes the common interface for source of migrations.
Different technologies support various ways of interaction with them. For SQL based databases, SQL files may solve the problem. However, when dealing with NoSQL databases, as MongoDB, it will not work.
So, migration.Source is a interface let the developer "store" migrations in many ways.
type Summary ¶
type Summary struct { Migration Migration // contains filtered or unexported fields }
Summary is the record that keeps all the data collected while the migration was ran.
func NewSummary ¶
NewSummary creates a new summary based on a migration instance.
type Target ¶
type Target interface { // Version returns the current version of the database. Version() (time.Time, error) // AddMigration persists the version on the database (or any other mean // necessary). AddMigration(summary *Summary) error // RemoveMigration removes a migration record from the database. RemoveMigration(summary *Summary) error // MigrationsExecuted returns all the migrations executed. MigrationsExecuted() ([]time.Time, error) }
Target describes the common interface for target of migrations. Each target is a implementation of a specific database (or anything versionable).