-- Delivery requests: what a client asked for, tracked until it has gone out -- with evidence. Closure is derived from packages and manual deliveries, so -- there is no status column to fall out of step. CREATE TABLE "delivery_requests" ( "id" TEXT NOT NULL, "projectId" TEXT NOT NULL, "requestNumber" INTEGER NOT NULL, "episode" TEXT NOT NULL DEFAULT '', "deliverable" TEXT NOT NULL DEFAULT 'EXR', "requestedBy" TEXT NOT NULL, "note" TEXT, "dueDate" TIMESTAMP(3), "createdById" TEXT NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "cancelledAt" TIMESTAMP(3), "cancelledById" TEXT, "cancelReason" TEXT, CONSTRAINT "delivery_requests_pkey" PRIMARY KEY ("id") ); CREATE TABLE "delivery_request_lines" ( "id" TEXT NOT NULL, "requestId" TEXT NOT NULL, "shotId" TEXT NOT NULL, "shotCode" TEXT NOT NULL, "clientCode" TEXT, CONSTRAINT "delivery_request_lines_pkey" PRIMARY KEY ("id") ); CREATE UNIQUE INDEX "delivery_requests_projectId_requestNumber_key" ON "delivery_requests"("projectId", "requestNumber"); CREATE INDEX "delivery_requests_projectId_cancelledAt_idx" ON "delivery_requests"("projectId", "cancelledAt"); CREATE UNIQUE INDEX "delivery_request_lines_requestId_shotId_key" ON "delivery_request_lines"("requestId", "shotId"); CREATE INDEX "delivery_request_lines_shotId_idx" ON "delivery_request_lines"("shotId"); ALTER TABLE "delivery_requests" ADD CONSTRAINT "delivery_requests_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "projects"("id") ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE "delivery_request_lines" ADD CONSTRAINT "delivery_request_lines_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "delivery_requests"("id") ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE "delivery_request_lines" ADD CONSTRAINT "delivery_request_lines_shotId_fkey" FOREIGN KEY ("shotId") REFERENCES "shots"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- A manual delivery closes a request line only with the same evidence a -- package carries: where it was uploaded and who was sent the link. ALTER TABLE "shots" ADD COLUMN "manualDeliveryUrl" TEXT; ALTER TABLE "shots" ADD COLUMN "manualDeliverySharedWith" TEXT;