forked from mtusi2026/mtusi2026lab5
58 lines
2.8 KiB
SQL
58 lines
2.8 KiB
SQL
-- Durable handoff: staging + queued/job/outbox фиксируются до ответа 202.
|
|
ALTER TABLE attachments DROP CONSTRAINT attachments_status_check;
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check
|
|
CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed', 'deleting'));
|
|
ALTER TABLE attachments ADD COLUMN staging_key text;
|
|
ALTER TABLE attachments ADD COLUMN error_code text;
|
|
UPDATE attachments SET staging_key = object_key WHERE status IN ('pending', 'deleting');
|
|
|
|
CREATE TABLE attachment_jobs (
|
|
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
|
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
|
|
pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version = 1),
|
|
attempt integer NOT NULL DEFAULT 1 CHECK (attempt > 0),
|
|
failures integer NOT NULL DEFAULT 0 CHECK (failures >= 0),
|
|
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
lease_token uuid,
|
|
lease_until timestamptz,
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
|
);
|
|
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until)
|
|
WHERE lease_until IS NOT NULL;
|
|
|
|
CREATE TABLE attachment_outbox (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
|
attempt integer NOT NULL CHECK (attempt > 0),
|
|
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
published_at timestamptz,
|
|
claim_token uuid,
|
|
claim_until timestamptz,
|
|
UNIQUE (job_id, attempt)
|
|
);
|
|
CREATE INDEX attachment_outbox_pending_idx ON attachment_outbox (available_at, id)
|
|
WHERE published_at IS NULL;
|
|
|
|
CREATE TABLE attachment_variants (
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
|
pipeline_version integer NOT NULL CHECK (pipeline_version = 1),
|
|
name text NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
|
object_key text NOT NULL UNIQUE,
|
|
content_type text NOT NULL,
|
|
size_bytes integer NOT NULL CHECK (size_bytes > 0),
|
|
width integer NOT NULL CHECK (width > 0),
|
|
height integer NOT NULL CHECK (height > 0),
|
|
sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
|
PRIMARY KEY (attachment_id, pipeline_version, name)
|
|
);
|
|
|
|
-- failed + error_code в attachments — устойчивый реестр ошибок вместо DLQ.
|
|
CREATE TABLE attachment_replays (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
|
attempt integer NOT NULL,
|
|
operator text NOT NULL CHECK (char_length(operator) BETWEEN 1 AND 128),
|
|
reason text NOT NULL CHECK (char_length(reason) BETWEEN 1 AND 512),
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
|
);
|