added free agents
This commit is contained in:
91
internal/db/migrations/20260222140000_free_agents.go
Normal file
91
internal/db/migrations/20260222140000_free_agents.go
Normal file
@@ -0,0 +1,91 @@
|
||||
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 {
|
||||
// Create season_league_free_agents table
|
||||
_, err := conn.NewCreateTable().
|
||||
Model((*db.SeasonLeagueFreeAgent)(nil)).
|
||||
IfNotExists().
|
||||
ForeignKey(`("season_id") REFERENCES "seasons" ("id") ON DELETE CASCADE`).
|
||||
ForeignKey(`("league_id") REFERENCES "leagues" ("id") ON DELETE CASCADE`).
|
||||
ForeignKey(`("player_id") REFERENCES "players" ("id") ON DELETE CASCADE`).
|
||||
ForeignKey(`("registered_by_user_id") REFERENCES "users" ("id")`).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create fixture_free_agents table
|
||||
_, err = conn.NewCreateTable().
|
||||
Model((*db.FixtureFreeAgent)(nil)).
|
||||
IfNotExists().
|
||||
ForeignKey(`("fixture_id") REFERENCES "fixtures" ("id") ON DELETE CASCADE`).
|
||||
ForeignKey(`("player_id") REFERENCES "players" ("id") ON DELETE CASCADE`).
|
||||
ForeignKey(`("team_id") REFERENCES "teams" ("id") ON DELETE CASCADE`).
|
||||
ForeignKey(`("nominated_by_user_id") REFERENCES "users" ("id")`).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create index on fixture_free_agents for team lookups
|
||||
_, err = conn.NewCreateIndex().
|
||||
Model((*db.FixtureFreeAgent)(nil)).
|
||||
Index("idx_ffa_fixture_team").
|
||||
Column("fixture_id", "team_id").
|
||||
IfNotExists().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add is_free_agent column to fixture_result_player_stats
|
||||
_, err = conn.NewAddColumn().
|
||||
Model((*db.FixtureResultPlayerStats)(nil)).
|
||||
ColumnExpr("is_free_agent BOOLEAN NOT NULL DEFAULT false").
|
||||
IfNotExists().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
// DOWN migration
|
||||
func(ctx context.Context, conn *bun.DB) error {
|
||||
// Drop is_free_agent column from fixture_result_player_stats
|
||||
_, err := conn.NewDropColumn().
|
||||
Model((*db.FixtureResultPlayerStats)(nil)).
|
||||
ColumnExpr("is_free_agent").
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Drop fixture_free_agents table
|
||||
_, err = conn.NewDropTable().
|
||||
Model((*db.FixtureFreeAgent)(nil)).
|
||||
IfExists().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Drop season_league_free_agents table
|
||||
_, err = conn.NewDropTable().
|
||||
Model((*db.SeasonLeagueFreeAgent)(nil)).
|
||||
IfExists().
|
||||
Exec(ctx)
|
||||
return err
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user