Files
mtusi2026lab5/migrations/005_async_attachments.sql
T

96 lines
3.6 KiB
SQL

-- Lab5: asynchronous attachment processing (queued → worker → ready/failed).
-- Additive: extends lab4 attachments with staging, jobs, outbox, variants.
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
ALTER TABLE attachments
ADD CONSTRAINT attachments_status_check
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS error_code TEXT;
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT;
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version INT NOT NULL DEFAULT 1;
-- Final original may be filled only after the worker publishes it.
ALTER TABLE attachments ALTER COLUMN object_key DROP NOT NULL;
CREATE TABLE IF NOT EXISTS attachment_variants (
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version INT NOT NULL,
name TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
width INT NOT NULL,
height INT NOT NULL,
sha256 TEXT NOT NULL,
object_key TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, pipeline_version, name),
CONSTRAINT attachment_variants_name_check
CHECK (name IN ('thumbnail', 'crop', 'optimized')),
CONSTRAINT attachment_variants_sha256_check CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachment_variants_size_check CHECK (size_bytes > 0),
CONSTRAINT attachment_variants_pixels_check CHECK (width > 0 AND height > 0)
);
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 INT NOT NULL DEFAULT 1,
attempt INT NOT NULL DEFAULT 1,
status TEXT NOT NULL,
lease_owner TEXT,
lease_until TIMESTAMPTZ,
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
last_error TEXT,
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT attachment_jobs_status_check
CHECK (status IN ('pending', 'leased', 'done', 'failed')),
CONSTRAINT attachment_jobs_attempt_check CHECK (attempt >= 1)
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_attachment_jobs_one_active
ON attachment_jobs (attachment_id, pipeline_version)
WHERE status IN ('pending', 'leased');
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_lease
ON attachment_jobs (status, lease_until)
WHERE status = 'leased';
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_pending
ON attachment_jobs (next_attempt_at)
WHERE status = 'pending';
-- Transactional outbox: COMMIT of job intent and broker publish are decoupled.
CREATE TABLE IF NOT EXISTS outbox (
id BIGSERIAL PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
payload JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
published_at TIMESTAMPTZ,
publish_attempts INT NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_outbox_unpublished
ON outbox (id)
WHERE published_at IS NULL;
-- Operator-visible failed registry (DLQ mirror) with safe explicit replay.
CREATE TABLE IF NOT EXISTS dead_letter_jobs (
id BIGSERIAL PRIMARY KEY,
job_id UUID NOT NULL,
attachment_id UUID NOT NULL,
pipeline_version INT NOT NULL DEFAULT 1,
payload JSONB NOT NULL,
error_code TEXT NOT NULL,
attempts INT NOT NULL,
failed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
replayed_at TIMESTAMPTZ,
replay_note TEXT
);
CREATE INDEX IF NOT EXISTS idx_dead_letter_open
ON dead_letter_jobs (failed_at)
WHERE replayed_at IS NULL;