Dawid Wysokiński
77c52c117e
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #31
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/bundb/internal/model"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func init() {
|
|
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
|
|
return db.RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
|
|
columns := []string{
|
|
"internals",
|
|
"barbarians",
|
|
}
|
|
for _, c := range columns {
|
|
if _, err := tx.NewAddColumn().
|
|
Model(&model.Group{}).
|
|
ColumnExpr("? boolean default false", bun.Safe(c)).
|
|
IfNotExists().
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("couldn't add the %s column: %w", c, err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}, func(ctx context.Context, db *bun.DB) error {
|
|
return db.RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
|
|
columns := []string{
|
|
"internals",
|
|
"barbarians",
|
|
}
|
|
for _, c := range columns {
|
|
if _, err := tx.NewDropColumn().
|
|
Model(&model.Group{}).
|
|
Column(c).
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("couldn't drop the %s column: %w", c, err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
})
|
|
}
|