Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/db/src/schemas/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
primaryKey,
integer,
index,
uniqueIndex,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import type { AdapterAccount } from "next-auth/adapters";

export const users = pgTable(
Expand All @@ -18,7 +20,12 @@ export const users = pgTable(
image: text("image"),
},
(table) => [
// For the exact-match lookups (admin grant, manual check-in, bans).
index("user_email_idx").on(table.email),
// One account per address, case-insensitive: the auth adapter looks users
// up by lower(email) and assumes one row comes back, while providers keep
// whatever casing the address was typed in.
uniqueIndex("user_email_lower_unique_idx").on(sql`lower(${table.email})`),
index("user_name_idx").on(table.name),
],
);
Expand Down
2 changes: 2 additions & 0 deletions packages/db/src/schemas/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
integer,
index,
uniqueIndex,
check,
} from "drizzle-orm/pg-core";
import { relations, sql } from "drizzle-orm";
import { users } from "./auth";
Expand Down Expand Up @@ -64,6 +65,7 @@ export const events = pgTable(
),
// Both bootcamp pages read every session of one term.
index("event_bootcamp_term_idx").on(table.bootcampTerm),
check("event_max_check_ins_check", sql`${table.maxCheckIns} > 0`),
],
);

Expand Down
6 changes: 6 additions & 0 deletions packages/db/src/schemas/hackathons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
unique,
uniqueIndex,
foreignKey,
check,
} from "drizzle-orm/pg-core";
import { relations, sql } from "drizzle-orm";
import { users } from "./auth";
Expand Down Expand Up @@ -72,6 +73,8 @@ export const hackathons = pgTable(
// non-uuid argument on this column — two editions sharing a name make one
// permanently unreachable through the admin UI, with no error.
unique("unique_hackathon_name").on(table.name),
check("hackathon_dates_check", sql`${table.endDate} >= ${table.startDate}`),
check("hackathon_max_participants_check", sql`${table.maxParticipants} > 0`),
],
);

Expand All @@ -97,6 +100,7 @@ export const hackathonTeams = pgTable(
(table) => [
index("team_hackathon_id_idx").on(table.hackathonId),
index("team_captain_id_idx").on(table.captainId),
check("team_max_members_check", sql`${table.maxMembers} > 0`),
],
);

Expand Down Expand Up @@ -423,6 +427,8 @@ export const hackathonEvents = pgTable(
uniqueIndex("event_check_in_code_idx")
.on(table.hackathonId, table.checkInCode)
.where(sql`${table.checkInCode} is not null`),
check("hackathon_event_times_check", sql`${table.endTime} >= ${table.startTime}`),
check("hackathon_event_points_check", sql`${table.points} >= 0`),
],
);

Expand Down
29 changes: 25 additions & 4 deletions packages/db/src/schemas/judging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
uniqueIndex,
primaryKey,
foreignKey,
check,
} from "drizzle-orm/pg-core";
import { relations, sql } from "drizzle-orm";
import { users } from "./auth";
Expand All @@ -26,8 +27,9 @@ import { hackathons, hackathonProjects } from "./hackathons";
// (judge.ts) and the retired panel's tables share this database: `project`,
// `track`, `vote`, `judging_project` and `judge` are all taken.
//
// No CHECK constraints, matching the rest of the schema: ranges are enforced
// by the API, which is the only writer.
// The API enforces every range. The CHECK constraints below repeat the ones
// a wrong number would corrupt (ratings, schedule arithmetic), so a bug or a
// hand-written UPDATE fails loudly instead of skewing results.

