Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

30.0 KB
Raw Blame
-- schema definition of email when we migrated it from its own database to macrodb. updated all table names and enum names
-- to be prefixed with email_. definition was generated by pg_dump so it is a little weird.

CREATE TYPE email_backfill_job_status AS ENUM ('Init', 'InProgress', 'Complete', 'Cancelled', 'Failed');
CREATE TYPE email_user_provider_enum AS ENUM ('GMAIL');
CREATE TYPE email_backfill_thread_status AS ENUM ('InProgress', 'Skipped', 'Completed', 'Failed', 'Cancelled');
CREATE TYPE email_backfill_message_status AS ENUM ('InProgress', 'Completed', 'Failed', 'Cancelled');
CREATE TYPE email_message_list_visibility_enum AS ENUM ('Show', 'Hide');
CREATE TYPE email_label_type_enum AS ENUM ('System', 'User');
CREATE TYPE email_recipient_type AS ENUM ('TO', 'CC', 'BCC');
CREATE TYPE email_label_list_visibility_enum AS ENUM ('LabelShow', 'LabelShowIfUnread', 'LabelHide');


CREATE TABLE public.email_attachments
(
    id                     uuid                                   NOT NULL,
    message_id             uuid                                   NOT NULL,
    provider_attachment_id text,
    filename               character varying(512),
    mime_type              character varying(255),
    size_bytes             bigint,
    content_id             character varying(255),
    created_at             timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: attachments_macro; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_attachments_macro
(
    id         uuid                                   NOT NULL,
    message_id uuid                                   NOT NULL,
    item_id    uuid                                   NOT NULL,
    item_type  character varying(255)                 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: backfill_job; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_backfill_jobs
(
    id                       uuid                                                                              NOT NULL,
    link_id                  uuid,
    threads_requested_limit  integer,
    total_threads            integer                          DEFAULT 0                                        NOT NULL,
    status                   public.email_backfill_job_status DEFAULT 'Init'::public.email_backfill_job_status NOT NULL,
    threads_retrieved_count  integer                          DEFAULT 0                                        NOT NULL,
    threads_processed_count  integer                          DEFAULT 0                                        NOT NULL,
    messages_retrieved_count integer                          DEFAULT 0                                        NOT NULL,
    messages_processed_count integer                          DEFAULT 0                                        NOT NULL,
    threads_succeeded_count  integer                          DEFAULT 0                                        NOT NULL,
    threads_skipped_count    integer                          DEFAULT 0                                        NOT NULL,
    threads_failed_count     integer                          DEFAULT 0                                        NOT NULL,
    messages_succeeded_count integer                          DEFAULT 0                                        NOT NULL,
    messages_failed_count    integer                          DEFAULT 0                                        NOT NULL,
    created_at               timestamp with time zone         DEFAULT now()                                    NOT NULL,
    updated_at               timestamp with time zone         DEFAULT now()                                    NOT NULL,
    fusionauth_user_id       text                                                                              NOT NULL
);


--
-- Name: backfill_message; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_backfill_messages
(
    backfill_job_id     uuid                                                                                            NOT NULL,
    thread_provider_id  character varying(255)                                                                          NOT NULL,
    message_provider_id character varying(255)                                                                          NOT NULL,
    status              public.email_backfill_message_status DEFAULT 'InProgress'::public.email_backfill_message_status NOT NULL,
    error_message       text,
    retry_count         integer                              DEFAULT 0                                                  NOT NULL,
    created_at          timestamp with time zone             DEFAULT now()                                              NOT NULL,
    updated_at          timestamp with time zone             DEFAULT now()                                              NOT NULL
);


--
-- Name: backfill_thread; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_backfill_threads
(
    backfill_job_id          uuid                                                                                          NOT NULL,
    thread_provider_id       character varying(255)                                                                        NOT NULL,
    status                   public.email_backfill_thread_status DEFAULT 'InProgress'::public.email_backfill_thread_status NOT NULL,
    messages_retrieved_count integer                             DEFAULT 0                                                 NOT NULL,
    messages_processed_count integer                             DEFAULT 0                                                 NOT NULL,
    metadata_updated         boolean                             DEFAULT false                                             NOT NULL,
    messages_succeeded_count integer                             DEFAULT 0                                                 NOT NULL,
    messages_failed_count    integer                             DEFAULT 0                                                 NOT NULL,
    error_message            text,
    retry_count              integer                             DEFAULT 0                                                 NOT NULL,
    created_at               timestamp with time zone            DEFAULT now()                                             NOT NULL,
    updated_at               timestamp with time zone            DEFAULT now()                                             NOT NULL
);


--
-- Name: contacts; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_contacts
(
    id                 uuid                                   NOT NULL,
    link_id            uuid                                   NOT NULL,
    email_address      character varying(320)                 NOT NULL,
    name               character varying(255),
    original_photo_url text,
    sfs_photo_url      text,
    created_at         timestamp with time zone DEFAULT now() NOT NULL,
    updated_at         timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: gmail_histories; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_gmail_histories
(
    link_id    uuid                                   NOT NULL,
    history_id text                                   NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: labels; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_labels
(
    id                      uuid                                                                                                   NOT NULL,
    link_id                 uuid                                                                                                   NOT NULL,
    provider_label_id       text                                                                                                   NOT NULL,
    name                    character varying(255)                                                                                 NOT NULL,
    created_at              timestamp with time zone                  DEFAULT now()                                                NOT NULL,
    message_list_visibility public.email_message_list_visibility_enum DEFAULT 'Show'::public.email_message_list_visibility_enum    NOT NULL,
    label_list_visibility   public.email_label_list_visibility_enum   DEFAULT 'LabelShow'::public.email_label_list_visibility_enum NOT NULL,
    type                    public.email_label_type_enum              DEFAULT 'User'::public.email_label_type_enum                 NOT NULL
);


--
-- Name: links; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_links
(
    id                 uuid                                   NOT NULL,
    macro_id           text                                   NOT NULL,
    fusionauth_user_id text                                   NOT NULL,
    email_address      character varying(320)                 NOT NULL,
    provider           public.email_user_provider_enum        NOT NULL,
    is_sync_active     boolean                  DEFAULT true  NOT NULL,
    created_at         timestamp with time zone DEFAULT now() NOT NULL,
    updated_at         timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: message_labels; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_message_labels
(
    message_id uuid NOT NULL,
    label_id   uuid NOT NULL
);


--
-- Name: message_recipients; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_message_recipients
(
    message_id     uuid                        NOT NULL,
    contact_id     uuid                        NOT NULL,
    recipient_type public.email_recipient_type NOT NULL
);


--
-- Name: messages; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_messages
(
    id                  uuid                                   NOT NULL,
    provider_id         text,
    thread_id           uuid                                   NOT NULL,
    provider_thread_id  text,
    link_id             uuid                                   NOT NULL,
    provider_history_id text,
    internal_date_ts    timestamp with time zone,
    snippet             text,
    size_estimate       bigint,
    subject             text,
    from_contact_id     uuid,
    sent_at             timestamp with time zone,
    has_attachments     boolean                  DEFAULT false NOT NULL,
    is_read             boolean                  DEFAULT false NOT NULL,
    is_starred          boolean                  DEFAULT false NOT NULL,
    is_sent             boolean                  DEFAULT false NOT NULL,
    is_draft            boolean                  DEFAULT false NOT NULL,
    body_text           text,
    body_html_sanitized text,
    body_macro          text,
    headers_jsonb       jsonb,
    created_at          timestamp with time zone DEFAULT now() NOT NULL,
    updated_at          timestamp with time zone DEFAULT now() NOT NULL,
    global_id           text,
    replying_to_id      uuid
);


--
-- Name: scheduled_messages; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_scheduled_messages
(
    link_id    uuid                                   NOT NULL,
    message_id uuid                                   NOT NULL,
    send_time  timestamp with time zone               NOT NULL,
    sent       boolean                  DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: sfs_mappings; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_sfs_mappings
(
    source      text                                   NOT NULL,
    destination text                                   NOT NULL,
    created_at  timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: sync_tokens; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_sync_tokens
(
    link_id                   uuid NOT NULL,
    contacts_sync_token       character varying(255),
    other_contacts_sync_token character varying(255)
);


--
-- Name: threads; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_threads
(
    id                         uuid                                   NOT NULL,
    provider_id                text,
    link_id                    uuid                                   NOT NULL,
    inbox_visible              boolean                  DEFAULT false NOT NULL,
    is_read                    boolean                  DEFAULT false NOT NULL,
    latest_inbound_message_ts  timestamp with time zone,
    latest_outbound_message_ts timestamp with time zone,
    latest_non_spam_message_ts timestamp with time zone,
    created_at                 timestamp with time zone DEFAULT now() NOT NULL,
    updated_at                 timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: user_history; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.email_user_history
(
    link_id    uuid                                   NOT NULL,
    thread_id  uuid                                   NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: attachments_macro attachments_macro_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_attachments_macro
    ADD CONSTRAINT email_attachments_macro_pkey PRIMARY KEY (id);


--
-- Name: attachments attachments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_attachments
    ADD CONSTRAINT email_attachments_pkey PRIMARY KEY (id);


--
-- Name: backfill_job backfill_job_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_backfill_jobs
    ADD CONSTRAINT email_backfill_job_pkey PRIMARY KEY (id);


--
-- Name: backfill_message backfill_message_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_backfill_messages
    ADD CONSTRAINT email_backfill_message_pkey PRIMARY KEY (backfill_job_id, thread_provider_id, message_provider_id);


--
-- Name: backfill_thread backfill_thread_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_backfill_threads
    ADD CONSTRAINT email_backfill_thread_pkey PRIMARY KEY (backfill_job_id, thread_provider_id);


--
-- Name: contacts contacts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_contacts
    ADD CONSTRAINT email_contacts_pkey PRIMARY KEY (id);


--
-- Name: gmail_histories gmail_histories_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_gmail_histories
    ADD CONSTRAINT email_gmail_histories_pkey PRIMARY KEY (link_id);


--
-- Name: labels labels_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_labels
    ADD CONSTRAINT email_labels_pkey PRIMARY KEY (id);


--
-- Name: links links_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_links
    ADD CONSTRAINT email_links_pkey PRIMARY KEY (id);


--
-- Name: message_labels message_labels_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_message_labels
    ADD CONSTRAINT email_message_labels_pkey PRIMARY KEY (message_id, label_id);


--
-- Name: message_recipients message_recipients_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_message_recipients
    ADD CONSTRAINT email_message_recipients_pkey PRIMARY KEY (message_id, contact_id, recipient_type);


--
-- Name: messages messages_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_messages
    ADD CONSTRAINT email_messages_pkey PRIMARY KEY (id);


--
-- Name: scheduled_messages scheduled_messages_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_scheduled_messages
    ADD CONSTRAINT email_scheduled_messages_pkey PRIMARY KEY (link_id, message_id);


--
-- Name: sfs_mappings sfs_mappings_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_sfs_mappings
    ADD CONSTRAINT email_sfs_mappings_pkey PRIMARY KEY (source);


--
-- Name: sync_tokens sync_tokens_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_sync_tokens
    ADD CONSTRAINT email_sync_tokens_pkey PRIMARY KEY (link_id);


--
-- Name: threads threads_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_threads
    ADD CONSTRAINT email_threads_pkey PRIMARY KEY (id);


--
-- Name: attachments_macro uq_attachments_macro_items; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_attachments_macro
    ADD CONSTRAINT email_uq_attachments_macro_items UNIQUE (message_id, item_id, item_type);


--
-- Name: attachments uq_attachments_message_id_provider_attachment_id; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_attachments
    ADD CONSTRAINT email_uq_attachments_message_id_provider_attachment_id UNIQUE (message_id, provider_attachment_id);


--
-- Name: labels uq_labels_link_id_provider_label_id; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_labels
    ADD CONSTRAINT email_uq_labels_link_id_provider_label_id UNIQUE (link_id, provider_label_id);


--
-- Name: links uq_links_user_email_provider; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_links
    ADD CONSTRAINT email_uq_links_user_email_provider UNIQUE (fusionauth_user_id, email_address, provider);


--
-- Name: user_history user_history_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_user_history
    ADD CONSTRAINT email_user_history_pkey PRIMARY KEY (link_id, thread_id);


--
-- Name: contacts_link_id_email_address_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX contacts_link_id_email_address_idx ON public.email_contacts USING btree (link_id, email_address);


--
-- Name: idx_attachments_macro_message_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_attachments_macro_message_id ON public.email_attachments_macro USING btree (message_id);


--
-- Name: idx_attachments_message_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_attachments_message_id ON public.email_attachments USING btree (message_id);


--
-- Name: idx_attachments_provider_attachment_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_attachments_provider_attachment_id ON public.email_attachments USING btree (provider_attachment_id);


--
-- Name: idx_labels_link_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_labels_link_id ON public.email_labels USING btree (link_id);


--
-- Name: idx_labels_link_id_for_trash_lookup; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_labels_link_id_for_trash_lookup ON public.email_labels USING btree (link_id) WHERE ((name)::text = 'TRASH'::text);


--
-- Name: idx_labels_link_id_name; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_labels_link_id_name ON public.email_labels USING btree (link_id, name);


--
-- Name: idx_labels_name; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_labels_name ON public.email_labels USING btree (name);


--
-- Name: idx_links_active_provider_hash_bucket; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_links_active_provider_hash_bucket ON public.email_links USING btree (provider, ((abs(hashtext((id)::text)) % 24))) WHERE (is_sync_active = true);


--
-- Name: idx_links_email_provider; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_links_email_provider ON public.email_links USING btree (lower((email_address)::text), provider);


--
-- Name: idx_links_fusionauth_user_active; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_links_fusionauth_user_active ON public.email_links USING btree (fusionauth_user_id, is_sync_active) WHERE (is_sync_active = true);


--
-- Name: idx_links_macro_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_links_macro_id ON public.email_links USING btree (macro_id);


--
-- Name: idx_links_provider_active; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_links_provider_active ON public.email_links USING btree (provider, is_sync_active) WHERE (is_sync_active = true);


--
-- Name: idx_message_labels_by_label; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_message_labels_by_label ON public.email_message_labels USING btree (label_id, message_id);


--
-- Name: idx_message_recipients_contact_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_message_recipients_contact_id ON public.email_message_recipients USING btree (contact_id);


--
-- Name: idx_messages_drafts_view; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_drafts_view ON public.email_messages USING btree (link_id, thread_id, internal_date_ts DESC) WHERE (is_draft = true);


--
-- Name: idx_messages_from_contact_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_from_contact_id ON public.email_messages USING btree (from_contact_id);


--
-- Name: idx_messages_link_id_global_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_link_id_global_id ON public.email_messages USING btree (link_id, global_id);


--
-- Name: idx_messages_link_id_replying_to_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_link_id_replying_to_id ON public.email_messages USING btree (link_id, replying_to_id);


--
-- Name: idx_messages_link_thread_date; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_link_thread_date ON public.email_messages USING btree (link_id, thread_id, internal_date_ts DESC);


--
-- Name: idx_messages_sent_view; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_sent_view ON public.email_messages USING btree (thread_id, internal_date_ts DESC) WHERE (is_sent = true);


--
-- Name: idx_messages_starred_view; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_starred_view ON public.email_messages USING btree (link_id, thread_id, internal_date_ts DESC) WHERE (is_starred = true);


--
-- Name: idx_messages_thread_id_internal_date_ts; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_messages_thread_id_internal_date_ts ON public.email_messages USING btree (thread_id, internal_date_ts DESC);


--
-- Name: idx_scheduled_messages_send_time_sent; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_scheduled_messages_send_time_sent ON public.email_scheduled_messages USING btree (send_time, sent);


--
-- Name: idx_sfs_mappings_destination; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_sfs_mappings_destination ON public.email_sfs_mappings USING btree (destination);


--
-- Name: idx_threads_inbox_view_not_null; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_threads_inbox_view_not_null ON public.email_threads USING btree (link_id, latest_inbound_message_ts DESC) WHERE ((inbox_visible = true) AND (latest_inbound_message_ts IS NOT NULL));


--
-- Name: idx_threads_non_spam_ts; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_threads_non_spam_ts ON public.email_threads USING btree (link_id, latest_non_spam_message_ts DESC) WHERE (latest_non_spam_message_ts IS NOT NULL);


--
-- Name: idx_threads_sent_view; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_email_threads_sent_view ON public.email_threads USING btree (link_id, latest_outbound_message_ts DESC) WHERE (latest_outbound_message_ts IS NOT NULL);


--
-- Name: uq_messages_link_id_provider_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX uq_messages_link_id_provider_id ON public.email_messages USING btree (link_id, provider_id) WHERE (provider_id IS NOT NULL);


--
-- Name: uq_threads_link_id_provider_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX uq_threads_link_id_provider_id ON public.email_threads USING btree (link_id, provider_id) WHERE (provider_id IS NOT NULL);


--
-- Name: attachments_macro attachments_macro_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_attachments_macro
    ADD CONSTRAINT email_attachments_macro_message_id_fkey FOREIGN KEY (message_id) REFERENCES public.email_messages (id) ON DELETE CASCADE;


--
-- Name: attachments attachments_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_attachments
    ADD CONSTRAINT email_attachments_message_id_fkey FOREIGN KEY (message_id) REFERENCES public.email_messages (id) ON DELETE CASCADE;


--
-- Name: backfill_job backfill_job_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_backfill_jobs
    ADD CONSTRAINT email_backfill_job_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE SET NULL;


--
-- Name: backfill_message backfill_message_backfill_job_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_backfill_messages
    ADD CONSTRAINT email_backfill_message_backfill_job_id_fkey FOREIGN KEY (backfill_job_id) REFERENCES public.email_backfill_jobs (id) ON DELETE CASCADE;


--
-- Name: backfill_thread backfill_thread_backfill_job_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_backfill_threads
    ADD CONSTRAINT email_backfill_thread_backfill_job_id_fkey FOREIGN KEY (backfill_job_id) REFERENCES public.email_backfill_jobs (id) ON DELETE CASCADE;


--
-- Name: contacts contacts_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_contacts
    ADD CONSTRAINT email_contacts_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: sync_tokens fk_sync_tokens_link_id; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_sync_tokens
    ADD CONSTRAINT email_fk_sync_tokens_link_id FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: gmail_histories gmail_histories_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_gmail_histories
    ADD CONSTRAINT email_gmail_histories_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: labels labels_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_labels
    ADD CONSTRAINT email_labels_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: message_labels message_labels_label_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_message_labels
    ADD CONSTRAINT email_message_labels_label_id_fkey FOREIGN KEY (label_id) REFERENCES public.email_labels (id) ON DELETE CASCADE;


--
-- Name: message_labels message_labels_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_message_labels
    ADD CONSTRAINT email_message_labels_message_id_fkey FOREIGN KEY (message_id) REFERENCES public.email_messages (id) ON DELETE CASCADE;


--
-- Name: message_recipients message_recipients_contact_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_message_recipients
    ADD CONSTRAINT email_message_recipients_contact_id_fkey FOREIGN KEY (contact_id) REFERENCES public.email_contacts (id) ON DELETE CASCADE;


--
-- Name: message_recipients message_recipients_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_message_recipients
    ADD CONSTRAINT email_message_recipients_message_id_fkey FOREIGN KEY (message_id) REFERENCES public.email_messages (id) ON DELETE CASCADE;


--
-- Name: messages messages_from_contact_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_messages
    ADD CONSTRAINT email_messages_from_contact_id_fkey FOREIGN KEY (from_contact_id) REFERENCES public.email_contacts (id) ON DELETE SET NULL;


--
-- Name: messages messages_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_messages
    ADD CONSTRAINT email_messages_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: messages messages_replying_to_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_messages
    ADD CONSTRAINT email_messages_replying_to_id_fkey FOREIGN KEY (replying_to_id) REFERENCES public.email_messages (id);


--
-- Name: messages messages_thread_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_messages
    ADD CONSTRAINT email_messages_thread_id_fkey FOREIGN KEY (thread_id) REFERENCES public.email_threads (id) ON DELETE CASCADE;


--
-- Name: scheduled_messages scheduled_messages_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_scheduled_messages
    ADD CONSTRAINT email_scheduled_messages_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: scheduled_messages scheduled_messages_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_scheduled_messages
    ADD CONSTRAINT email_scheduled_messages_message_id_fkey FOREIGN KEY (message_id) REFERENCES public.email_messages (id) ON DELETE CASCADE;


--
-- Name: threads threads_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_threads
    ADD CONSTRAINT email_threads_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: user_history user_history_link_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_user_history
    ADD CONSTRAINT email_user_history_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.email_links (id) ON DELETE CASCADE;


--
-- Name: user_history user_history_thread_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.email_user_history
    ADD CONSTRAINT email_user_history_thread_id_fkey FOREIGN KEY (thread_id) REFERENCES public.email_threads (id) ON DELETE CASCADE;


--
-- PostgreSQL database dump complete
--