-- Base Tables CREATE TABLE users ( uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), suuid CHAR(8) UNIQUE NOT NULL, username TEXT UNIQUE NOT NULL CHECK ( length(username) >= 3 AND username ~ '^[a-zA-Z0-9_-]+$' ) , password TEXT NOT NULL CHECK (length(password) >= 8), indexable BOOLEAN DEFAULT false, created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE ('utc'::text, NOW()) NOT NULL ); CREATE TABLE message_threads ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL ); CREATE TABLE thread_participants ( thread_id UUID REFERENCES message_threads (id) ON DELETE CASCADE, user_uuid UUID REFERENCES users (uuid) ON DELETE CASCADE, PRIMARY KEY (thread_id, user_uuid) ); CREATE TABLE messages ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), thread_id UUID REFERENCES message_threads (id) ON DELETE CASCADE, content TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL );