Documentation ¶
Overview ¶
Package gormigrate is a migration helper for Gorm (http://jinzhu.me/gorm/). Gorm already have useful migrate functions (http://jinzhu.me/gorm/database.html#migration), just misses proper schema versioning and rollback cababilities.
Example:
package main import ( "log" "github.com/go-gormigrate/gormigrate" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" ) type Person struct { gorm.Model Name string } type Pet struct { gorm.Model Name string PersonID int } func main() { db, err := gorm.Open("sqlite3", "mydb.sqlite3") if err != nil { log.Fatal(err) } if err = db.DB().Ping(); err != nil { log.Fatal(err) } db.LogMode(true) m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{ { ID: "201608301400", Migrate: func(tx *gorm.DB) error { return tx.AutoMigrate(&Person{}).Error }, Rollback: func(tx *gorm.DB) error { return tx.DropTable("people").Error }, }, { ID: "201608301430", Migrate: func(tx *gorm.DB) error { return tx.AutoMigrate(&Pet{}).Error }, Rollback: func(tx *gorm.DB) error { return tx.DropTable("pets").Error }, }, }) err = m.Migrate() if err == nil { log.Printf("Migration did run successfully") } else { log.Printf("Could not migrate: %v", err) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultOptions can be used if you don't want to think about options. DefaultOptions = &Options{ TableName: "migrations", IDColumnName: "id", UseTransaction: false, } // ErrRollbackImpossible is returned when trying to rollback a migration // that has no rollback function. ErrRollbackImpossible = errors.New("It's impossible to rollback this migration") // ErrNoMigrationDefined is returned when no migration is defined. ErrNoMigrationDefined = errors.New("No migration defined") )
Functions ¶
This section is empty.
Types ¶
type Gormigrate ¶
type Gormigrate struct {
// contains filtered or unexported fields
}
Gormigrate represents a collection of all migrations of a database schema.
func New ¶
func New(db *gorm.DB, options *Options, migrations []*Migration) *Gormigrate
New returns a new Gormigrate.
func (*Gormigrate) Migrate ¶
func (g *Gormigrate) Migrate() error
Migrate executes all migrations that did not run yet.
func (*Gormigrate) RollbackLast ¶
func (g *Gormigrate) RollbackLast() error
RollbackLast undo the last migration
func (*Gormigrate) RollbackMigration ¶
func (g *Gormigrate) RollbackMigration(m *Migration) error
RollbackMigration undo a migration.
type MigrateFunc ¶
MigrateFunc is the func signature for migrating.
type Migration ¶
type Migration struct { // ID is the migration identifier. Usually a timestamp like "201601021504". ID string // Migrate is a function that will br executed while running this migration. Migrate MigrateFunc // Rollback will be executed on rollback. Can be nil. Rollback RollbackFunc }
Migration represents a database migration (a modification to be made on the database).
type Options ¶
type Options struct { // TableName is the migration table. TableName string // IDColumnName is the name of column where the migration id will be stored. IDColumnName string // UseTransaction makes Gormigrate execute migrations inside a single transaction. // Keep in mind that not all databases support DDL commands inside transactions. UseTransaction bool }
Options define options for all migrations.
type RollbackFunc ¶
RollbackFunc is the func signature for rollbacking.