-- Idempotent schema for lab5. Applied under a session advisory lock at startup. CREATE TABLE IF NOT EXISTS users ( id uuid PRIMARY KEY, username text NOT NULL UNIQUE, display_name text NOT NULL, password_hash text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE TABLE IF NOT EXISTS sessions ( token_hash text PRIMARY KEY, user_id uuid NOT NULL REFERENCES users (id), expires_at timestamptz NOT NULL, revoked_at timestamptz ); CREATE INDEX IF NOT EXISTS sessions_user_idx ON sessions (user_id); CREATE TABLE IF NOT EXISTS chats ( id uuid PRIMARY KEY, title text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE TABLE IF NOT EXISTS chat_members ( chat_id uuid NOT NULL REFERENCES chats (id), user_id uuid NOT NULL REFERENCES users (id), position integer NOT NULL, PRIMARY KEY (chat_id, user_id) ); CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (user_id, chat_id); CREATE TABLE IF NOT EXISTS messages ( id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats (id), sender_id uuid NOT NULL REFERENCES users (id), body text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE INDEX IF NOT EXISTS messages_chat_order_idx ON messages (chat_id, created_at, id); CREATE TABLE IF NOT EXISTS 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, content_type text NOT NULL, size_bytes integer NOT NULL, sha256 text NOT NULL, status text NOT NULL, error_code text, pipeline_version integer NOT NULL, ready_pipeline_version integer, staging_key text NOT NULL, original_key text NOT NULL, staging_deleted_at timestamptz, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), CONSTRAINT attachments_status_chk CHECK (status IN ('queued', 'processing', 'ready', 'failed')) ); CREATE TABLE IF NOT EXISTS message_attachments ( message_id uuid NOT NULL REFERENCES messages (id), attachment_id uuid NOT NULL REFERENCES attachments (id), position integer NOT NULL, PRIMARY KEY (message_id, position) ); CREATE TABLE IF NOT EXISTS idempotency_keys ( user_id uuid NOT NULL REFERENCES users (id), http_method text NOT NULL, path text NOT NULL, idem_key text NOT NULL, request_hash text NOT NULL, response_json jsonb, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY (user_id, http_method, path, idem_key) ); -- One logical job per (attachment, pipeline version). job_id is stable across redelivery and replay. CREATE TABLE IF NOT EXISTS jobs ( job_id uuid PRIMARY KEY, attachment_id uuid NOT NULL REFERENCES attachments (id), pipeline_version integer NOT NULL, attempt integer NOT NULL, status text NOT NULL, error_code text, error_class text, last_error text, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), next_attempt_at timestamptz, lease_until timestamptz, lease_owner text, CONSTRAINT jobs_version_uniq UNIQUE (attachment_id, pipeline_version), CONSTRAINT jobs_status_chk CHECK (status IN ('queued', 'processing', 'ready', 'failed', 'superseded')) ); CREATE INDEX IF NOT EXISTS jobs_status_idx ON jobs (status, next_attempt_at); CREATE TABLE IF NOT EXISTS attachment_variants ( attachment_id uuid NOT NULL REFERENCES attachments (id), pipeline_version integer NOT NULL, name text NOT NULL, content_type text NOT NULL, size_bytes integer NOT NULL, width integer NOT NULL, height integer NOT NULL, sha256 text NOT NULL, object_key text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY (attachment_id, pipeline_version, name) ); CREATE TABLE IF NOT EXISTS outbox ( id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES jobs (job_id), payload jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), published_at timestamptz ); CREATE INDEX IF NOT EXISTS outbox_unpublished_idx ON outbox (id) WHERE published_at IS NULL; CREATE TABLE IF NOT EXISTS failed_jobs ( id bigserial PRIMARY KEY, job_id uuid NOT NULL, attachment_id uuid NOT NULL, pipeline_version integer NOT NULL, attempt integer NOT NULL, error_code text NOT NULL, error_class text NOT NULL, safe_message text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), replayed_at timestamptz ); CREATE TABLE IF NOT EXISTS job_audit ( id bigserial PRIMARY KEY, job_id uuid, attachment_id uuid, action text NOT NULL, detail jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE TABLE IF NOT EXISTS runtime_control ( key text PRIMARY KEY, value jsonb NOT NULL ); INSERT INTO runtime_control (key, value) VALUES ('outbox_paused', 'false'::jsonb), ('worker_fault', '{"mode": null, "armed": false}'::jsonb), ('max_queued_jobs', 'null'::jsonb) ON CONFLICT (key) DO NOTHING;