Files
mtusi2026lab6/migrations/00009_async_jobs.sql
T

32 lines
1.4 KiB
PL/PgSQL

-- +goose Up
-- +goose StatementBegin
ALTER TABLE attachments DROP CONSTRAINT attachments_status;
ALTER TABLE attachments ADD CONSTRAINT attachments_status CHECK (status IN ('queued','processing','ready','failed'));
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]';
ALTER TABLE attachments ADD COLUMN error_code text;
CREATE TABLE attachment_jobs (
job_id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
pipeline_version int NOT NULL DEFAULT 1 CHECK (pipeline_version=1),
attempt int NOT NULL DEFAULT 0,
staging_key text NOT NULL,
published_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE FUNCTION enqueue_attachment() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF NEW.status='queued' THEN
INSERT INTO attachment_jobs(attachment_id, staging_key) VALUES (NEW.id, NEW.object_key);
END IF;
RETURN NEW;
END $$;
CREATE TRIGGER attachment_job AFTER INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION enqueue_attachment();
-- +goose StatementEnd
-- +goose Down
DROP TRIGGER attachment_job ON attachments;
DROP FUNCTION enqueue_attachment();
DROP TABLE attachment_jobs;
ALTER TABLE attachments DROP COLUMN variants, DROP COLUMN error_code;
ALTER TABLE attachments DROP CONSTRAINT attachments_status;
ALTER TABLE attachments ADD CONSTRAINT attachments_status CHECK (status='ready');