package database import ( "context" "fmt" "ims/database/system" "ims/database/tenant" "ims/util/db" "ims/util/log" "github.com/go-gormigrate/gormigrate/v2" "gorm.io/gorm" ) func Init(ctx context.Context) error { return System(db.FromContext(ctx), func(m *db.Migrator) error { return m.Migrate() }) } func System(tx *gorm.DB, fn func(m *db.Migrator) error) error { return exec(tx, 0, fn, system.Migrations) } func Tenant(tx *gorm.DB, tenantId uint, fn func(m *db.Migrator) error) error { return exec(tx, tenantId, fn, tenant.Migrations) } func exec(tx *gorm.DB, tenantId uint, fn func(m *db.Migrator) error, migrations []*gormigrate.Migration) error { return fn(&db.Migrator{ Schema: db.TenantSchema(tenantId), TenantId: tenantId, DB: tx, Migrations: migrations, Log: func(s string, a ...any) { log.Debug(fmt.Sprintf(s, a...)) }, }) }