forked from mtusi2026/mtusi2026lab5
59 lines
2.8 KiB
SQL
59 lines
2.8 KiB
SQL
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'ready';
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS error_code TEXT;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT;
|
|
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version INTEGER NOT NULL DEFAULT 1;
|
|
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status;
|
|
ALTER TABLE attachments ADD CONSTRAINT attachments_status CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
|
CREATE INDEX IF NOT EXISTS attachments_staging_idx ON attachments(staging_key) WHERE staging_key IS NOT NULL;
|
|
CREATE TABLE IF NOT EXISTS attachment_variants (
|
|
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
pipeline_version INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
s3_key TEXT NOT NULL,
|
|
content_type TEXT NOT NULL,
|
|
size_bytes BIGINT NOT NULL,
|
|
width INTEGER NOT NULL,
|
|
height INTEGER NOT NULL,
|
|
sha256 CHAR(64) NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (attachment_id, pipeline_version, name),
|
|
CONSTRAINT variants_name CHECK (name IN ('thumbnail', 'crop', 'optimized'))
|
|
);
|
|
CREATE TABLE IF NOT EXISTS attachment_jobs (
|
|
job_id UUID PRIMARY KEY,
|
|
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
pipeline_version INTEGER NOT NULL DEFAULT 1,
|
|
status TEXT NOT NULL,
|
|
attempt INTEGER NOT NULL DEFAULT 0,
|
|
max_attempts INTEGER NOT NULL,
|
|
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
lease_until TIMESTAMPTZ,
|
|
worker_id TEXT,
|
|
error_code TEXT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
CONSTRAINT jobs_status CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
|
|
CONSTRAINT jobs_effect_key UNIQUE (attachment_id, pipeline_version)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS attachment_jobs_status_idx ON attachment_jobs(status, lease_until);
|
|
CREATE TABLE IF NOT EXISTS job_events (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
job_id UUID NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
event TEXT NOT NULL,
|
|
detail TEXT
|
|
);
|
|
CREATE INDEX IF NOT EXISTS job_events_job_idx ON job_events(job_id, id);
|
|
CREATE TABLE IF NOT EXISTS outbox (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
job_id UUID NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
topic TEXT NOT NULL,
|
|
msg_key TEXT NOT NULL,
|
|
payload JSONB NOT NULL,
|
|
available_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
published_at TIMESTAMPTZ
|
|
);
|
|
CREATE INDEX IF NOT EXISTS outbox_pending_idx ON outbox(available_at, id) WHERE published_at IS NULL;
|