Skip to content

Commit

Permalink
db: refactor code and add comment referencing DG.pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Sep 24, 2024
1 parent 99104a0 commit 1f054e0
Show file tree
Hide file tree
Showing 21 changed files with 720 additions and 657 deletions.
2 changes: 2 additions & 0 deletions packages/database/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

# Temporary for Migration
data

dist
2 changes: 1 addition & 1 deletion packages/database/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
schema: './src/schema.ts',
schema: './dist/schema/index.js',
out: './drizzle',
dialect: 'postgresql',
dbCredentials: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ EXCEPTION
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "public"."review_status" AS ENUM('PENDING', 'APPROVED', 'REJECTED');
CREATE TYPE "public"."semester" AS ENUM('1', '2', '3');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "public"."semester" AS ENUM('1', '2', '3');
CREATE TYPE "public"."study_program" AS ENUM('S', 'T', 'I');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "public"."study_program" AS ENUM('S', 'T', 'I');
CREATE TYPE "public"."review_status" AS ENUM('PENDING', 'APPROVED', 'REJECTED');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Expand All @@ -34,29 +34,6 @@ EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "cart" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"study_program" "study_program" NOT NULL,
"academic_year" integer NOT NULL,
"semester" "semester" NOT NULL,
"name" text DEFAULT 'Untitled' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "cart_item" (
"id" text PRIMARY KEY NOT NULL,
"cart_id" text NOT NULL,
"course_no" text NOT NULL,
"section_no" integer NOT NULL,
"color" text,
"hidden" boolean DEFAULT false NOT NULL,
"cart_order" integer NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "course" (
"id" text PRIMARY KEY NOT NULL,
"study_program" "study_program" NOT NULL,
Expand All @@ -70,7 +47,7 @@ CREATE TABLE IF NOT EXISTS "course" (
"course_desc_th" text,
"faculty" text NOT NULL,
"department" text NOT NULL,
"credit" real NOT NULL,
"credit" numeric NOT NULL,
"credit_hours" text NOT NULL,
"course_condition" text,
"midterm_start" timestamp,
Expand All @@ -84,28 +61,6 @@ CREATE TABLE IF NOT EXISTS "course" (
CONSTRAINT "course_unique" UNIQUE("study_program","academic_year","semester","course_no")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "review" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"study_program" "study_program" NOT NULL,
"academic_year" integer NOT NULL,
"semester" "semester" NOT NULL,
"course_no" text NOT NULL,
"content" text NOT NULL,
"rating" integer NOT NULL,
"status" "review_status" DEFAULT 'PENDING' NOT NULL,
"rejection_reason" text,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "review_votes" (
"id" text PRIMARY KEY NOT NULL,
"review_id" text NOT NULL,
"user_id" text NOT NULL,
"vote_type" "vote_type" NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "course_section" (
"id" text PRIMARY KEY NOT NULL,
"course_id" text NOT NULL,
Expand Down Expand Up @@ -134,6 +89,51 @@ CREATE TABLE IF NOT EXISTS "course_class" (
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "cart" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"study_program" "study_program" NOT NULL,
"academic_year" integer NOT NULL,
"semester" "semester" NOT NULL,
"name" text DEFAULT 'Untitled' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "cart_item" (
"id" text PRIMARY KEY NOT NULL,
"cart_id" text NOT NULL,
"course_no" text NOT NULL,
"section_no" integer NOT NULL,
"color" text,
"hidden" boolean DEFAULT false NOT NULL,
"cart_order" integer NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "review" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"study_program" "study_program" NOT NULL,
"academic_year" integer NOT NULL,
"semester" "semester" NOT NULL,
"course_no" text NOT NULL,
"content" text NOT NULL,
"rating" integer NOT NULL,
"status" "review_status" DEFAULT 'PENDING' NOT NULL,
"rejection_reason" text,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "review_votes" (
"id" text PRIMARY KEY NOT NULL,
"review_id" text NOT NULL,
"user_id" text NOT NULL,
"vote_type" "vote_type" NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
"id" text PRIMARY KEY NOT NULL,
"email" text NOT NULL,
Expand All @@ -147,43 +147,43 @@ CREATE TABLE IF NOT EXISTS "user" (
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "cart" ADD CONSTRAINT "cart_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "course_section" ADD CONSTRAINT "course_section_course_id_course_id_fk" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "cart_item" ADD CONSTRAINT "cart_item_cart_id_cart_id_fk" FOREIGN KEY ("cart_id") REFERENCES "public"."cart"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "course_class" ADD CONSTRAINT "course_class_section_id_course_section_id_fk" FOREIGN KEY ("section_id") REFERENCES "public"."course_section"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "review" ADD CONSTRAINT "review_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "cart" ADD CONSTRAINT "cart_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "review_votes" ADD CONSTRAINT "review_votes_review_id_review_id_fk" FOREIGN KEY ("review_id") REFERENCES "public"."review"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "cart_item" ADD CONSTRAINT "cart_item_cart_id_cart_id_fk" FOREIGN KEY ("cart_id") REFERENCES "public"."cart"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "review_votes" ADD CONSTRAINT "review_votes_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "review" ADD CONSTRAINT "review_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "course_section" ADD CONSTRAINT "course_section_course_id_course_id_fk" FOREIGN KEY ("course_id") REFERENCES "public"."course"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "review_votes" ADD CONSTRAINT "review_votes_review_id_review_id_fk" FOREIGN KEY ("review_id") REFERENCES "public"."review"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "course_class" ADD CONSTRAINT "course_class_section_id_course_section_id_fk" FOREIGN KEY ("section_id") REFERENCES "public"."course_section"("id") ON DELETE no action ON UPDATE no action;
ALTER TABLE "review_votes" ADD CONSTRAINT "review_votes_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit 1f054e0

Please sign in to comment.