nerdware/apps/server/drizzle/0000_loose_doctor_spectrum.sql
AyoubBenziza 9fe30feec5 feat(server): Drizzle + PostgreSQL schema + Open Trivia DB seed
- Drizzle (postgres-js) setup: schema (quiz_category, quiz_question,
  quiz_played, game_history), client (nullable — no DATABASE_URL → null),
  drizzle.config, initial migration, migrate script
- Open Trivia DB seed: curated geek categories, mcq + truefalse, base64
  decode (incl. type/difficulty), 1 req/5s rate limit, idempotent
  (prompt unique); also seeds the FR in-code bank as source 'manual'
- quiz wired to the DB: per-room pool preloaded at game start (loadQuizPool
  excludes already-played), records quiz_played for cross-session
  anti-repeat; falls back to the in-code bank when no DB
- docker-compose for local Postgres; db:* scripts; DATABASE_URL in env

Roadmap V1 step 5. Verified against a real Postgres: migrate + seed (270
OpenTDB + 10 manual), DB-backed game serves OpenTDB questions and fills
quiz_played; no-DB fallback still serves the in-code bank.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 12:24:17 +02:00

39 lines
No EOL
1.6 KiB
SQL

CREATE TABLE "game_history" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"room_code" text NOT NULL,
"played_at" timestamp with time zone DEFAULT now() NOT NULL,
"modes" jsonb,
"results" jsonb
);
--> statement-breakpoint
CREATE TABLE "quiz_category" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" text NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "quiz_category_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE "quiz_played" (
"question_id" uuid NOT NULL,
"room_code" text NOT NULL,
"played_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "quiz_played_question_id_room_code_pk" PRIMARY KEY("question_id","room_code")
);
--> statement-breakpoint
CREATE TABLE "quiz_question" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"category_id" uuid,
"format" text NOT NULL,
"prompt" text NOT NULL,
"difficulty" integer DEFAULT 1 NOT NULL,
"source" text NOT NULL,
"choices" jsonb,
"correct_index" integer,
"accepted_answers" jsonb,
"image_url" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "quiz_question_prompt_unique" UNIQUE("prompt")
);
--> statement-breakpoint
ALTER TABLE "quiz_played" ADD CONSTRAINT "quiz_played_question_id_quiz_question_id_fk" FOREIGN KEY ("question_id") REFERENCES "public"."quiz_question"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "quiz_question" ADD CONSTRAINT "quiz_question_category_id_quiz_category_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."quiz_category"("id") ON DELETE no action ON UPDATE no action;