gormigrate.v1: gopkg.in/gormigrate.v1 Index | Files

package gormigrate

import "gopkg.in/gormigrate.v1"

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.


package main

import (

    _ "github.com/jinzhu/gorm/dialects/sqlite"

type Person struct {
    Name string

type Pet struct {
    Name     string
    PersonID int

func main() {
    db, err := gorm.Open("sqlite3", "mydb.sqlite3")
    if err != nil {
    if err = db.DB().Ping(); err != nil {


    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)


Package Files

doc.go gormigrate.go


var (
    // DefaultOptions can be used if you don't want to think about options.
    DefaultOptions = &Options{
        TableName:                 "migrations",
        IDColumnName:              "id",
        IDColumnSize:              255,
        UseTransaction:            false,
        ValidateUnknownMigrations: false,

    // ErrRollbackImpossible is returned when trying to rollback a migration
    // that has no rollback function.
    ErrRollbackImpossible = errors.New("gormigrate: It's impossible to rollback this migration")

    // ErrNoMigrationDefined is returned when no migration is defined.
    ErrNoMigrationDefined = errors.New("gormigrate: No migration defined")

    // ErrMissingID is returned when the ID od migration is equal to ""
    ErrMissingID = errors.New("gormigrate: Missing ID in migration")

    // ErrNoRunMigration is returned when any run migration was found while
    // running RollbackLast
    ErrNoRunMigration = errors.New("gormigrate: Could not find last run migration")

    // ErrMigrationIDDoesNotExist is returned when migrating or rolling back to a migration ID that
    // does not exist in the list of migrations
    ErrMigrationIDDoesNotExist = errors.New("gormigrate: Tried to migrate to an ID that doesn't exist")

    // ErrUnknownPastMigration is returned if a migration exists in the DB that doesn't exist in the code
    ErrUnknownPastMigration = errors.New("gormigrate: Found migration in DB that does not exist in code")

type DuplicatedIDError Uses

type DuplicatedIDError struct {
    ID string

DuplicatedIDError is returned when more than one migration have the same ID

func (*DuplicatedIDError) Error Uses

func (e *DuplicatedIDError) Error() string

type Gormigrate Uses

type Gormigrate struct {
    // contains filtered or unexported fields

Gormigrate represents a collection of all migrations of a database schema.

func New Uses

func New(db *gorm.DB, options *Options, migrations []*Migration) *Gormigrate

New returns a new Gormigrate.

func (*Gormigrate) InitSchema Uses

func (g *Gormigrate) InitSchema(initSchema InitSchemaFunc)

InitSchema sets a function that is run if no migration is found. The idea is preventing to run all migrations when a new clean database is being migrating. In this function you should create all tables and foreign key necessary to your application.

func (*Gormigrate) Migrate Uses

func (g *Gormigrate) Migrate() error

Migrate executes all migrations that did not run yet.

func (*Gormigrate) MigrateTo Uses

func (g *Gormigrate) MigrateTo(migrationID string) error

MigrateTo executes all migrations that did not run yet up to the migration that matches `migrationID`.

func (*Gormigrate) RollbackLast Uses

func (g *Gormigrate) RollbackLast() error

RollbackLast undo the last migration

func (*Gormigrate) RollbackMigration Uses

func (g *Gormigrate) RollbackMigration(m *Migration) error

RollbackMigration undo a migration.

func (*Gormigrate) RollbackTo Uses

func (g *Gormigrate) RollbackTo(migrationID string) error

RollbackTo undoes migrations up to the given migration that matches the `migrationID`. Migration with the matching `migrationID` is not rolled back.

type InitSchemaFunc Uses

type InitSchemaFunc func(*gorm.DB) error

InitSchemaFunc is the func signature for initializing the schema.

type MigrateFunc Uses

type MigrateFunc func(*gorm.DB) error

MigrateFunc is the func signature for migrating.

type Migration Uses

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 Uses

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
    // IDColumnSize is the length of the migration id column
    IDColumnSize int
    // UseTransaction makes Gormigrate execute migrations inside a single transaction.
    // Keep in mind that not all databases support DDL commands inside transactions.
    UseTransaction bool
    // ValidateUnknownMigrations will cause migrate to fail if there's unknown migration
    // IDs in the database
    ValidateUnknownMigrations bool

Options define options for all migrations.

type ReservedIDError Uses

type ReservedIDError struct {
    ID string

ReservedIDError is returned when a migration is using a reserved ID

func (*ReservedIDError) Error Uses

func (e *ReservedIDError) Error() string

type RollbackFunc Uses

type RollbackFunc func(*gorm.DB) error

RollbackFunc is the func signature for rollbacking.

Package gormigrate imports 3 packages (graph) and is imported by 43 packages. Updated 2020-07-16. Refresh now. Tools for package owners.