feat(attachments): add pipeline and outbox schema and queries
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
-- +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;
|
||||
@@ -0,0 +1,28 @@
|
||||
-- name: CreateAttachmentVariant :exec
|
||||
INSERT INTO attachment_variants (
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
variant,
|
||||
s3_key,
|
||||
content_type,
|
||||
size_bytes,
|
||||
width,
|
||||
height,
|
||||
sha256
|
||||
) VALUES (
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(pipeline_version),
|
||||
sqlc.arg(variant),
|
||||
sqlc.arg(s3_key),
|
||||
sqlc.arg(content_type),
|
||||
sqlc.arg(size_bytes),
|
||||
sqlc.arg(width),
|
||||
sqlc.arg(height),
|
||||
sqlc.arg(sha256)
|
||||
);
|
||||
|
||||
-- name: ListAttachmentVariants :many
|
||||
SELECT * FROM attachment_variants
|
||||
WHERE attachment_id = sqlc.arg(attachment_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
ORDER BY variant;
|
||||
@@ -4,10 +4,14 @@ INSERT INTO attachments (
|
||||
chat_id,
|
||||
owner_id,
|
||||
s3_key,
|
||||
staging_key,
|
||||
job_id,
|
||||
filename,
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
width,
|
||||
height,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
@@ -15,10 +19,14 @@ SELECT
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
sqlc.arg(owner_id)::uuid,
|
||||
sqlc.arg(s3_key)::text,
|
||||
sqlc.narg(staging_key)::text,
|
||||
sqlc.narg(job_id)::uuid,
|
||||
sqlc.arg(filename)::text,
|
||||
sqlc.arg(content_type)::attachment_content_type,
|
||||
sqlc.arg(size_bytes)::int,
|
||||
sqlc.arg(sha256)::text,
|
||||
sqlc.arg(width)::int,
|
||||
sqlc.arg(height)::int,
|
||||
NULLIF(sqlc.arg(error_code)::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
@@ -77,6 +85,30 @@ SELECT
|
||||
) AS referenced
|
||||
FROM attachments a;
|
||||
|
||||
-- name: LockAttachmentJob :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now()
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: LockExpiredAttachmentJobs :many
|
||||
SELECT * FROM attachments
|
||||
WHERE status = 'processing'
|
||||
AND lease_until <= now()
|
||||
ORDER BY lease_until, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
RETURNING *;
|
||||
|
||||
-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
@@ -84,12 +116,73 @@ SET status = 'failed',
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid AND status = 'ready';
|
||||
|
||||
-- name: MarkAttachmentReady :one
|
||||
-- name: ClaimAttachmentJob :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
SET status = 'processing',
|
||||
attempts = attempts + 1,
|
||||
lease_until = sqlc.arg(lease_until)::timestamptz,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND status = 'queued'
|
||||
AND attempts + 1 = sqlc.arg(attempt)::int
|
||||
RETURNING *;
|
||||
|
||||
-- name: CompleteAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
width = sqlc.arg(width)::int,
|
||||
height = sqlc.arg(height)::int,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RetryAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: FailAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RecoverExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
|
||||
-- name: FailExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = 'retry_exhausted',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
|
||||
-- name: DeleteQueuedAttachment :execrows
|
||||
DELETE FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid AND status = 'queued';
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
-- name: CreateOutboxEntry :one
|
||||
INSERT INTO outbox (
|
||||
job_id,
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
attempt,
|
||||
available_at
|
||||
)
|
||||
SELECT
|
||||
job_id,
|
||||
id,
|
||||
pipeline_version,
|
||||
sqlc.arg(attempt)::int,
|
||||
sqlc.arg(available_at)::timestamptz
|
||||
FROM attachments
|
||||
WHERE id = sqlc.arg(attachment_id)::uuid
|
||||
AND job_id IS NOT NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: LockPendingOutboxEntries :many
|
||||
SELECT * FROM outbox
|
||||
WHERE published_at IS NULL
|
||||
AND available_at <= now()
|
||||
ORDER BY available_at, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: MarkOutboxPublished :execrows
|
||||
UPDATE outbox
|
||||
SET published_at = now()
|
||||
WHERE id = sqlc.arg(id)::bigint
|
||||
AND published_at IS NULL;
|
||||
Reference in New Issue
Block a user