From 60f3cd0df9a991c46b3ba01cc1f1c1fd70c15122 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 10 Jul 2026 18:40:41 +0200 Subject: [PATCH] feat: add Candidate and LibraryItem schema Adds Candidate (per-job download candidates found by the pipeline) and LibraryItem (per-request imported album, deduped on artist+album) tables. LibraryItem.requestId is @unique since Request.libraryItem is a one-to-one back-relation (required for Prisma validation, matching the existing Job.requestId pattern). --- .../migration.sql | 42 ++++++++++++++ web/prisma/schema.prisma | 57 ++++++++++++++----- 2 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 web/prisma/migrations/20260710164008_add_candidate_library_item/migration.sql diff --git a/web/prisma/migrations/20260710164008_add_candidate_library_item/migration.sql b/web/prisma/migrations/20260710164008_add_candidate_library_item/migration.sql new file mode 100644 index 0000000..869bb8f --- /dev/null +++ b/web/prisma/migrations/20260710164008_add_candidate_library_item/migration.sql @@ -0,0 +1,42 @@ +-- CreateTable +CREATE TABLE "Candidate" ( + "id" TEXT NOT NULL, + "jobId" TEXT NOT NULL, + "source" TEXT NOT NULL, + "format" TEXT NOT NULL, + "qualityClass" INTEGER NOT NULL, + "trackCount" INTEGER NOT NULL, + "confidence" DOUBLE PRECISION NOT NULL, + "sourceRef" TEXT NOT NULL, + "chosen" BOOLEAN NOT NULL DEFAULT false, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "Candidate_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "LibraryItem" ( + "id" TEXT NOT NULL, + "requestId" TEXT NOT NULL, + "artist" TEXT NOT NULL, + "album" TEXT NOT NULL, + "path" TEXT NOT NULL, + "source" TEXT NOT NULL, + "format" TEXT NOT NULL, + "qualityClass" INTEGER NOT NULL, + "importedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "LibraryItem_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "LibraryItem_requestId_key" ON "LibraryItem"("requestId"); + +-- CreateIndex +CREATE UNIQUE INDEX "LibraryItem_artist_album_key" ON "LibraryItem"("artist", "album"); + +-- AddForeignKey +ALTER TABLE "Candidate" ADD CONSTRAINT "Candidate_jobId_fkey" FOREIGN KEY ("jobId") REFERENCES "Job"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "LibraryItem" ADD CONSTRAINT "LibraryItem_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "Request"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/web/prisma/schema.prisma b/web/prisma/schema.prisma index 58e42cc..0efa371 100644 --- a/web/prisma/schema.prisma +++ b/web/prisma/schema.prisma @@ -24,23 +24,54 @@ enum JobState { } model Request { - id String @id @default(cuid()) - artist String - album String - status RequestStatus @default(pending) - createdAt DateTime @default(now()) - job Job? + id String @id @default(cuid()) + artist String + album String + status RequestStatus @default(pending) + createdAt DateTime @default(now()) + job Job? + libraryItem LibraryItem? } 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 + candidates Candidate[] +} + +model Candidate { + id String @id @default(cuid()) + job Job @relation(fields: [jobId], references: [id], onDelete: Cascade) + jobId String + source String + format String + qualityClass Int + trackCount Int + confidence Float + sourceRef String + chosen Boolean @default(false) + createdAt DateTime @default(now()) +} + +model LibraryItem { 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 + artist String + album String + path String + source String + format String + qualityClass Int + importedAt DateTime @default(now()) + + @@unique([artist, album]) }