chore: port lab5 solution as lab6 base

This commit is contained in:
2026-11-18 18:40:47 +03:00
parent 470d9d6f1e
commit 98d5787545
105 changed files with 21898 additions and 0 deletions
@@ -0,0 +1,50 @@
CREATE TABLE "users" (
"id" UUID NOT NULL,
"username" VARCHAR(32) NOT NULL,
"display_name" VARCHAR(100) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "users_pkey" PRIMARY KEY ("id"),
CONSTRAINT "users_username_format" CHECK ("username" ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT "users_display_name_not_blank" CHECK (btrim("display_name") <> '')
);
CREATE TABLE "chats" (
"id" UUID NOT NULL,
"title" VARCHAR(100) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "chats_pkey" PRIMARY KEY ("id"),
CONSTRAINT "chats_title_not_blank" CHECK (btrim("title") <> '')
);
CREATE TABLE "chat_members" (
"chat_id" UUID NOT NULL,
"user_id" UUID NOT NULL,
CONSTRAINT "chat_members_pkey" PRIMARY KEY ("chat_id","user_id")
);
CREATE TABLE "messages" (
"id" UUID NOT NULL,
"chat_id" UUID NOT NULL,
"sender_id" UUID NOT NULL,
"text" VARCHAR(4000) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
CREATE INDEX "chat_members_user_id_idx" ON "chat_members"("user_id");
CREATE INDEX "messages_chat_id_created_at_id_idx" ON "messages"("chat_id", "created_at", "id");
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "messages" ADD CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,14 @@
ALTER TABLE "users" ADD COLUMN "password_hash" TEXT NOT NULL;
CREATE TABLE "sessions" (
"token_hash" CHAR(64) NOT NULL,
"user_id" UUID NOT NULL,
"expires_at" TIMESTAMPTZ(3) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "sessions_pkey" PRIMARY KEY ("token_hash")
);
CREATE INDEX "sessions_user_id_idx" ON "sessions"("user_id");
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,13 @@
CREATE TABLE "idempotency_keys" (
"user_id" UUID NOT NULL,
"chat_id" UUID NOT NULL,
"key" VARCHAR(128) NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"message_id" UUID NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "idempotency_keys_pkey" PRIMARY KEY ("user_id","chat_id","key"),
CONSTRAINT "idempotency_keys_key_format" CHECK ("key" ~ '^[A-Za-z0-9._:-]{1,128}$')
);
ALTER TABLE "idempotency_keys" ADD CONSTRAINT "idempotency_keys_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,45 @@
CREATE TABLE "attachments" (
"id" UUID NOT NULL,
"chat_id" UUID NOT NULL,
"owner_id" UUID NOT NULL,
"filename" VARCHAR(255) NOT NULL,
"content_type" VARCHAR(32) NOT NULL,
"size_bytes" INTEGER NOT NULL,
"sha256" CHAR(64) NOT NULL,
"object_key" VARCHAR(128) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "attachments_pkey" PRIMARY KEY ("id"),
CONSTRAINT "attachments_filename_not_blank" CHECK (btrim("filename") <> ''),
CONSTRAINT "attachments_content_type_allowed" CHECK ("content_type" IN ('image/png', 'image/jpeg')),
CONSTRAINT "attachments_size_range" CHECK ("size_bytes" BETWEEN 1 AND 10485760),
CONSTRAINT "attachments_sha256_format" CHECK ("sha256" ~ '^[a-f0-9]{64}$')
);
CREATE TABLE "message_attachments" (
"message_id" UUID NOT NULL,
"position" SMALLINT NOT NULL,
"attachment_id" UUID NOT NULL,
CONSTRAINT "message_attachments_pkey" PRIMARY KEY ("message_id","position"),
CONSTRAINT "message_attachments_position_range" CHECK ("position" BETWEEN 0 AND 9)
);
CREATE UNIQUE INDEX "attachments_object_key_key" ON "attachments"("object_key");
CREATE INDEX "attachments_chat_id_idx" ON "attachments"("chat_id");
CREATE INDEX "attachments_owner_id_idx" ON "attachments"("owner_id");
CREATE UNIQUE INDEX "message_attachments_message_id_attachment_id_key" ON "message_attachments"("message_id", "attachment_id");
CREATE INDEX "message_attachments_attachment_id_idx" ON "message_attachments"("attachment_id");
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "message_attachments" ADD CONSTRAINT "message_attachments_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "message_attachments" ADD CONSTRAINT "message_attachments_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE NO ACTION ON UPDATE CASCADE;
@@ -0,0 +1,97 @@
ALTER TABLE "attachments"
ADD COLUMN "staging_key" VARCHAR(128),
ADD COLUMN "status" VARCHAR(16) NOT NULL DEFAULT 'ready',
ADD COLUMN "error_code" VARCHAR(64);
ALTER TABLE "attachments" ALTER COLUMN "status" DROP DEFAULT;
ALTER TABLE "attachments"
ADD CONSTRAINT "attachments_status_allowed" CHECK ("status" IN ('queued', 'processing', 'ready', 'failed')),
ADD CONSTRAINT "attachments_error_code_when_failed" CHECK (("status" = 'failed') = ("error_code" IS NOT NULL));
CREATE UNIQUE INDEX "attachments_staging_key_key" ON "attachments"("staging_key");
CREATE TABLE "attachment_variants" (
"attachment_id" UUID NOT NULL,
"pipeline_version" INTEGER NOT NULL,
"name" VARCHAR(16) NOT NULL,
"content_type" VARCHAR(32) NOT NULL,
"object_key" VARCHAR(128) NOT NULL,
"size_bytes" INTEGER NOT NULL,
"width" INTEGER NOT NULL,
"height" INTEGER NOT NULL,
"sha256" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "attachment_variants_pkey" PRIMARY KEY ("attachment_id","pipeline_version","name"),
CONSTRAINT "attachment_variants_name_allowed" CHECK ("name" IN ('thumbnail', 'crop', 'optimized')),
CONSTRAINT "attachment_variants_content_type_allowed" CHECK ("content_type" IN ('image/jpeg', 'image/png', 'image/webp')),
CONSTRAINT "attachment_variants_positive" CHECK ("pipeline_version" >= 1 AND "size_bytes" >= 1 AND "width" >= 1 AND "height" >= 1),
CONSTRAINT "attachment_variants_sha256_format" CHECK ("sha256" ~ '^[a-f0-9]{64}$')
);
CREATE TABLE "attachment_jobs" (
"id" UUID NOT NULL,
"attachment_id" UUID NOT NULL,
"pipeline_version" INTEGER NOT NULL,
"status" VARCHAR(16) NOT NULL,
"attempt" INTEGER NOT NULL,
"max_attempts" INTEGER NOT NULL,
"next_attempt_at" TIMESTAMPTZ(3) NOT NULL,
"lease_owner" UUID,
"lease_until" TIMESTAMPTZ(3),
"error_code" VARCHAR(64),
"last_error" VARCHAR(200),
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"finished_at" TIMESTAMPTZ(3),
CONSTRAINT "attachment_jobs_pkey" PRIMARY KEY ("id"),
CONSTRAINT "attachment_jobs_status_allowed" CHECK ("status" IN ('queued', 'processing', 'ready', 'failed')),
CONSTRAINT "attachment_jobs_attempt_range" CHECK ("pipeline_version" >= 1 AND "attempt" >= 1 AND "attempt" <= "max_attempts"),
CONSTRAINT "attachment_jobs_lease_when_processing" CHECK ("status" <> 'processing' OR ("lease_owner" IS NOT NULL AND "lease_until" IS NOT NULL)),
CONSTRAINT "attachment_jobs_error_code_when_failed" CHECK (("status" = 'failed') = ("error_code" IS NOT NULL))
);
CREATE TABLE "job_events" (
"id" BIGSERIAL NOT NULL,
"job_id" UUID NOT NULL,
"attempt" INTEGER NOT NULL,
"event" VARCHAR(32) NOT NULL,
"detail" VARCHAR(200),
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "job_events_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "outbox" (
"id" BIGSERIAL NOT NULL,
"job_id" UUID NOT NULL,
"payload" JSONB NOT NULL,
"available_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"published_at" TIMESTAMPTZ(3),
"publish_attempts" INTEGER NOT NULL DEFAULT 0,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "outbox_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "attachment_variants_object_key_key" ON "attachment_variants"("object_key");
CREATE UNIQUE INDEX "attachment_jobs_attachment_id_pipeline_version_key" ON "attachment_jobs"("attachment_id", "pipeline_version");
CREATE INDEX "attachment_jobs_status_lease_until_idx" ON "attachment_jobs"("status", "lease_until");
CREATE INDEX "job_events_job_id_idx" ON "job_events"("job_id");
CREATE INDEX "outbox_published_at_available_at_idx" ON "outbox"("published_at", "available_at");
CREATE INDEX "outbox_job_id_idx" ON "outbox"("job_id");
ALTER TABLE "attachment_variants" ADD CONSTRAINT "attachment_variants_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "attachment_jobs" ADD CONSTRAINT "attachment_jobs_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "job_events" ADD CONSTRAINT "job_events_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "attachment_jobs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "outbox" ADD CONSTRAINT "outbox_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "attachment_jobs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+3
View File
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
+190
View File
@@ -0,0 +1,190 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(uuid()) @db.Uuid
username String @unique @db.VarChar(32)
displayName String @map("display_name") @db.VarChar(100)
passwordHash String @map("password_hash")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
memberships ChatMember[]
messages Message[]
sessions Session[]
attachments Attachment[]
@@map("users")
}
model Session {
tokenHash String @id @map("token_hash") @db.Char(64)
userId String @map("user_id") @db.Uuid
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@map("sessions")
}
model Chat {
id String @id @default(uuid()) @db.Uuid
title String @db.VarChar(100)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
members ChatMember[]
messages Message[]
attachments Attachment[]
@@map("chats")
}
model ChatMember {
chatId String @map("chat_id") @db.Uuid
userId String @map("user_id") @db.Uuid
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Restrict)
@@id([chatId, userId])
@@index([userId])
@@map("chat_members")
}
model Message {
id String @id @default(uuid()) @db.Uuid
chatId String @map("chat_id") @db.Uuid
senderId String @map("sender_id") @db.Uuid
text String @db.VarChar(4000)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
sender User @relation(fields: [senderId], references: [id], onDelete: Restrict)
idempotencyKeys IdempotencyKey[]
attachments MessageAttachment[]
@@index([chatId, createdAt, id])
@@map("messages")
}
model Attachment {
id String @id @default(uuid()) @db.Uuid
chatId String @map("chat_id") @db.Uuid
ownerId String @map("owner_id") @db.Uuid
filename String @db.VarChar(255)
contentType String @map("content_type") @db.VarChar(32)
sizeBytes Int @map("size_bytes")
sha256 String @db.Char(64)
objectKey String @unique @map("object_key") @db.VarChar(128)
stagingKey String? @unique @map("staging_key") @db.VarChar(128)
status String @db.VarChar(16)
errorCode String? @map("error_code") @db.VarChar(64)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3)
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict)
messages MessageAttachment[]
variants AttachmentVariant[]
jobs AttachmentJob[]
@@index([chatId])
@@index([ownerId])
@@map("attachments")
}
model AttachmentVariant {
attachmentId String @map("attachment_id") @db.Uuid
pipelineVersion Int @map("pipeline_version")
name String @db.VarChar(16)
contentType String @map("content_type") @db.VarChar(32)
objectKey String @unique @map("object_key") @db.VarChar(128)
sizeBytes Int @map("size_bytes")
width Int
height Int
sha256 String @db.Char(64)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: Cascade)
@@id([attachmentId, pipelineVersion, name])
@@map("attachment_variants")
}
model AttachmentJob {
id String @id @db.Uuid
attachmentId String @map("attachment_id") @db.Uuid
pipelineVersion Int @map("pipeline_version")
status String @db.VarChar(16)
attempt Int
maxAttempts Int @map("max_attempts")
nextAttemptAt DateTime @map("next_attempt_at") @db.Timestamptz(3)
leaseOwner String? @map("lease_owner") @db.Uuid
leaseUntil DateTime? @map("lease_until") @db.Timestamptz(3)
errorCode String? @map("error_code") @db.VarChar(64)
lastError String? @map("last_error") @db.VarChar(200)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3)
finishedAt DateTime? @map("finished_at") @db.Timestamptz(3)
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: Cascade)
events JobEvent[]
outbox OutboxEntry[]
@@unique([attachmentId, pipelineVersion])
@@index([status, leaseUntil])
@@map("attachment_jobs")
}
model JobEvent {
id BigInt @id @default(autoincrement())
jobId String @map("job_id") @db.Uuid
attempt Int
event String @db.VarChar(32)
detail String? @db.VarChar(200)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
job AttachmentJob @relation(fields: [jobId], references: [id], onDelete: Cascade)
@@index([jobId])
@@map("job_events")
}
model OutboxEntry {
id BigInt @id @default(autoincrement())
jobId String @map("job_id") @db.Uuid
payload Json
availableAt DateTime @default(now()) @map("available_at") @db.Timestamptz(3)
publishedAt DateTime? @map("published_at") @db.Timestamptz(3)
publishAttempts Int @default(0) @map("publish_attempts")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
job AttachmentJob @relation(fields: [jobId], references: [id], onDelete: Cascade)
@@index([publishedAt, availableAt])
@@index([jobId])
@@map("outbox")
}
model MessageAttachment {
messageId String @map("message_id") @db.Uuid
position Int @db.SmallInt
attachmentId String @map("attachment_id") @db.Uuid
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: NoAction)
@@id([messageId, position])
@@unique([messageId, attachmentId])
@@index([attachmentId])
@@map("message_attachments")
}
model IdempotencyKey {
userId String @map("user_id") @db.Uuid
chatId String @map("chat_id") @db.Uuid
key String @db.VarChar(128)
requestHash String @map("request_hash") @db.Char(64)
messageId String @map("message_id") @db.Uuid
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
@@id([userId, chatId, key])
@@map("idempotency_keys")
}