import ssl from urllib.parse import urlparse import asyncpg SCHEMA = """ CREATE TABLE IF NOT EXISTS users ( id UUID PRIMARY KEY, username TEXT UNIQUE NOT NULL, display_name TEXT NOT NULL, password_hash TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS sessions ( token TEXT PRIMARY KEY, user_id UUID NOT NULL REFERENCES users(id), expires_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS chats ( id UUID PRIMARY KEY, title TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS chat_members ( chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, user_id UUID NOT NOT NULL REFERENCES users(id) ON DELETE CASCADE, PRIMARY KEY(chat_id,user_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), text TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS message_attachments ( message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, attachment_id UUID NOT NULL, PRIMARY KEY(message_id,attachment_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, staging_key TEXT NOT NULL, final_key TEXT, status TEXT NOT NULL, error_code TEXT, created_at TIMESTAMPTZ NOT NULL, updated_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS attachment_variants ( attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, 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, PRIMARY KEY(attachment_id,name) ); CREATE TABLE IF NOT EXISTS idempotency ( user_id UUID NOT NULL, method TEXT NOT NULL, path TEXT NOT NULL, key TEXT NOT NULL, body_hash TEXT NOT NULL, status INTEGER NOT NULL, response_json JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL, PRIMARY KEY(user_id,method,path,key) ); CREATE INDEX IF NOT EXISTS idx_sessions_expiry ON sessions(expires_at); CREATE INDEX IF NOT EXISTS idx_messages_chat_time ON messages(chat_id,created_at,id); CREATE INDEX IF NOT EXISTS idx_chats_time ON chats(created_at,id); CREATE INDEX IF NOT EXISTS idx_attachments_status ON attachments(status); """ SCHEMA = SCHEMA.replace("user_id UUID NOT NOT NULL", "user_id UUID NOT NULL") def pg_ssl(database_url: str, ca_path: str) -> ssl.SSLContext: context = ssl.create_default_context(cafile=ca_path) context.check_hostname = True context.verify_mode = ssl.CERT_REQUIRED return context async def connect(database_url: str, ca_path: str): return await asyncpg.connect(database_url, ssl=pg_ssl(database_url, ca_path)) async def migrate(pool): async with pool.acquire() as con: async with con.transaction(): await con.execute("SELECT pg_advisory_xact_lock($1)", 721548103) await con.execute(SCHEMA) await con.execute( "GRANT SELECT, UPDATE ON attachments TO messenger_worker" ) await con.execute( "GRANT SELECT, INSERT, UPDATE ON attachment_variants TO messenger_worker" )