102 lines
3.2 KiB
SQL
102 lines
3.2 KiB
SQL
ALTER TABLE attachments
|
|
DROP CONSTRAINT attachments_status;
|
|
|
|
ALTER TABLE attachments
|
|
ADD CONSTRAINT attachments_status
|
|
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
|
|
ADD COLUMN staging_key TEXT,
|
|
ADD COLUMN pipeline_version INTEGER NOT NULL DEFAULT 1
|
|
CHECK (pipeline_version >= 1),
|
|
ADD COLUMN error_code TEXT;
|
|
|
|
ALTER TABLE attachments
|
|
ADD CONSTRAINT attachments_error_state
|
|
CHECK (
|
|
(status = 'failed' AND error_code IS NOT NULL)
|
|
OR
|
|
(status <> 'failed' AND error_code IS NULL)
|
|
);
|
|
|
|
CREATE UNIQUE INDEX attachments_staging_key_unique
|
|
ON attachments (staging_key)
|
|
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 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 > 0),
|
|
width INTEGER NOT NULL CHECK (width > 0),
|
|
height INTEGER NOT NULL CHECK (height > 0),
|
|
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
PRIMARY KEY (attachment_id, pipeline_version, name)
|
|
);
|
|
|
|
CREATE TABLE attachment_jobs (
|
|
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 DEFAULT 'queued'
|
|
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
|
|
attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
|
|
max_attempts INTEGER NOT NULL CHECK (max_attempts >= 1),
|
|
available_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
|
lease_token UUID,
|
|
lease_until TIMESTAMPTZ,
|
|
error_code TEXT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
UNIQUE (attachment_id, pipeline_version),
|
|
|
|
CHECK (attempt <= max_attempts),
|
|
|
|
CHECK (
|
|
(
|
|
status = 'processing'
|
|
AND lease_token IS NOT NULL
|
|
AND lease_until IS NOT NULL
|
|
)
|
|
OR
|
|
(
|
|
status <> 'processing'
|
|
AND lease_token IS NULL
|
|
AND lease_until IS NULL
|
|
)
|
|
),
|
|
|
|
CHECK (
|
|
(status = 'failed' AND error_code IS NOT NULL)
|
|
OR
|
|
(status <> 'failed' AND error_code IS NULL)
|
|
)
|
|
);
|
|
|
|
CREATE INDEX attachment_jobs_recovery_idx
|
|
ON attachment_jobs (lease_until)
|
|
WHERE status = 'processing';
|
|
|
|
CREATE INDEX attachment_jobs_failed_idx
|
|
ON attachment_jobs (updated_at, id)
|
|
WHERE status = 'failed';
|
|
|
|
CREATE TABLE attachment_outbox (
|
|
id UUID PRIMARY KEY,
|
|
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
|
|
attempt INTEGER NOT NULL CHECK (attempt >= 1),
|
|
available_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
|
published_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
UNIQUE (job_id, attempt)
|
|
);
|
|
|
|
CREATE INDEX attachment_outbox_pending_idx
|
|
ON attachment_outbox (available_at, id)
|
|
WHERE published_at IS NULL;
|