Complete Lab 5 grade 4 async attachment pipeline
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
-- Lab 5: durable staging, transactional outbox and leased image jobs.
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_ck;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_ck CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
||||
ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued';
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS attachments_staging_key_uq ON attachments(staging_key) WHERE staging_key IS NOT NULL;
|
||||
|
||||
CREATE TABLE attachment_jobs (
|
||||
id UUID PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
pipeline_version INTEGER NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1),
|
||||
attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
|
||||
attempts_in_cycle INTEGER NOT NULL DEFAULT 1,
|
||||
max_attempts INTEGER NOT NULL CHECK (max_attempts BETWEEN 1 AND 20),
|
||||
status VARCHAR(16) NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'succeeded', 'failed')),
|
||||
available_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
lease_until TIMESTAMPTZ,
|
||||
last_error_code VARCHAR(64),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMPTZ,
|
||||
CONSTRAINT attachment_jobs_cycle_attempt_ck CHECK (attempts_in_cycle BETWEEN 1 AND max_attempts)
|
||||
);
|
||||
CREATE INDEX attachment_jobs_due_idx ON attachment_jobs(status, available_at);
|
||||
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs(status, lease_until);
|
||||
|
||||
CREATE TABLE job_outbox (
|
||||
id UUID PRIMARY KEY,
|
||||
job_id UUID NOT NULL REFERENCES attachment_jobs(id) ON DELETE CASCADE,
|
||||
attempt INTEGER NOT NULL CHECK (attempt >= 1),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
published_at TIMESTAMPTZ,
|
||||
CONSTRAINT job_outbox_job_attempt_uq UNIQUE (job_id, attempt)
|
||||
);
|
||||
CREATE INDEX job_outbox_pending_idx ON job_outbox(published_at, created_at);
|
||||
|
||||
CREATE TABLE job_replay_audit (
|
||||
id UUID PRIMARY KEY,
|
||||
job_id UUID NOT NULL REFERENCES attachment_jobs(id) ON DELETE CASCADE,
|
||||
previous_attempt INTEGER NOT NULL,
|
||||
new_attempt INTEGER NOT NULL,
|
||||
operator VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CHECK (new_attempt > previous_attempt)
|
||||
);
|
||||
Reference in New Issue
Block a user