You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ims/database/migrator.go

39 lines
885 B

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...))
},
})
}