CREATE TABLE public.channel ( id uuid NOT NULL, name text NOT NULL, is_public boolean NOT NULL, workspace_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, created_by uuid NOT NULL ); CREATE TABLE public.channel_member ( id uuid NOT NULL, channel_id uuid NOT NULL, user_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.channel_thread ( id uuid NOT NULL, channel_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.channel_thread_message ( id uuid NOT NULL, user_id uuid NOT NULL, channel_thread_id uuid NOT NULL, message text NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.user_message ( id uuid NOT NULL, user_id uuid NOT NULL, recipient_id uuid NOT NULL, message text NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, workspace_id uuid NOT NULL ); CREATE TABLE public.workspace ( id uuid NOT NULL, name text NOT NULL, owner_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, url_slug text NOT NULL ); CREATE TABLE public.workspace_member ( user_id uuid NOT NULL, workspace_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, type text DEFAULT 'member'::text NOT NULL ); CREATE TABLE public.users ( id uuid DEFAULT public.gen_random_uuid() NOT NULL, name text NOT NULL, email text NOT NULL, display_name text, bio text, phone_number text, timezone text, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, last_seen timestamp with time zone, password text NOT NULL ); CREATE VIEW public.online_users AS SELECT users.id, users.last_seen FROM public.users WHERE (users.last_seen >= (now() - '00:00:30'::interval)); CREATE TABLE public.workspace_user_type ( type text NOT NULL ); ALTER TABLE ONLY public.channel_member ADD CONSTRAINT channel_member_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.channel_thread_message ADD CONSTRAINT channel_thread_message_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.channel_thread ADD CONSTRAINT channel_thread_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.channel ADD CONSTRAINT channels_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.user_message ADD CONSTRAINT user_message_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.workspace_user_type ADD CONSTRAINT user_type_pkey PRIMARY KEY (type); ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.workspace_member ADD CONSTRAINT workspace_members_pkey PRIMARY KEY (user_id, workspace_id); ALTER TABLE ONLY public.workspace ADD CONSTRAINT workspace_name_key UNIQUE (name); ALTER TABLE ONLY public.workspace ADD CONSTRAINT workspace_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.workspace ADD CONSTRAINT workspace_url_slug_key UNIQUE (url_slug); ALTER TABLE ONLY public.channel_member ADD CONSTRAINT channel_member_channel_id_fkey FOREIGN KEY (channel_id) REFERENCES public.channel(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.channel_member ADD CONSTRAINT channel_member_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.channel_thread ADD CONSTRAINT channel_thread_channel_id_fkey FOREIGN KEY (channel_id) REFERENCES public.channel(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.channel_thread_message ADD CONSTRAINT channel_thread_message_channel_thread_id_fkey FOREIGN KEY (channel_thread_id) REFERENCES public.channel_thread(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.channel_thread_message ADD CONSTRAINT channel_thread_message_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.channel ADD CONSTRAINT channels_workspace_id_fkey FOREIGN KEY (workspace_id) REFERENCES public.workspace(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.user_message ADD CONSTRAINT user_message_recipient_id_fkey FOREIGN KEY (recipient_id) REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.user_message ADD CONSTRAINT user_message_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.user_message ADD CONSTRAINT user_message_workspace_id_fkey FOREIGN KEY (workspace_id) REFERENCES public.workspace(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.workspace_member ADD CONSTRAINT workspace_member_type_fkey FOREIGN KEY (type) REFERENCES public.workspace_user_type(type) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.workspace_member ADD CONSTRAINT workspace_members_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.workspace_member ADD CONSTRAINT workspace_members_workspace_id_fkey FOREIGN KEY (workspace_id) REFERENCES public.workspace(id) ON UPDATE RESTRICT ON DELETE RESTRICT; ALTER TABLE ONLY public.workspace ADD CONSTRAINT workspace_owner_fkey FOREIGN KEY (owner_id) REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT; INSERT INTO workspace_user_type(type) VALUES ('admin'), ('owner'), ('member');