feat: add Request and Job schema with migration
Adds the Prisma schema (Request/Job models, RequestStatus/JobState enums), a Prisma client singleton at web/src/lib/db.ts, and the init_request_job migration, applied and verified against Postgres. Also publishes the db service's port (5432:5432) in docker-compose.yml so host-run Prisma commands can reach localhost:5432 for local dev.
This commit is contained in:
@@ -7,6 +7,8 @@ services:
|
|||||||
POSTGRES_DB: ${POSTGRES_DB}
|
POSTGRES_DB: ${POSTGRES_DB}
|
||||||
volumes:
|
volumes:
|
||||||
- postgres-data:/var/lib/postgresql/data
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
|
|||||||
+4
-1
@@ -6,7 +6,10 @@
|
|||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"test": "vitest run"
|
"test": "vitest run",
|
||||||
|
"postinstall": "prisma generate",
|
||||||
|
"db:migrate": "prisma migrate deploy",
|
||||||
|
"db:dev": "prisma migrate dev"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"next": "15.5.20",
|
"next": "15.5.20",
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "RequestStatus" AS ENUM ('pending', 'completed', 'needs_attention');
|
||||||
|
|
||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "JobState" AS ENUM ('requested', 'matching', 'matched', 'downloading', 'tagging', 'imported', 'needs_attention');
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Request" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"artist" TEXT NOT NULL,
|
||||||
|
"album" TEXT NOT NULL,
|
||||||
|
"status" "RequestStatus" NOT NULL DEFAULT 'pending',
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT "Request_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Job" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"requestId" TEXT NOT NULL,
|
||||||
|
"state" "JobState" NOT NULL DEFAULT 'requested',
|
||||||
|
"currentStage" TEXT NOT NULL DEFAULT 'intake',
|
||||||
|
"attempts" INTEGER NOT NULL DEFAULT 0,
|
||||||
|
"error" TEXT,
|
||||||
|
"claimedAt" TIMESTAMP(3),
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "Job_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Job_requestId_key" ON "Job"("requestId");
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Job" ADD CONSTRAINT "Job_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "Request"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Please do not edit this file manually
|
||||||
|
# It should be added in your version-control system (e.g., Git)
|
||||||
|
provider = "postgresql"
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "postgresql"
|
||||||
|
url = env("DATABASE_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
enum RequestStatus {
|
||||||
|
pending
|
||||||
|
completed
|
||||||
|
needs_attention
|
||||||
|
}
|
||||||
|
|
||||||
|
enum JobState {
|
||||||
|
requested
|
||||||
|
matching
|
||||||
|
matched
|
||||||
|
downloading
|
||||||
|
tagging
|
||||||
|
imported
|
||||||
|
needs_attention
|
||||||
|
}
|
||||||
|
|
||||||
|
model Request {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
artist String
|
||||||
|
album String
|
||||||
|
status RequestStatus @default(pending)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
job Job?
|
||||||
|
}
|
||||||
|
|
||||||
|
model Job {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
request Request @relation(fields: [requestId], references: [id], onDelete: Cascade)
|
||||||
|
requestId String @unique
|
||||||
|
state JobState @default(requested)
|
||||||
|
currentStage String @default("intake")
|
||||||
|
attempts Int @default(0)
|
||||||
|
error String?
|
||||||
|
claimedAt DateTime?
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
|
||||||
|
const globalForPrisma = globalThis as unknown as { prisma?: PrismaClient };
|
||||||
|
|
||||||
|
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
||||||
Reference in New Issue
Block a user