// One row per edition: the state machine and the switches organisers flip on
// judging day.
Expand Down Expand Up @@ -329,7 +331,14 @@ export const judgingEntryChallenges = pgTable(
isOptIn: boolean("is_opt_in").notNull().default(true),
},
(table) => [
primaryKey({ columns: [table.entryId, table.challengeId] }),
// Declared in the order drizzle-kit reads it back (constraint_column_usage,
// which lists columns by name), under the name the key already has. Written
// entry-first, push saw a different key on every deploy and dropped and
// re-added it. The key in the database stays (entry_id, challenge_id).
primaryKey({
name: "judging_entry_challenge_entry_id_challenge_id_pk",
columns: [table.challengeId, table.entryId],
}),
index("judging_entry_challenge_challenge_idx").on(table.challengeId),
foreignKey({
name: "judging_entry_challenge_challenge_fk",
Expand Down Expand Up @@ -529,7 +538,14 @@ export const judgingSchedules = pgTable(
}),
createdAt: timestamp("created_at").defaultNow().notNull(),
},
(table) => [uniqueIndex("judging_schedule_hackathon_idx").on(table.hackathonId)],
(table) => [
uniqueIndex("judging_schedule_hackathon_idx").on(table.hackathonId),
check("judging_schedule_window_check", sql`${table.windowEnd} > ${table.windowStart}`),
check(
"judging_schedule_minutes_check",
sql`least(${table.setupMinutes}, ${table.judgingMinutes}, ${table.teardownMinutes}, ${table.sameBuildingBreakMinutes}, ${table.crossBuildingBreakMinutes}) >= 0`,
),
],
);

// One presentation: a project, for one challenge, in one room, at one time.
Expand Down Expand Up @@ -576,6 +592,10 @@ export const judgingAppointments = pgTable(
),
index("judging_appointment_entry_idx").on(table.entryId),
index("judging_appointment_judge_idx").on(table.assignedJudgeId),
check(
"judging_appointment_times_check",
sql`${table.startsAt} <= ${table.deadlineAt} and ${table.deadlineAt} <= ${table.endsAt}`,
),
foreignKey({
name: "judging_appointment_challenge_fk",
columns: [table.challengeId],
Expand Down Expand Up @@ -748,6 +768,7 @@ export const judgingEvaluationRatings = pgTable(
},
(table) => [
primaryKey({ columns: [table.evaluationId, table.itemId] }),
check("judging_eval_rating_value_check", sql`${table.value} between 1 and 5`),
foreignKey({
name: "judging_eval_rating_evaluation_fk",
columns: [table.evaluationId],
Expand Down
20 changes: 16 additions & 4 deletions packages/db/src/schemas/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
integer,
index,
foreignKey,
check,
} from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
import { relations, sql } from "drizzle-orm";
import { users } from "./auth";
import { hackathons, hackathonParticipants } from "./hackathons";

Expand All @@ -32,7 +33,7 @@ export const hackathonPointAdjustments = pgTable(
.notNull()
.references(() => hackathons.id, { onDelete: "cascade" }),
participantId: uuid("participant_id").notNull(),
// Non-zero, -1000 to 1000. Checked by the API.
// Non-zero, -1000 to 1000. Checked by the API and by the constraint below.
delta: integer("delta").notNull(),
reason: text("reason").notNull(),
actorId: text("actor_id").references(() => users.id, {
Expand All @@ -50,6 +51,10 @@ export const hackathonPointAdjustments = pgTable(
columns: [table.participantId],
foreignColumns: [hackathonParticipants.id],
}).onDelete("cascade"),
check(
"point_adjustment_delta_check",
sql`${table.delta} <> 0 and ${table.delta} between -1000 and 1000`,
),
],
);

Expand All @@ -74,7 +79,10 @@ export const hackathonStoreItems = pgTable(
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
},
(table) => [index("store_item_hackathon_idx").on(table.hackathonId)],
(table) => [
index("store_item_hackathon_idx").on(table.hackathonId),
check("store_item_price_check", sql`${table.price} >= 0 and ${table.stock} >= 0`),
],
);

// An organiser handing something over at the store table. The id comes from
Expand All @@ -94,7 +102,7 @@ export const hackathonStorePurchases = pgTable(
itemName: text("item_name").notNull(),
unitPrice: integer("unit_price").notNull(),
quantity: integer("quantity").notNull(),
// unitPrice * quantity, written by the API.
// unitPrice * quantity, written by the API and held by the constraint below.
total: integer("total").notNull(),
stockTracked: boolean("stock_tracked").notNull(),
actorId: text("actor_id").references(() => users.id, {
Expand Down Expand Up @@ -124,6 +132,10 @@ export const hackathonStorePurchases = pgTable(
columns: [table.itemId],
foreignColumns: [hackathonStoreItems.id],
}).onDelete("set null"),
check(
"store_purchase_amount_check",
sql`${table.quantity} > 0 and ${table.unitPrice} >= 0 and ${table.total} = ${table.unitPrice} * ${table.quantity}`,
),
],
);

Expand Down
Loading