Skip to content

Commit

Permalink
feat(db): update schema + migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
naimulcsx committed Nov 19, 2024
1 parent 13bb2eb commit b190856
Show file tree
Hide file tree
Showing 5 changed files with 844 additions and 37 deletions.
108 changes: 107 additions & 1 deletion apps/web/app/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { json, pgTable, timestamp, uuid, varchar } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
import {
boolean,
date,
integer,
json,
pgTable,
primaryKey,
timestamp,
uuid,
varchar,
} from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom(),
Expand All @@ -14,6 +25,101 @@ export const users = pgTable('users', {
metadata: json('metadata').default({}),
});

export const usersRelations = relations(users, ({ many }) => ({
submissions: many(submissions),
}));

export const problems = pgTable('problems', {
id: uuid('id').primaryKey().defaultRandom(),
pid: varchar('pid', { length: 50 }).notNull().unique(),
name: varchar('name', { length: 100 }).notNull(),
url: varchar('url', { length: 255 }).notNull(),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
difficulty: integer('difficulty'),
metadata: json('metadata').default({}),
});

export const problemRelations = relations(problems, ({ many }) => ({
submissions: many(submissions),
problemTags: many(problemTags),
}));

export const tags = pgTable('tags', {
id: uuid('id').primaryKey().defaultRandom(),
name: varchar('name', { length: 50 }).notNull().unique(),
});

export const tagsRelations = relations(tags, ({ many }) => ({
problemTags: many(problemTags),
}));

export const problemTags = pgTable(
'problem_tags',
{
problemId: uuid('problem_id')
.references(() => problems.id)
.notNull(),
tagId: uuid('tag_id')
.references(() => tags.id)
.notNull(),
},
(table) => [primaryKey({ columns: [table.problemId, table.tagId] })],
);

export const problemTagsRelations = relations(problemTags, ({ one }) => ({
problem: one(problems, {
fields: [problemTags.problemId],
references: [problems.id],
}),
tag: one(tags, { fields: [problemTags.tagId], references: [tags.id] }),
}));

export const submissions = pgTable('submissions', {
id: uuid('id').primaryKey().defaultRandom(),
solveTime: integer('solve_time'),
verdict: varchar('verdict', {
enum: ['AC', 'PS', 'WA', 'TLE', 'MLE', 'RE', 'CE', 'SK', 'HCK', 'OTH'],
}),
solvedAt: date('solved_at'),
userId: uuid('user_id')
.references(() => users.id)
.notNull(),
problemId: uuid('problem_id')
.references(() => problems.id)
.notNull(),
isVerified: boolean('is_verified').default(false),
acCount: integer('ac_count').default(0),
psCount: integer('ps_count').default(0),
waCount: integer('wa_count').default(0),
tleCount: integer('tle_count').default(0),
mleCount: integer('mle_count').default(0),
reCount: integer('re_count').default(0),
ceCount: integer('ce_count').default(0),
skCount: integer('sk_count').default(0),
hckCount: integer('hck_count').default(0),
othCount: integer('oth_count').default(0),
metadata: json('metadata').default({}),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
});

export const submissionsRelations = relations(submissions, ({ one }) => ({
user: one(users, { fields: [submissions.userId], references: [users.id] }),
problem: one(problems, {
fields: [submissions.problemId],
references: [problems.id],
}),
}));

export const databaseSchema = {
users,
problems,
tags,
problemTags,
usersRelations,
problemRelations,
tagsRelations,
problemTagsRelations,
submissionsRelations,
};
70 changes: 70 additions & 0 deletions apps/web/drizzle/migrations/0001_add-other-tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
CREATE TABLE IF NOT EXISTS "problem_tags" (
"problem_id" uuid NOT NULL,
"tag_id" uuid NOT NULL,
CONSTRAINT "problem_tags_problem_id_tag_id_pk" PRIMARY KEY("problem_id","tag_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "problems" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"pid" varchar(50) NOT NULL,
"name" varchar(100) NOT NULL,
"url" varchar(255) NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
"difficulty" integer,
"metadata" json DEFAULT '{}'::json,
CONSTRAINT "problems_pid_unique" UNIQUE("pid")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "submissions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"solve_time" integer,
"verdict" varchar,
"solved_at" date,
"user_id" uuid NOT NULL,
"problem_id" uuid NOT NULL,
"is_verified" boolean DEFAULT false,
"ac_count" integer DEFAULT 0,
"ps_count" integer DEFAULT 0,
"wa_count" integer DEFAULT 0,
"tle_count" integer DEFAULT 0,
"mle_count" integer DEFAULT 0,
"re_count" integer DEFAULT 0,
"ce_count" integer DEFAULT 0,
"sk_count" integer DEFAULT 0,
"hck_count" integer DEFAULT 0,
"oth_count" integer DEFAULT 0,
"metadata" json DEFAULT '{}'::json,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "tags" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" varchar(50) NOT NULL,
CONSTRAINT "tags_name_unique" UNIQUE("name")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "problem_tags" ADD CONSTRAINT "problem_tags_problem_id_problems_id_fk" FOREIGN KEY ("problem_id") REFERENCES "public"."problems"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "problem_tags" ADD CONSTRAINT "problem_tags_tag_id_tags_id_fk" FOREIGN KEY ("tag_id") REFERENCES "public"."tags"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "submissions" ADD CONSTRAINT "submissions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "submissions" ADD CONSTRAINT "submissions_problem_id_problems_id_fk" FOREIGN KEY ("problem_id") REFERENCES "public"."problems"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit b190856

Please sign in to comment.