Files
mtusi2026lab5-fork/migrations/0006_attachment_jobs.sql
T
2026-11-16 14:41:07 +03:00

49 lines
2.0 KiB
SQL

-- staging → final prefix; job-очередь и lease
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS pipeline_version integer NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS staging_key text,
ADD COLUMN IF NOT EXISTS ready_at timestamptz,
ADD COLUMN IF NOT EXISTS failed_at timestamptz,
ADD COLUMN IF NOT EXISTS error_code text;
-- idempotency эффекта: (attachment_id, pipeline_version, variant)
CREATE TABLE IF NOT EXISTS attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
variant text NOT NULL,
object_key text NOT NULL,
content_type text NOT NULL,
size_bytes bigint NOT NULL,
width int NOT NULL,
height int NOT NULL,
sha256 char(64) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, pipeline_version, variant)
);
-- outbox + очередь job-ов
CREATE TABLE IF NOT EXISTS attachment_jobs (
job_id uuid PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
attempt int NOT NULL DEFAULT 1,
status text NOT NULL DEFAULT 'queued', -- queued|processing|done|failed
-- outbox
published_at timestamptz,
publish_attempts int NOT NULL DEFAULT 0,
-- lease
lease_owner text,
lease_expires_at timestamptz,
-- retry
next_attempt_at timestamptz NOT NULL DEFAULT now(),
last_error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (attachment_id, pipeline_version) -- одна логическая работа на версию
);
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_unpublished
ON attachment_jobs (next_attempt_at)
WHERE published_at IS NULL AND status = 'queued';
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_lease
ON attachment_jobs (lease_expires_at)
WHERE status = 'processing';