Documentation
¶
Overview ¶
Package migrations versions the database schema.
Each Migration runs once per database, in ID order, and is recorded in the schema_migrations table. A database lock (PostgreSQL advisory lock, MySQL GET_LOCK) serializes runners, so API replicas, init containers and `gohead migrate` can start at the same time safely.
Adding a migration: append to All with the next ID. Write it so it also works on a fresh database where 0001_baseline already created the latest models (check with tx.Migrator().HasColumn/HasTable before changing), and give it a Down when it can be undone. Never edit a released migration.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var All = []Migration{ { ID: "0001_baseline", Description: "Tables of GoHead 0.5 (creates them, or brings databases from earlier versions up to date)", Up: baseline, }, { ID: "0002_revisions", Description: "Content revisions (history of every entry change)", Up: func(tx *gorm.DB) error { return tx.AutoMigrate(&revision0002{}) }, Down: func(tx *gorm.DB) error { return tx.Migrator().DropTable(&revision0002{}) }, }, { ID: "0003_i18n", Description: "Locales, localized attributes and translations", Up: i18nUp, Down: i18nDown, }, }
All lists the migrations in order.
var ErrPending = errors.New("database schema is not up to date")
ErrPending is returned by Check when migrations have not run.
Functions ¶
func Check ¶ added in v0.5.0
Check returns ErrPending (with the pending IDs) when migrations have not run, for processes that must not change the schema themselves.
func MigrateDatabase ¶
MigrateDatabase applies every pending migration. Kept for callers (and tests) that only need an up-to-date schema.
Types ¶
type Migration ¶ added in v0.5.0
type Migration struct {
ID string // sortable, e.g. "0002_item_locale"
Description string
Up func(tx *gorm.DB) error
Down func(tx *gorm.DB) error // nil: cannot be undone
}
Migration is one versioned schema change.