39 lines
2.2 KiB
SQL
39 lines
2.2 KiB
SQL
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
|
|
ALTER TABLE attachments ALTER COLUMN object_key DROP NOT NULL;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key text;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS job_id uuid;
|
|
UPDATE attachments SET job_id=gen_random_uuid() WHERE job_id IS NULL;
|
|
ALTER TABLE attachments ALTER COLUMN job_id SET NOT NULL;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version > 0);
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS attempts integer NOT NULL DEFAULT 0 CHECK (attempts >= 0);
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS lease_until timestamptz;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS next_attempt_at timestamptz;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS variants jsonb NOT NULL DEFAULT '[]'::jsonb;
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed'));
|
|
ALTER TABLE attachments ADD CONSTRAINT attachment_stage_for_pending CHECK (status IN ('ready','failed') OR staging_key IS NOT NULL);
|
|
CREATE INDEX attachments_recovery ON attachments(status, lease_until, next_attempt_at);
|
|
CREATE INDEX attachments_job_id ON attachments(job_id);
|
|
|
|
CREATE TABLE job_outbox (
|
|
job_id uuid NOT NULL,
|
|
attempt integer NOT NULL CHECK (attempt >= 1),
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
published_at timestamptz,
|
|
PRIMARY KEY(job_id, attempt)
|
|
);
|
|
CREATE INDEX job_outbox_pending ON job_outbox(available_at, created_at) WHERE published_at IS NULL;
|
|
|
|
CREATE TABLE pipeline_audit (
|
|
id bigserial PRIMARY KEY,
|
|
attachment_id uuid NOT NULL,
|
|
job_id uuid NOT NULL,
|
|
pipeline_version integer NOT NULL,
|
|
event varchar(40) NOT NULL,
|
|
detail jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
|
);
|
|
CREATE INDEX pipeline_audit_attachment ON pipeline_audit(attachment_id, created_at);
|