schema: add check constraint requiring tournament_id for qualifying events

Team sports (NHL, NBA, etc.) have scoring_events with no canonical
tournament, so we can't require tournament_id globally. The constraint
only applies when is_qualifying_event=true.

Phase 3 Task 1 of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Chris Parsons 2026-05-01 21:19:14 +00:00
parent 327c7b91ca
commit 0040e078d8
No known key found for this signature in database

View file

@ -1,4 +1,4 @@
import { integer, pgTable, varchar, uuid, timestamp, pgEnum, boolean, text, date, decimal, uniqueIndex, index, jsonb } from "drizzle-orm/pg-core";
import { check, integer, pgTable, varchar, uuid, timestamp, pgEnum, boolean, text, date, decimal, uniqueIndex, index, jsonb } from "drizzle-orm/pg-core";
import { relations, sql } from "drizzle-orm";
export const users = pgTable("users", {
@ -554,7 +554,12 @@ export const scoringEvents = pgTable("scoring_events", {
completedAt: timestamp("completed_at"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});
}, (t) => ({
qualifyingEventsRequireTournament: check(
"scoring_events_qualifying_require_tournament",
sql`NOT ${t.isQualifyingEvent} OR ${t.tournamentId} IS NOT NULL`,
),
}));
// Results for participants in specific events
export const eventResults = pgTable("event_results", {