go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.91k stars 3.93k forks source link

Production ready Database migration management (Make AutoMigrate production suitable) #5807

Open alob-mtc opened 2 years ago

alob-mtc commented 2 years ago

Describe the feature

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

Related Issues

Huholoman commented 1 year ago

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.

Mcklmo commented 11 months ago

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.

alob-mtc commented 11 months ago

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