-- -- Tables -- This file declares all tables in the public schema. -- -- Extensions create extension if not exists "http" with schema "extensions"; create extension if not exists "citext" with schema "extensions"; -- Private schema (used by sales policies migration) create schema if not exists "private"; create table public.companies ( id bigint generated by default as identity primary key, created_at timestamp with time zone not null default now(), name text not null, sector text, size smallint, linkedin_url text, website extensions.citext, phone_number text, address text, zipcode text, city text, state_abbr text, sales_id bigint, context_links json, country text, description text, revenue text, tax_identifier text, logo jsonb ); create table public.contacts ( id bigint generated by default as identity primary key, first_name text, last_name text, gender text, title text, background text, avatar jsonb, first_seen timestamp with time zone, last_seen timestamp with time zone, has_newsletter boolean, status text, tags bigint[], company_id bigint, sales_id bigint, linkedin_url text, email_jsonb jsonb, phone_jsonb jsonb ); create table public.contact_notes ( id bigint generated by default as identity (sequence name public."contactNotes_id_seq") not null, contact_id bigint not null, text text, date timestamp with time zone default now(), sales_id bigint, status text, attachments jsonb[] ); create table public.deals ( id bigint generated by default as identity primary key, name text not null, company_id bigint, contact_ids bigint[], category text, stage text not null, description text, amount bigint, created_at timestamp with time zone not null default now(), updated_at timestamp with time zone not null default now(), archived_at timestamp with time zone, expected_closing_date date, sales_id bigint, index smallint ); create table public.deal_notes ( id bigint generated by default as identity (sequence name public."dealNotes_id_seq") not null, deal_id bigint not null, type text, text text, date timestamp with time zone default now(), sales_id bigint, attachments jsonb[] ); create table public.sales ( id bigint generated by default as identity primary key, first_name text not null default 'Pending'::text, last_name text not null default 'Pending'::text, email extensions.citext not null, administrator boolean not null, user_id uuid not null, avatar jsonb, disabled boolean not null default false, secondary_emails jsonb not null default '[]'::jsonb, constraint sales_secondary_emails_is_array check (jsonb_typeof(secondary_emails) = 'array') ); create unique index uq__sales__user_id on public.sales using btree (user_id); create table public.tags ( id bigint generated by default as identity primary key, name text not null, color text not null ); create table public.tasks ( id bigint generated by default as identity primary key, contact_id bigint not null, type text, text text, due_date timestamp with time zone, done_date timestamp with time zone, sales_id bigint ); create table public.configuration ( id integer not null default 1 primary key, config jsonb not null default '{}'::jsonb, constraint configuration_singleton check (id = 1) ); create table public.favicons_excluded_domains ( id bigint generated by default as identity primary key, domain text not null ); -- -- Foreign keys -- alter table public.companies add constraint companies_sales_id_fkey foreign key (sales_id) references public.sales(id); alter table public.contacts add constraint contacts_company_id_fkey foreign key (company_id) references public.companies(id) on update cascade on delete cascade; alter table public.contacts add constraint contacts_sales_id_fkey foreign key (sales_id) references public.sales(id); alter table public.contact_notes add constraint "contactNotes_contact_id_fkey" foreign key (contact_id) references public.contacts(id) on update cascade on delete cascade; alter table public.contact_notes add constraint "contactNotes_sales_id_fkey" foreign key (sales_id) references public.sales(id) on update cascade on delete cascade; alter table public.deals add constraint deals_company_id_fkey foreign key (company_id) references public.companies(id) on update cascade on delete cascade; alter table public.deals add constraint deals_sales_id_fkey foreign key (sales_id) references public.sales(id); alter table public.deal_notes add constraint "dealNotes_deal_id_fkey" foreign key (deal_id) references public.deals(id) on update cascade on delete cascade; alter table public.deal_notes add constraint "dealNotes_sales_id_fkey" foreign key (sales_id) references public.sales(id); alter table public.sales add constraint sales_user_id_fkey foreign key (user_id) references auth.users(id); alter table public.tasks add constraint tasks_contact_id_fkey foreign key (contact_id) references public.contacts(id) on update cascade on delete cascade; -- Legacy primary key constraint names (from before snake_case rename) alter table only public.contact_notes add constraint "contactNotes_pkey" primary key (id); alter table only public.deal_notes add constraint "dealNotes_pkey" primary key (id); -- -- Indexes on foreign keys -- create index contact_notes_contact_id_idx on public.contact_notes using btree (contact_id); create index contacts_company_id_idx on public.contacts using btree (company_id); create index deal_notes_deal_id_idx on public.deal_notes using btree (deal_id); create index deals_company_id_idx on public.deals using btree (company_id);