50 lines
901 B
Go
50 lines
901 B
Go
|
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
|
||
|
})
|
||
|
}
|