59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func init() {
|
|
Migrations.MustRegister(
|
|
// UP migration
|
|
func(ctx context.Context, conn *bun.DB) error {
|
|
_, err := conn.NewCreateTable().
|
|
Model((*db.PlayoffSeriesSchedule)(nil)).
|
|
IfNotExists().
|
|
ForeignKey(`("series_id") REFERENCES "playoff_series" ("id") ON DELETE CASCADE`).
|
|
ForeignKey(`("proposed_by_team_id") REFERENCES "teams" ("id")`).
|
|
ForeignKey(`("accepted_by_team_id") REFERENCES "teams" ("id")`).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create index on series_id for faster lookups
|
|
_, err = conn.NewCreateIndex().
|
|
Model((*db.PlayoffSeriesSchedule)(nil)).
|
|
Index("idx_playoff_series_schedules_series_id").
|
|
Column("series_id").
|
|
IfNotExists().
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create index on status for filtering
|
|
_, err = conn.NewCreateIndex().
|
|
Model((*db.PlayoffSeriesSchedule)(nil)).
|
|
Index("idx_playoff_series_schedules_status").
|
|
Column("status").
|
|
IfNotExists().
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
},
|
|
// DOWN migration
|
|
func(ctx context.Context, conn *bun.DB) error {
|
|
_, err := conn.NewDropTable().
|
|
Model((*db.PlayoffSeriesSchedule)(nil)).
|
|
IfExists().
|
|
Exec(ctx)
|
|
return err
|
|
},
|
|
)
|
|
}
|