109 lines
4.5 KiB
SQL
109 lines
4.5 KiB
SQL
-- Задание на обработку, варианты и outbox.
|
|
-- Сообщение брокера не содержит байты и пароли: в outbox лежит только JSON задания.
|
|
-- Ключ эффекта — (attachment_id, pipeline_version, name). Повтор не создаёт вторую строку.
|
|
|
|
ALTER TABLE attachments DROP CONSTRAINT attachments_status_chk;
|
|
|
|
ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued';
|
|
|
|
ALTER TABLE attachments ADD COLUMN error_code text;
|
|
|
|
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1;
|
|
|
|
ALTER TABLE attachments ADD COLUMN staging_key text;
|
|
|
|
ALTER TABLE attachments ADD COLUMN staging_deleted_at timestamptz;
|
|
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
|
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_pipeline_chk CHECK (pipeline_version >= 1);
|
|
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_error_chk CHECK (
|
|
(status = 'failed' AND error_code IN ('invalid_image', 'processing_failed', 'retry_exhausted', 'processing_timeout'))
|
|
OR (status <> 'failed' AND error_code IS NULL)
|
|
);
|
|
|
|
CREATE TABLE attachment_jobs (
|
|
id uuid PRIMARY KEY,
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
|
pipeline_version integer NOT NULL,
|
|
status text NOT NULL,
|
|
attempt integer NOT NULL,
|
|
max_attempts integer NOT NULL,
|
|
lease_owner text,
|
|
lease_until timestamptz,
|
|
next_attempt_at timestamptz,
|
|
error_code text,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
CONSTRAINT attachment_jobs_status_chk CHECK (status IN ('queued', 'processing', 'ready', 'failed', 'superseded')),
|
|
CONSTRAINT attachment_jobs_attempt_chk CHECK (attempt >= 1 AND attempt <= max_attempts),
|
|
CONSTRAINT attachment_jobs_max_chk CHECK (max_attempts BETWEEN 1 AND 20),
|
|
CONSTRAINT attachment_jobs_version_chk CHECK (pipeline_version >= 1),
|
|
CONSTRAINT attachment_jobs_error_chk CHECK (
|
|
error_code IS NULL OR error_code IN (
|
|
'invalid_image', 'processing_failed', 'retry_exhausted', 'processing_timeout', 'superseded'
|
|
)
|
|
),
|
|
CONSTRAINT attachment_jobs_one_version UNIQUE (attachment_id, pipeline_version)
|
|
);
|
|
|
|
CREATE INDEX attachment_jobs_due_idx ON attachment_jobs (status, next_attempt_at);
|
|
|
|
CREATE TABLE attachment_variants (
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
|
pipeline_version integer NOT NULL,
|
|
name text NOT NULL,
|
|
content_type text NOT NULL,
|
|
size_bytes bigint NOT NULL,
|
|
width integer NOT NULL,
|
|
height integer NOT NULL,
|
|
sha256 text NOT NULL,
|
|
object_key text NOT NULL UNIQUE,
|
|
created_at timestamptz NOT NULL,
|
|
PRIMARY KEY (attachment_id, pipeline_version, name),
|
|
CONSTRAINT attachment_variants_name_chk CHECK (name IN ('original', 'thumbnail', 'crop', 'optimized')),
|
|
CONSTRAINT attachment_variants_type_chk CHECK (content_type IN ('image/png', 'image/jpeg', 'image/webp')),
|
|
CONSTRAINT attachment_variants_size_chk CHECK (size_bytes >= 1),
|
|
CONSTRAINT attachment_variants_dim_chk CHECK (width >= 1 AND height >= 1),
|
|
CONSTRAINT attachment_variants_sha_chk CHECK (sha256 ~ '^[a-f0-9]{64}$')
|
|
);
|
|
|
|
CREATE TABLE outbox (
|
|
id uuid PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs (id),
|
|
attempt integer NOT NULL,
|
|
payload jsonb NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
published_at timestamptz,
|
|
CONSTRAINT outbox_attempt_chk CHECK (attempt >= 1)
|
|
);
|
|
|
|
CREATE INDEX outbox_pending_idx ON outbox (created_at) WHERE published_at IS NULL;
|
|
|
|
CREATE TABLE job_events (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL,
|
|
attachment_id uuid NOT NULL,
|
|
pipeline_version integer NOT NULL,
|
|
kind text NOT NULL,
|
|
detail text,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
CONSTRAINT job_events_kind_chk CHECK (char_length(kind) BETWEEN 1 AND 40),
|
|
CONSTRAINT job_events_detail_chk CHECK (detail IS NULL OR char_length(detail) <= 64)
|
|
);
|
|
|
|
CREATE TABLE fault_injections (
|
|
id uuid PRIMARY KEY,
|
|
kind text NOT NULL,
|
|
attachment_id uuid,
|
|
armed boolean NOT NULL DEFAULT true,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
CONSTRAINT fault_injections_kind_chk CHECK (kind IN (
|
|
'skip_inline_publish', 'crash_before_effect', 'crash_after_objects',
|
|
'crash_after_commit', 'transient_once'
|
|
))
|
|
);
|
|
|
|
CREATE INDEX fault_injections_armed_idx ON fault_injections (kind, created_at) WHERE armed;
|