Skip to content

Instantly share code, notes, and snippets.

@molind
Last active February 12, 2024 20:19
Show Gist options
  • Save molind/a67100448b886b7257e30799e06a0718 to your computer and use it in GitHub Desktop.
Save molind/a67100448b886b7257e30799e06a0718 to your computer and use it in GitHub Desktop.
Disable migration in gorm.io
package db
import (
"fmt"
"userspace_back/db/model"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func NewDB() *gorm.DB {
dialector := NoMigrateDialector{Dialector: postgres.Open("dbname=test")}
db, err := gorm.Open(dialector, &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&model.Users, &model.Devices)
if len(Log) > 0 {
panic(Log)
}
return db
}
type NoMigrateDialector struct {
gorm.Dialector
}
func (d NoMigrateDialector) Migrator(db *gorm.DB) gorm.Migrator {
return NoMigrateMigrator{Migrator: d.Dialector.Migrator(db)}
}
type NoMigrateMigrator struct {
gorm.Migrator
}
var Log = []string{}
// Tables
func (m NoMigrateMigrator) CreateTable(dst ...interface{}) error {
Log = append(Log, "CreateTable: "+fmt.Sprintf("%T", dst))
return nil
}
func (m NoMigrateMigrator) DropTable(dst ...interface{}) error {
Log = append(Log, "DropTable: "+fmt.Sprintf("%v", dst))
return nil
}
func (m NoMigrateMigrator) RenameTable(oldName, newName interface{}) error {
Log = append(Log, "RenameTable: "+fmt.Sprintf("%v %v", oldName, newName))
return nil
}
// Columns
func (m NoMigrateMigrator) AddColumn(dst interface{}, field string) error {
Log = append(Log, "AddColumn: "+fmt.Sprintf("%v %v", dst, field))
return nil
}
func (m NoMigrateMigrator) DropColumn(dst interface{}, field string) error {
Log = append(Log, "DropColumn: "+fmt.Sprintf("%v %v", dst, field))
return nil
}
func (m NoMigrateMigrator) AlterColumn(dst interface{}, field string) error {
Log = append(Log, "AlterColumn: "+fmt.Sprintf("%v %v", dst, field))
return nil
}
func (m NoMigrateMigrator) MigrateColumn(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error {
Log = append(Log, "MigrateColumn: "+fmt.Sprintf("%v %v", dst, field))
return nil
}
func (m NoMigrateMigrator) RenameColumn(dst interface{}, oldName, field string) error {
Log = append(Log, "RenameColumn: "+fmt.Sprintf("%v %v %v", dst, oldName, field))
return nil
}
// Views
func (m NoMigrateMigrator) CreateView(name string, option gorm.ViewOption) error {
Log = append(Log, "CreateView: "+fmt.Sprintf("%v %v", name, option))
return nil
}
func (m NoMigrateMigrator) DropView(name string) error {
Log = append(Log, "DropView: "+fmt.Sprintf("%v", name))
return nil
}
// Constraints
func (m NoMigrateMigrator) CreateConstraint(dst interface{}, name string) error {
Log = append(Log, "CreateConstraint: "+fmt.Sprintf("%v %v", dst, name))
return nil
}
func (m NoMigrateMigrator) DropConstraint(dst interface{}, name string) error {
Log = append(Log, "DropConstraint: "+fmt.Sprintf("%v %v", dst, name))
return nil
}
// Indexes
func (m NoMigrateMigrator) CreateIndex(dst interface{}, name string) error {
Log = append(Log, "CreateIndex: "+fmt.Sprintf("%v %v", dst, name))
return nil
}
func (m NoMigrateMigrator) DropIndex(dst interface{}, name string) error {
Log = append(Log, "DropIndex: "+fmt.Sprintf("%v %v", dst, name))
return nil
}
func (m NoMigrateMigrator) RenameIndex(dst interface{}, oldName, newName string) error {
Log = append(Log, "RenameIndex: "+fmt.Sprintf("%v %v %v", dst, oldName, newName))
return nil
}
@thiduzz
Copy link

thiduzz commented May 19, 2022

Nice! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment