Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alob-mtc opened this issue Oct 24, 2022 · 3 comments
Assignees
Labels
type:feature_request feature request

Comments

@alob-mtc
Copy link

alob-mtc commented Oct 24, 2022

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

@github-actions github-actions bot added the type:feature_request feature request label Oct 24, 2022
@Huholoman
Copy link

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
Copy link

Mcklmo commented Nov 28, 2023

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
Copy link
Author

alob-mtc commented Dec 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request
Projects
None yet
Development

No branches or pull requests

4 participants