55 lines
2.1 KiB
SQL
55 lines
2.1 KiB
SQL
-- +goose Up
|
|
CREATE TYPE attachment_variant_name AS ENUM ('thumbnail', 'crop', 'optimized');
|
|
|
|
ALTER TABLE attachments
|
|
ADD COLUMN staging_key text UNIQUE,
|
|
ADD COLUMN job_id uuid UNIQUE,
|
|
ADD COLUMN pipeline_version int NOT NULL DEFAULT 1 CHECK (pipeline_version > 0),
|
|
ADD COLUMN attempts int NOT NULL DEFAULT 0 CHECK (attempts >= 0),
|
|
ADD COLUMN width int CHECK (width > 0),
|
|
ADD COLUMN height int CHECK (height > 0),
|
|
ADD COLUMN lease_until timestamptz;
|
|
|
|
CREATE INDEX idx_attachments_expired_lease ON attachments (lease_until) WHERE status = 'processing';
|
|
|
|
CREATE TABLE attachment_variants (
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
|
pipeline_version int NOT NULL CHECK (pipeline_version > 0),
|
|
variant attachment_variant_name NOT NULL,
|
|
s3_key text NOT NULL UNIQUE,
|
|
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg', 'image/webp')),
|
|
size_bytes int NOT NULL CHECK (size_bytes > 0),
|
|
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
|
width int NOT NULL CHECK (width > 0),
|
|
height int NOT NULL CHECK (height > 0),
|
|
PRIMARY KEY (attachment_id, pipeline_version, variant)
|
|
);
|
|
|
|
CREATE TABLE outbox (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachments (job_id) ON DELETE CASCADE,
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
|
pipeline_version int NOT NULL CHECK (pipeline_version > 0),
|
|
attempt int NOT NULL CHECK (attempt > 0),
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
available_at timestamptz NOT NULL DEFAULT now(),
|
|
published_at timestamptz,
|
|
UNIQUE (job_id, attempt)
|
|
);
|
|
|
|
CREATE INDEX idx_outbox_pending ON outbox (available_at, id) WHERE published_at IS NULL;
|
|
|
|
-- +goose Down
|
|
DROP TABLE outbox;
|
|
DROP TABLE attachment_variants;
|
|
DROP TYPE attachment_variant_name;
|
|
DROP INDEX idx_attachments_expired_lease;
|
|
ALTER TABLE attachments
|
|
DROP COLUMN height,
|
|
DROP COLUMN width,
|
|
DROP COLUMN lease_until,
|
|
DROP COLUMN attempts,
|
|
DROP COLUMN pipeline_version,
|
|
DROP COLUMN job_id,
|
|
DROP COLUMN staging_key;
|