Dawid Wysokiński
ffcb965590
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #6
32 lines
769 B
Go
32 lines
769 B
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"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 {
|
|
if _, err := db.NewCreateTable().
|
|
Model(&model.APIKey{}).
|
|
Varchar(defaultVarcharLength).
|
|
ForeignKey(`(user_id) REFERENCES users (id) ON DELETE CASCADE`).
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("couldn't create the 'api_keys' table: %w", err)
|
|
}
|
|
return nil
|
|
}, func(ctx context.Context, db *bun.DB) error {
|
|
if _, err := db.NewDropTable().
|
|
Model(&model.APIKey{}).
|
|
IfExists().
|
|
Cascade().
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("couldn't drop the 'api_keys' table: %w", err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|