panel/migrations/20241217231158_initial.go

50 lines
901 B
Go
Raw Normal View History

2024-12-20 23:29:45 +01:00
package migrations
import (
"context"
"omnibill.net/omnibill/models"
"github.com/uptrace/bun"
)
func init() {
dbModels := []interface{}{
(*models.User)(nil),
(*models.UserLog)(nil),
(*models.UserLoginMethod)(nil),
}
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
for _, model := range dbModels {
if _, err := tx.NewCreateTable().Model(model).Exec(ctx); err != nil {
return err
}
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}, func(ctx context.Context, db *bun.DB) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
for _, model := range dbModels {
if _, err := tx.NewDropTable().Model(model).Exec(ctx); err != nil {
return err
}
}
if err := tx.Commit(); err != nil {
return err
}
return nil
})
}