Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

fclairamb/gorm-migrate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build codecov Go Report Card Go version Go.Dev reference MIT license

Gorm database migration

Simple library to take advantage of the gorm's migration API.

Choices

  • It only applies migrations. It's up to you to chose when to apply each operation.
  • Any failure cancels every single change (including the one to the migrations listing table)
  • There are no consistency checks between migrations. ALl migrations will be applied as long as they are after the current migration.

Known issues

  • The column dropping operation doesn't work in SQLite because this library performs all the changes within a transaction, and the SQLite Migrator's DropColumn works by creating a transaction in pure-SQL with a BEGIN when a sub-transaction using a SAVEPOINT is necessary here.

How to use

import (
	"gorm.io/gorm"
	migrate "github.com/fclairamb/gorm-migrate"
)

func performMigrations(db *gorm.DB) error {
    
    steps := []*migrate.MigrationStep{
        {
            Name: "2020-09-12 01:00",
            Up: func(db *gorm.DB) error {
                return db.Migrator().AutoMigrate(&User{})
            },
            Down: func(db *gorm.DB) error {
                if db.Migrator().HasTable(&User{}) {
                    return db.Migrator().DropTable(&User{})
                }
                return nil
            },
        },
        {
            Name: "2020-09-12 02:00",
            Up: func(db *gorm.DB) error {
                return nil
            },
            Down: func(db *gorm.DB) error {
                return nil
            },
        },
    }
    
    if nb, err := migrate.Migrate(db, steps, migrate.UpFull); err != nil {
        return err
    } else if nb > 0 {
        log.Printf("Performed %d migrations !\n", nb)
    }
    return nil
}