All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #18
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gitea.dwysokinski.me/twhelp/sessions/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 {
|
|
if _, err := tx.NewAddColumn().
|
|
Model(&model.APIKey{}).
|
|
ColumnExpr("name varchar(100)").
|
|
IfNotExists().
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("api_keys - couldn't add the name column: %w", err)
|
|
}
|
|
|
|
if _, err := tx.NewUpdate().
|
|
Model(&model.APIKey{}).
|
|
Set("name = ?", "api key").
|
|
Where("true").
|
|
Exec(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return fmt.Errorf("api_keys - name - couldn't set default value: %w", err)
|
|
}
|
|
|
|
if _, err := tx.ExecContext(ctx, "ALTER TABLE api_keys ALTER COLUMN name SET NOT NULL"); err != nil {
|
|
return fmt.Errorf("api_keys - name - couldn't set not null: %w", err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}, func(ctx context.Context, db *bun.DB) error {
|
|
if _, err := db.NewDropColumn().
|
|
Model(&model.APIKey{}).
|
|
Column("name").
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("api_keys - couldn't drop the name column: %w", err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|