added season types and changed new season to be a modal

This commit is contained in:
2026-02-18 19:43:54 +11:00
parent 42282d05b1
commit 2db24c3f77
13 changed files with 561 additions and 263 deletions

View File

@@ -498,13 +498,22 @@ func ResetDatabase(ctx context.Context, cfg *config.Config) error {
conn := db.NewDB(cfg.DB)
defer func() { _ = conn.Close() }()
models := conn.RegisterModels()
conn.RegisterModels()
for _, model := range models {
if err := conn.ResetModel(ctx, model); err != nil {
return errors.Wrap(err, "reset model")
}
err = RunMigrations(ctx, cfg, "rollback", "all")
if err != nil {
return errors.Wrap(err, "RunMigrations: rollback")
}
err = RunMigrations(ctx, cfg, "up", "all")
if err != nil {
return errors.Wrap(err, "RunMigrations: up")
}
// for _, model := range models {
// if err := conn.ResetModel(ctx, model); err != nil {
// return errors.Wrap(err, "reset model")
// }
// }
fmt.Println("✅ Database reset complete")
return nil

View File

@@ -55,13 +55,7 @@ func init() {
if err != nil {
return err
}
// Remove slap_version column from seasons table
_, err = conn.NewDropColumn().
Model((*db.Season)(nil)).
ColumnExpr("slap_version").
Exec(ctx)
return err
return nil
},
)
}

View File

@@ -0,0 +1,63 @@
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 {
// Add your migration code here
_, err := conn.NewAddColumn().
Model((*db.Season)(nil)).
IfNotExists().
ColumnExpr("type VARCHAR NOT NULL").
Exec(ctx)
if err != nil {
return err
}
leagues := []db.League{
{
Name: "Pro League",
ShortName: "Pro",
Description: "For the most experienced Slapshotters in OSL",
},
{
Name: "Intermediate League",
ShortName: "IM",
Description: "For returning players who've been practicing in RPUGs and PUBs",
},
{
Name: "Open League",
ShortName: "Open",
Description: "For new players just getting started with Slapshot",
},
{
Name: "Draft League",
ShortName: "Draft",
Description: "A league where teams are selected by a draft system",
},
}
for _, league := range leagues {
_, err = conn.NewInsert().
Model(&league).
On("CONFLICT DO NOTHING").
Exec(ctx)
if err != nil {
return err
}
}
return nil
},
// DOWN migration
func(ctx context.Context, conn *bun.DB) error {
// Add your rollback code here
return nil
},
)
}

View File

@@ -25,6 +25,17 @@ const (
StatusCompleted SeasonStatus = "completed"
)
type SeasonType string
func (s SeasonType) String() string {
return string(s)
}
const (
SeasonTypeRegular SeasonType = "regular"
SeasonTypeDraft SeasonType = "draft"
)
type Season struct {
bun.BaseModel `bun:"table:seasons,alias:s"`
@@ -36,13 +47,14 @@ type Season struct {
FinalsStartDate bun.NullTime `bun:"finals_start_date" json:"finals_start_date"`
FinalsEndDate bun.NullTime `bun:"finals_end_date" json:"finals_end_date"`
SlapVersion string `bun:"slap_version,notnull,default:'rebound'" json:"slap_version"`
Type string `bun:"type,notnull" json:"type"`
Leagues []League `bun:"m2m:season_leagues,join:Season=League" json:"-"`
Teams []Team `bun:"m2m:team_participations,join:Season=Team" json:"-"`
}
// NewSeason creats a new season
func NewSeason(ctx context.Context, tx bun.Tx, name, version, shortname string,
func NewSeason(ctx context.Context, tx bun.Tx, name, version, shortname, type_ string,
start time.Time, audit *AuditMeta,
) (*Season, error) {
season := &Season{
@@ -50,12 +62,19 @@ func NewSeason(ctx context.Context, tx bun.Tx, name, version, shortname string,
ShortName: strings.ToUpper(shortname),
StartDate: start.Truncate(time.Hour * 24),
SlapVersion: version,
Type: type_,
}
err := Insert(tx, season).
WithAudit(audit, nil).Exec(ctx)
if err != nil {
return nil, errors.WithMessage(err, "db.Insert")
}
if season.Type == SeasonTypeDraft.String() {
err = NewSeasonLeague(ctx, tx, season.ShortName, "Draft", audit)
if err != nil {
return nil, errors.Wrap(err, "NewSeasonLeague")
}
}
return season, nil
}