forked from mtusi2026/mtusi2026lab6
50 lines
2.5 KiB
SQL
50 lines
2.5 KiB
SQL
-- +goose Up
|
|
CREATE TABLE attachments (
|
|
id uuid PRIMARY KEY,
|
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
|
owner_id uuid NOT NULL REFERENCES users(id),
|
|
filename text NOT NULL CHECK(char_length(filename) BETWEEN 1 AND 255),
|
|
content_type text NOT NULL CHECK(content_type IN ('image/png','image/jpeg')),
|
|
size_bytes bigint NOT NULL CHECK(size_bytes BETWEEN 1 AND 10485760),
|
|
sha256 text NOT NULL CHECK(sha256 ~ '^[a-f0-9]{64}$'),
|
|
status text NOT NULL CHECK(status IN ('uploading','queued','processing','ready','failed','missing','abandoned')),
|
|
pipeline_version integer NOT NULL DEFAULT 1 CHECK(pipeline_version>0),
|
|
error_code text NOT NULL DEFAULT '',
|
|
variants jsonb NOT NULL DEFAULT '[]' CHECK(jsonb_typeof(variants)='array' AND jsonb_array_length(variants)<=3),
|
|
staging_expired boolean NOT NULL DEFAULT false,
|
|
staging_cleaned boolean NOT NULL DEFAULT false,
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
FOREIGN KEY(chat_id,owner_id) REFERENCES chat_members(chat_id,user_id)
|
|
);
|
|
CREATE INDEX attachments_owner ON attachments(owner_id,id);
|
|
CREATE INDEX attachments_staging_cleanup ON attachments(updated_at,id) WHERE NOT staging_cleaned;
|
|
CREATE TABLE message_attachments (
|
|
message_id uuid NOT NULL REFERENCES messages(id),
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
|
ordinal integer NOT NULL CHECK(ordinal BETWEEN 0 AND 9),
|
|
PRIMARY KEY(message_id,ordinal),
|
|
UNIQUE(message_id,attachment_id)
|
|
);
|
|
CREATE INDEX attachment_references ON message_attachments(attachment_id);
|
|
CREATE TABLE attachment_jobs (
|
|
id uuid PRIMARY KEY,
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
|
pipeline_version integer NOT NULL CHECK(pipeline_version>0),
|
|
attempt integer NOT NULL DEFAULT 0 CHECK(attempt>=0),
|
|
traceparent text NOT NULL DEFAULT '' CHECK(char_length(traceparent)<=55),
|
|
tracestate text NOT NULL DEFAULT '' CHECK(char_length(tracestate)<=512),
|
|
lease_id uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
|
|
lease_until timestamptz NOT NULL DEFAULT 'epoch',
|
|
next_attempt timestamptz NOT NULL DEFAULT 'epoch',
|
|
published_at timestamptz NOT NULL DEFAULT 'epoch',
|
|
completed boolean NOT NULL DEFAULT false,
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
UNIQUE(attachment_id,pipeline_version)
|
|
);
|
|
CREATE INDEX pending_attachment_jobs ON attachment_jobs(next_attempt,published_at) WHERE NOT completed;
|
|
-- +goose Down
|
|
DROP TABLE attachment_jobs;
|
|
DROP TABLE message_attachments;
|
|
DROP TABLE attachments;
|