Describe the feature

  • Extend AutoMigrate to scan Schema/Model changes, generate SQL migration scripts automatically, and write to file (up.sql and down.sql) to reconcile those changes with the Database with version tracking.
  • Extend AutoMigrate not to run those changes automatically as it currently does, but only provide APIs to allow the user to control when to run the migrations against the database.
  • Extend AutoMigrate to support down migrations as it currently only supports up migrations.
package main

import (
    "fmt"
    migrator "github.com/alob-mtc/migrator/lib"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "time"
)

type User struct {
    ID        string `gorm:"primaryKey;"`
    Username  string `gorm:"index; not null"`
    Active    *bool  `gorm:"index; not null; default:false"`
    CreatedAt time.Time
    UpdatedAt time.Time
}

type Product struct {
    ID        string `gorm:"primaryKey;"`
    Name      string `gorm:"index; not null"`
    CreatedAt time.Time
    DeletedAt gorm.DeletedAt `gorm:"index"`
}


func main() {
    // Connecting to postgres
    dns := "postgres://test:test@localhost:54312/test_db"
    db, err := gorm.Open(postgres.Open(dns), &gorm.Config{})
    if err != nil {
        log.Fatal(err)
    }

    // Register Model
    newMigrator := migrator.New(db, "migrations/sql/")
    newMigrator.RegisterModel(&User{}, &Product{})

}

Example use of User-APIs

Creating Migrations

//.....

func main() {

    //.....

    err = newMigrator.Run(db, "create", "add_username_column")
    if err != nil {
        log.Fatal(err)
    }

}

Running Migrations

//.....

func main() {

    //.....

    err = newMigrator.Run(db, "up")
    if err != nil {
        log.Fatal(err)
    }

}

Rolling Back Migrations

//.....

func main() {

    //.....

    err = newMigrator.Run(db, "down")
    if err != nil {
        log.Fatal(err)
    }

}

Example implementation in a stand-alone lib here (https://github.com/alob-mtc/migrator), but I am of the opinion that if gorm adopts this internally will be nice

Motivation

  • AutoMigrate is not production suitable, as the current behaviour is not ideal for a production environment.
  • Sequelize and TypeORM from the Javascript ecosystem help the user better manage database migration. Auto-generate migration to a file and allow the user to run and rollback these SQL migrations

Related Issues

Comment From: Huholoman

Go migrate works well, no need to add this functionality here. But i miss a diff cmd which would generate sql migration scripts so much.

Comment From: Mcklmo

I'm developing an app that currently uses Gorm as Postgres Orm. We are expected to deliver production ready code in January. How do you currently work around this issue with Gorm? Do you use it's Migrator interfaces? Would be extremely appreciative of some example projects using Gorm in production.

Comment From: alob-mtc

Gorm has an ORM is production ready for sure. Its auto-migrate isn't suitable for production environments has you tend to want to have move control over your database migration in a production setting, so you might need to use a different tool that lets you manage database migrations separately.

(https://github.com/alob-mtc/migrator) is a tool I’m developing to address this. Please Note that it’s still in development, and you are welcome to give it a try

Comment From: iamyegor

@Mcklmo did you figure out what to do about this?

Comment From: Mcklmo

@Mcklmo did you figure out what to do about this?

We are using golang-migrate as migration tool along with Gorm as ORM 😃

https://github.com/golang-migrate/migrate

Comment From: iamyegor

@Mcklmo Thanks! Will check it out