From 62127cc5e4fd2cd645aef217c0602cd032bdcff7 Mon Sep 17 00:00:00 2001 From: unurled Date: Mon, 14 Jul 2025 12:40:29 +0200 Subject: [PATCH] schema patched --- src/lib/server/db/schema/brackets.ts | 15 +++++++++---- src/lib/server/db/schema/rounds.ts | 32 +++++++++++++--------------- src/lib/server/db/schema/schema.ts | 2 +- src/lib/server/db/schema/sessions.ts | 2 +- src/lib/server/db/schema/users.ts | 2 +- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/lib/server/db/schema/brackets.ts b/src/lib/server/db/schema/brackets.ts index a4280dd..491d287 100644 --- a/src/lib/server/db/schema/brackets.ts +++ b/src/lib/server/db/schema/brackets.ts @@ -1,6 +1,8 @@ import * as t from 'drizzle-orm/pg-core'; import { timestamps } from '../util'; import { competitions } from './competitions'; +import { rounds } from './rounds'; +import { relations } from 'drizzle-orm'; export const bracketTypes = t.pgEnum('bracket_type', ['WINNER', 'LOSER', 'CONSOLATION', 'MAIN']); @@ -10,9 +12,14 @@ export const brackets = t.pgTable('brackets', { bracketType: bracketTypes('bracket_type').notNull(), position: t.integer('position').default(1).notNull(), isActive: t.boolean('is_active').default(true).notNull(), - competition_id: t - .uuid('competition_id') - .notNull() - .references(() => competitions.id, { onDelete: 'cascade' }), + competition_id: t.uuid('competition_id').notNull(), ...timestamps }); + +export const bracketsRelations = relations(brackets, ({ many, one }) => ({ + competition: one(competitions, { + fields: [brackets.competition_id], + references: [competitions.id] + }), + rounds: many(rounds) +})); diff --git a/src/lib/server/db/schema/rounds.ts b/src/lib/server/db/schema/rounds.ts index 58e5f09..7b7eeab 100644 --- a/src/lib/server/db/schema/rounds.ts +++ b/src/lib/server/db/schema/rounds.ts @@ -8,29 +8,27 @@ import { brackets } from './brackets'; export const schedulingModes = t.pgEnum('scheduling_modes', enumToPgEnum(SchedulingMode)); -export const rounds = t.pgTable( - 'rounds', - { - id: t.uuid('id').primaryKey().defaultRandom(), - name: t.text('name').notNull(), - round_number: t.integer('round_number').notNull(), - nb_matches: t.integer('nb_matches').notNull(), - competition_id: t.uuid('competition_id').notNull(), - scheduling_mode: schedulingModes('scheduling_modes'), - bracket_id: t.uuid('bracket_id').references(() => brackets.id), - ...timestamps - }, - (table) => ({ - uniqueCompetitionRound: t.unique().on(table.competition_id, table.round_number) - }) -); +export const rounds = t.pgTable('rounds', { + id: t.uuid('id').primaryKey().defaultRandom(), + name: t.text('name').notNull(), + round_number: t.integer('round_number').notNull(), + nb_matches: t.integer('nb_matches').notNull(), + competition_id: t.uuid('competition_id').notNull(), + scheduling_mode: schedulingModes('scheduling_modes'), + bracket_id: t.uuid('bracket_id'), + ...timestamps +}); export const roundsRelations = relations(rounds, ({ many, one }) => ({ competition: one(competitions, { fields: [rounds.competition_id], references: [competitions.id] }), - matches: many(matches) + matches: many(matches), + bracket: one(brackets, { + fields: [rounds.bracket_id], + references: [brackets.id] + }) })); export type Round = typeof rounds.$inferSelect; diff --git a/src/lib/server/db/schema/schema.ts b/src/lib/server/db/schema/schema.ts index ada3efc..1bca33a 100644 --- a/src/lib/server/db/schema/schema.ts +++ b/src/lib/server/db/schema/schema.ts @@ -69,7 +69,7 @@ export const usersToPermissions = t.pgTable( .notNull() .references(() => users.id), permission_id: t - .integer('permission_id') + .uuid('permission_id') .notNull() .references(() => permissions.id), granted: t.boolean('granted').notNull(), diff --git a/src/lib/server/db/schema/sessions.ts b/src/lib/server/db/schema/sessions.ts index 2027364..d1c339f 100644 --- a/src/lib/server/db/schema/sessions.ts +++ b/src/lib/server/db/schema/sessions.ts @@ -3,7 +3,7 @@ import { users } from './users'; import { binaryHash, timestamps } from '../util'; export const sessions = t.pgTable('session', { - id: t.text('id').primaryKey(), + id: t.uuid('id').primaryKey(), secret_hash: binaryHash('secret_hash').notNull(), user_id: t .uuid('user_id') diff --git a/src/lib/server/db/schema/users.ts b/src/lib/server/db/schema/users.ts index 635d242..8a259ed 100644 --- a/src/lib/server/db/schema/users.ts +++ b/src/lib/server/db/schema/users.ts @@ -4,7 +4,7 @@ import { relations } from 'drizzle-orm'; import { timestamps } from '../util'; export const users = t.pgTable('user', { - id: t.text('id').primaryKey(), + id: t.uuid('id').primaryKey(), username: t.text('username').notNull().unique(), oauth_id: t.uuid('oauth_id').notNull(), email: t.text('email').notNull().unique(),