From 12590ba0dfad60d68b71765802436d7fec2a2aa5 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Fri, 25 Jul 2025 21:48:07 +0300 Subject: [PATCH] Drop some unused columns, make TournamentRounds.maps always to be defined --- app/db/tables.ts | 12 +------ .../api-public/routes/tournament-match.$id.ts | 2 -- .../actions/to.$id.matches.$mid.server.ts | 1 - .../components/MatchActions.tsx | 2 +- .../tournament-bracket/core/mapList.server.ts | 6 +--- .../loaders/to.$id.matches.$mid.server.ts | 1 - .../queries/bestOfsByTournamentId.server.ts | 22 ------------ .../queries/findMatchById.server.ts | 14 +++----- .../tournament/TournamentRepository.server.ts | 2 +- .../095-drop-unused-tournament-columns.js | 35 +++++++++++++++++++ 10 files changed, 43 insertions(+), 54 deletions(-) delete mode 100644 app/features/tournament-bracket/queries/bestOfsByTournamentId.server.ts create mode 100644 migrations/095-drop-unused-tournament-columns.js diff --git a/app/db/tables.ts b/app/db/tables.ts index 1c7c636eb..b7cef6392 100644 --- a/app/db/tables.ts +++ b/app/db/tables.ts @@ -131,12 +131,6 @@ export interface BuildWeapon { weaponSplId: MainWeaponId; } -/** Image associated with the avatar when the event is showcased on the front page */ -export type CalendarEventAvatarMetadata = { - backgroundColor: string; - textColor: string; -}; - export type CalendarEventTag = keyof typeof tags; export interface CalendarEvent { @@ -153,8 +147,6 @@ export interface CalendarEvent { tournamentId: number | null; organizationId: number | null; avatarImgId: number | null; - // TODO: remove in migration - avatarMetadata: JSONColumnTypeNullable; } export interface CalendarEventBadge { @@ -537,8 +529,6 @@ export const TournamentMatchStatus = { }; export interface TournamentMatch { - // TODO: remove - bestOf: Generated<3 | 5 | 7>; chatCode: string | null; groupId: number; id: GeneratedAlways; @@ -620,7 +610,7 @@ export interface TournamentRound { id: GeneratedAlways; number: number; stageId: number; - maps: JSONColumnTypeNullable; + maps: JSONColumnType; } // when updating this also update `defaultBracketSettings` in tournament-utils.ts diff --git a/app/features/api-public/routes/tournament-match.$id.ts b/app/features/api-public/routes/tournament-match.$id.ts index 73ccb25a9..c0bc24bbc 100644 --- a/app/features/api-public/routes/tournament-match.$id.ts +++ b/app/features/api-public/routes/tournament-match.$id.ts @@ -49,7 +49,6 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => { "TournamentMatch.opponentOne", "TournamentMatch.opponentTwo", "Tournament.mapPickingStyle", - "TournamentMatch.bestOf", "TournamentRound.maps", jsonArrayFrom( eb @@ -122,7 +121,6 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => { : []; return resolveMapList({ - bestOf: match.bestOf, tournamentId: match.tournamentId, matchId: id, teams: [match.opponentOne.id, match.opponentTwo.id], diff --git a/app/features/tournament-bracket/actions/to.$id.matches.$mid.server.ts b/app/features/tournament-bracket/actions/to.$id.matches.$mid.server.ts index 2de4ea6c4..afcd3c21c 100644 --- a/app/features/tournament-bracket/actions/to.$id.matches.$mid.server.ts +++ b/app/features/tournament-bracket/actions/to.$id.matches.$mid.server.ts @@ -89,7 +89,6 @@ export const action: ActionFunction = async ({ params, request }) => { const mapList = match.opponentOne?.id && match.opponentTwo?.id ? resolveMapList({ - bestOf: match.bestOf, tournamentId, matchId, teams: [match.opponentOne.id, match.opponentTwo.id], diff --git a/app/features/tournament-bracket/components/MatchActions.tsx b/app/features/tournament-bracket/components/MatchActions.tsx index eb5d3ac38..d6867f090 100644 --- a/app/features/tournament-bracket/components/MatchActions.tsx +++ b/app/features/tournament-bracket/components/MatchActions.tsx @@ -93,7 +93,7 @@ export function MatchActions({ scores[1] + (winnerId === teams[1].id ? 1 : 0), ]; const wouldEndSet = isSetOverByScore({ - count: data.match.roundMaps?.count ?? data.match.bestOf, + count: data.match.roundMaps.count, countType: data.match.roundMaps?.type ?? "BEST_OF", scores: newScore, }); diff --git a/app/features/tournament-bracket/core/mapList.server.ts b/app/features/tournament-bracket/core/mapList.server.ts index 7e62d7bac..5a0897b93 100644 --- a/app/features/tournament-bracket/core/mapList.server.ts +++ b/app/features/tournament-bracket/core/mapList.server.ts @@ -21,11 +21,9 @@ import type { Bracket } from "./Bracket"; interface ResolveCurrentMapListArgs { tournamentId: number; mapPickingStyle: Tables["Tournament"]["mapPickingStyle"]; - /** @deprecated use maps.count instead */ - bestOf: 3 | 5 | 7; matchId: number; teams: [teamOneId: number, teamTwoId: number]; - maps: TournamentRoundMaps | null; + maps: TournamentRoundMaps; pickBanEvents: Array<{ mode: ModeShort; stageId: StageId; @@ -120,8 +118,6 @@ export function resolveFreshTeamPickedMapList( }; const count = () => { - if (!args.maps?.count) return args.bestOf; - if (args.maps.pickBan) { return pickBanCount(args.maps.pickBan, args.maps.count); } diff --git a/app/features/tournament-bracket/loaders/to.$id.matches.$mid.server.ts b/app/features/tournament-bracket/loaders/to.$id.matches.$mid.server.ts index ad959f226..ec6e792b0 100644 --- a/app/features/tournament-bracket/loaders/to.$id.matches.$mid.server.ts +++ b/app/features/tournament-bracket/loaders/to.$id.matches.$mid.server.ts @@ -28,7 +28,6 @@ export const loader = async ({ params }: LoaderFunctionArgs) => { const mapList = match.opponentOne?.id && match.opponentTwo?.id ? resolveMapList({ - bestOf: match.bestOf, tournamentId, matchId, teams: [match.opponentOne.id, match.opponentTwo.id], diff --git a/app/features/tournament-bracket/queries/bestOfsByTournamentId.server.ts b/app/features/tournament-bracket/queries/bestOfsByTournamentId.server.ts deleted file mode 100644 index 55f5edf38..000000000 --- a/app/features/tournament-bracket/queries/bestOfsByTournamentId.server.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { sql } from "~/db/sql"; -import type { Tables } from "~/db/tables"; - -const stm = sql.prepare(/* sql */ ` - select - "TournamentRound"."id" as "roundId", - "TournamentMatch"."bestOf" - from "TournamentRound" - left join "TournamentMatch" on "TournamentRound"."id" = "TournamentMatch"."roundId" - left join "TournamentStage" on "TournamentRound"."stageId" = "TournamentStage"."id" - where "TournamentStage"."tournamentId" = @tournamentId - group by "TournamentRound"."id" -`); - -interface BestOfByTournamentId { - roundId: Tables["TournamentRound"]["id"]; - bestOf: Tables["TournamentMatch"]["bestOf"]; -} - -export function bestOfsByTournamentId(tournamentId: number) { - return stm.all({ tournamentId }) as BestOfByTournamentId[]; -} diff --git a/app/features/tournament-bracket/queries/findMatchById.server.ts b/app/features/tournament-bracket/queries/findMatchById.server.ts index 2eea3aec5..17be9e6c9 100644 --- a/app/features/tournament-bracket/queries/findMatchById.server.ts +++ b/app/features/tournament-bracket/queries/findMatchById.server.ts @@ -9,7 +9,6 @@ const stm = sql.prepare(/* sql */ ` "TournamentMatch"."groupId", "TournamentMatch"."opponentOne", "TournamentMatch"."opponentTwo", - "TournamentMatch"."bestOf", "TournamentMatch"."chatCode", "Tournament"."mapPickingStyle", "TournamentRound"."id" as "roundId", @@ -50,27 +49,22 @@ export type FindMatchById = ReturnType; export const findMatchById = (id: number) => { const row = stm.get({ id }) as - | ((Pick< - Tables["TournamentMatch"], - "id" | "groupId" | "bestOf" | "chatCode" - > & + | ((Pick & Pick & { players: string }) & { opponentOne: string; opponentTwo: string; roundId: number; - roundMaps: string | null; + roundMaps: string; }) | undefined; if (!row) return; - const roundMaps = row.roundMaps - ? (JSON.parse(row.roundMaps) as TournamentRoundMaps) - : null; + const roundMaps = JSON.parse(row.roundMaps) as TournamentRoundMaps; return { ...row, - bestOf: (roundMaps?.count ?? row.bestOf) as 3 | 5 | 7, + bestOf: roundMaps.count, roundId: row.roundId, roundMaps, opponentOne: JSON.parse(row.opponentOne) as Match["opponent1"], diff --git a/app/features/tournament/TournamentRepository.server.ts b/app/features/tournament/TournamentRepository.server.ts index 37662ef6f..2c439f150 100644 --- a/app/features/tournament/TournamentRepository.server.ts +++ b/app/features/tournament/TournamentRepository.server.ts @@ -1095,7 +1095,7 @@ export function resetBracket(tournamentStageId: number) { export type TournamentRepositoryInsertableMatch = Omit< Insertable, - "status" | "bestOf" | "chatCode" + "status" | "chatCode" >; export function insertSwissMatches( diff --git a/migrations/095-drop-unused-tournament-columns.js b/migrations/095-drop-unused-tournament-columns.js new file mode 100644 index 000000000..c922ca1be --- /dev/null +++ b/migrations/095-drop-unused-tournament-columns.js @@ -0,0 +1,35 @@ +export function up(db) { + db.transaction(() => { + const roundsWithNullMaps = db + .prepare( + /* sql */ `select "id" from "TournamentRound" where "maps" is null`, + ) + .all() + .map((row) => row.id); + + for (const roundId of roundsWithNullMaps) { + const count = db + .prepare( + /* sql */ `select "bestOf" from "TournamentMatch" where "roundId" = @roundId`, + ) + .get({ roundId }).bestOf; + + db.prepare( + /* sql */ `update "TournamentRound" set "maps" = @maps where "id" = @id`, + ).run({ + id: roundId, + maps: JSON.stringify({ + type: "BEST_OF", + count, + }), + }); + } + + db.prepare( + /* sql */ `alter table "TournamentMatch" drop column "bestOf"`, + ).run(); + db.prepare( + /* sql */ `alter table "CalendarEvent" drop column "avatarMetadata"`, + ).run(); + })(); +}