71 lines
3.0 KiB
SQL
71 lines
3.0 KiB
SQL
ALTER TABLE attachments
|
|
DROP CONSTRAINT attachments_status_check,
|
|
ADD CONSTRAINT attachments_status_check
|
|
CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed')),
|
|
ADD COLUMN staging_key text COLLATE "C" CONSTRAINT attachments_staging_key_key UNIQUE,
|
|
ADD COLUMN original_key text COLLATE "C" CONSTRAINT attachments_original_key_key UNIQUE,
|
|
ADD COLUMN published_version integer NOT NULL DEFAULT 0 CHECK (published_version >= 0),
|
|
ADD COLUMN error_code text
|
|
CHECK (error_code IN ('invalid_image', 'processing_failed', 'retry_exhausted')),
|
|
ALTER COLUMN object_key DROP NOT NULL;
|
|
|
|
UPDATE attachments SET original_key = object_key, published_version = 1 WHERE status = 'ready';
|
|
UPDATE attachments SET staging_key = object_key WHERE status = 'pending';
|
|
|
|
CREATE INDEX attachments_staged_idx ON attachments (updated_at) WHERE staging_key IS NOT 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 COLLATE "C" NOT NULL CONSTRAINT attachment_variants_object_key_key UNIQUE,
|
|
content_type text NOT NULL CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
|
|
size_bytes bigint NOT NULL CHECK (size_bytes >= 1),
|
|
width integer NOT NULL CHECK (width >= 1),
|
|
height integer NOT NULL CHECK (height >= 1),
|
|
sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
|
created_at timestamptz NOT NULL,
|
|
PRIMARY KEY (attachment_id, pipeline_version, name)
|
|
);
|
|
|
|
CREATE TABLE jobs (
|
|
job_id uuid PRIMARY KEY,
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
|
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
|
|
status text NOT NULL CHECK (status IN ('queued', 'processing', 'done', 'failed', 'superseded')),
|
|
attempt integer NOT NULL CHECK (attempt >= 1),
|
|
claims integer NOT NULL DEFAULT 0 CHECK (claims >= 0),
|
|
lease_token uuid,
|
|
lease_until timestamptz,
|
|
next_attempt_at timestamptz,
|
|
last_error text,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
CONSTRAINT jobs_effect_key UNIQUE (attachment_id, pipeline_version)
|
|
);
|
|
|
|
CREATE INDEX jobs_lease_idx ON jobs (lease_until) WHERE status = 'processing';
|
|
|
|
CREATE TABLE outbox (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES jobs (job_id),
|
|
payload jsonb NOT NULL,
|
|
available_at timestamptz NOT NULL,
|
|
published_at timestamptz
|
|
);
|
|
|
|
CREATE INDEX outbox_pending_idx ON outbox (available_at, id) WHERE published_at IS NULL;
|
|
CREATE INDEX outbox_job_idx ON outbox (job_id);
|
|
|
|
CREATE TABLE job_events (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL,
|
|
attachment_id uuid NOT NULL,
|
|
kind text NOT NULL,
|
|
attempt integer,
|
|
detail jsonb NOT NULL DEFAULT '{}',
|
|
at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX job_events_attachment_idx ON job_events (attachment_id, id);
|