Documentation ¶
Index ¶
- Constants
- func BackendType(driverName string) string
- func RegisterBackend(key string, backend backends.Backend) error
- type Migration
- type MigrationLog
- type Migrator
- func (m *Migrator) AddMigration(name string, comment string, statement string, args ...interface{}) error
- func (m *Migrator) RepairHash(names ...string)
- func (m *Migrator) Run() ([]MigrationLog, error)
- func (m *Migrator) RunUnsafe() ([]MigrationLog, error)
- func (m *Migrator) UseBackend(key string) error
Constants ¶
const ( SUCCESS = iota PREVIOUS ERROR ERROR_HASH )
Variables ¶
This section is empty.
Functions ¶
func BackendType ¶
BackendType returns the backend key for a given database given a driverName.
Types ¶
type Migration ¶
type Migration struct { Name string Comment string Statement string // contains filtered or unexported fields }
Migration is a single schema change to apply to the database.
type MigrationLog ¶
A MigrationLog represents the results from a single migration.
type Migrator ¶
type Migrator struct { // The name of the database table to use for migration records. TableName string // contains filtered or unexported fields }
Migrator handles the process of migrating your database. Each instance of Migrator represents a single database that should have schema migrations applied to it.
func New ¶
New creates and returns a new Migrator instance. You typically should use one Migrator per database.
func (*Migrator) AddMigration ¶
func (m *Migrator) AddMigration(name string, comment string, statement string, args ...interface{}) error
The AddMigration method adds a new Migration to the list of migrations needed.
It is important to note that the name argument must be unique, and it is used to identify each migration. Changing the value of an existing migration name will likely cause errors. However, since a migration can be any SQL statement, you can use a new migration to change the name of an existing migration record in the DB. However, this is strongly discouraged, as it is easy to introduce an error state that will require manual edits to your migration table to fix.
An error is returned if a migration with the same name has already been added.
func (*Migrator) RepairHash ¶
RepairHash finds an existing migration by name and updates the hash in the DB. This is useful if you are using Run in safe mode, and there have been non-substantive changes to the Migration.Statement such as formatting or indenting changes.
The hash for each name supplied will be updated.
Hash repairs will be run just before the migrations and will not be applied if the migrations fail.
func (*Migrator) Run ¶
func (m *Migrator) Run() ([]MigrationLog, error)
Run executes the new migrations against the DB.
Run does a couple of things...
- Creates the migration table if it does not exist.
- Repairs any hashes that need to be updated.
- Executes each new migration in the order they were added.
- Adds each now migration record to the migration table.
- Returns a log of all migrations.
All the migrations are run as a single transaction. If a migration fails or an error is encountered, an error is returned and none of the migrations are applied. This ensures that if something goes wrong there is not an unknown state where some migrations are applied and some are not.
It is important to note that Run validates the integrity of past migrations. Once a migration has been run the hash is stored in the DB and the hash is checked against the migration hash each time it is run. This means that if changes are made to a migration statement after it has already been run, the two hashes will not match. In this case Run will return a hash mismatch error.
The reason the hash is checked on each subsequent run is simple. Adding "NOT NULL" to an already run "CREATE TABLE" migration will cause that brand-new development database you just created to have the right column definition. However, the production database will still have that column defined as "nullable" since the migration is not run again. This can cause the state of the development database and production database to slowly get out of sync.
If you have a style change like making all SQL keywords uppercase you can use RepairHash to rehash the migration and update the Hash in the database.
If you want to skip the hash validation you can use RunUnsafe instead.
func (*Migrator) RunUnsafe ¶
func (m *Migrator) RunUnsafe() ([]MigrationLog, error)
RunUnsafe executes the new migrations against the DB like Run, but in unsafe mode. Unsafe mode will not stop and return an error if an existing record hash does not match the hash of the migration.
The reason you may not want the hash checked on each subsequent run is simple. "alter table" and "ALTER TABLE" produce the same results, but have a different hash. To keep auto-formatters and linter changes from breaking old migrations RunUnsafe will ignore these and all other changes to the statement and args.
func (*Migrator) UseBackend ¶
UseBackend changes the default backend to a custom or built-in backend. The backend must be registered before it can be used. A backend can be registered once and used on multiple migrator instances.