82 lines
4.3 KiB
SQL
82 lines
4.3 KiB
SQL
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','rejected'));
|
|
ALTER TABLE attachments DROP CONSTRAINT attachments_ready_complete;
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_accepted_complete CHECK (
|
|
status NOT IN ('queued','processing','ready','failed') OR
|
|
(size_bytes IS NOT NULL AND sha256 IS NOT NULL AND width IS NOT NULL AND height IS NOT NULL));
|
|
ALTER TABLE attachments DROP CONSTRAINT attachments_unfinished_expiry;
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_unfinished_expiry CHECK (
|
|
status NOT IN ('pending','rejected','deleting') OR expires_at IS NOT NULL);
|
|
ALTER TABLE attachments
|
|
ADD COLUMN staging_key text UNIQUE,
|
|
ADD COLUMN error_code text CHECK (error_code ~ '^[a-z_]{1,64}$'),
|
|
ADD COLUMN ready_pipeline_version integer CHECK (ready_pipeline_version >= 1);
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_failed_code
|
|
CHECK (status <> 'failed' OR error_code IS NOT NULL);
|
|
DROP INDEX attachments_unfinished_idx;
|
|
CREATE INDEX attachments_unfinished_idx ON attachments (expires_at)
|
|
WHERE status IN ('pending','rejected','deleting');
|
|
CREATE INDEX attachments_staging_idx ON attachments (updated_at)
|
|
WHERE staging_key IS NOT NULL AND status IN ('ready','failed');
|
|
|
|
-- Rows accepted by lab4 already have their final original and complete metadata.
|
|
UPDATE attachments SET ready_pipeline_version = 1
|
|
WHERE status = 'ready' AND ready_pipeline_version IS NULL;
|
|
|
|
CREATE TABLE attachment_jobs (
|
|
job_id uuid PRIMARY KEY,
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
|
|
status text NOT NULL CHECK (status IN ('queued','processing','succeeded','failed','superseded')),
|
|
attempt integer NOT NULL DEFAULT 1 CHECK (attempt >= 1),
|
|
max_attempts integer NOT NULL CHECK (max_attempts >= 1),
|
|
next_attempt_at timestamptz(3) NOT NULL DEFAULT now(),
|
|
lease_owner text, lease_token uuid, lease_until timestamptz(3),
|
|
last_error_code text,
|
|
created_at timestamptz(3) NOT NULL DEFAULT now(),
|
|
updated_at timestamptz(3) NOT NULL DEFAULT now(),
|
|
finished_at timestamptz(3),
|
|
CONSTRAINT attachment_jobs_effect_key UNIQUE (attachment_id, pipeline_version),
|
|
CONSTRAINT attachment_jobs_lease CHECK ((status = 'processing') =
|
|
(lease_owner IS NOT NULL AND lease_token IS NOT NULL AND lease_until IS NOT NULL))
|
|
);
|
|
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until) WHERE status = 'processing';
|
|
CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at) WHERE status = 'failed';
|
|
|
|
CREATE TABLE attachment_variants (
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
|
|
variant text NOT NULL CHECK (variant IN ('thumbnail','crop','optimized')),
|
|
object_key text NOT NULL 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 ~ '^[a-f0-9]{64}$'),
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
created_at timestamptz(3) NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (attachment_id, pipeline_version, variant)
|
|
);
|
|
|
|
CREATE TABLE job_outbox (
|
|
id bigserial PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
payload jsonb NOT NULL,
|
|
available_at timestamptz(3) NOT NULL DEFAULT now(),
|
|
published_at timestamptz(3),
|
|
created_at timestamptz(3) NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX job_outbox_pending_idx ON job_outbox (available_at, id) WHERE published_at IS NULL;
|
|
|
|
CREATE TABLE job_events (
|
|
id bigserial PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
event text NOT NULL CHECK (event IN ('created','claimed','retry_scheduled','lease_expired',
|
|
'succeeded','failed','superseded','replayed','duplicate_skipped','staging_deleted')),
|
|
attempt integer NOT NULL,
|
|
detail jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at timestamptz(3) NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX job_events_job_idx ON job_events (job_id, id);
|