add email threads to companies
This commit is contained in:
20
src/db/migrations/0008_add_company_id_to_emails.sql
Normal file
20
src/db/migrations/0008_add_company_id_to_emails.sql
Normal file
@@ -0,0 +1,20 @@
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'emails' AND column_name = 'company_id'
|
||||
) THEN
|
||||
ALTER TABLE emails
|
||||
ADD COLUMN company_id UUID REFERENCES companies(id) ON DELETE SET NULL;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_emails_company_id ON emails(company_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_emails_company_thread ON emails(company_id, thread_id);
|
||||
|
||||
UPDATE emails e
|
||||
SET company_id = c.company_id
|
||||
FROM contacts c
|
||||
WHERE e.contact_id = c.id
|
||||
AND c.company_id IS NOT NULL
|
||||
AND (e.company_id IS NULL OR e.company_id <> c.company_id);
|
||||
@@ -85,6 +85,7 @@ export const emails = pgTable('emails', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
emailAccountId: uuid('email_account_id').references(() => emailAccounts.id, { onDelete: 'cascade' }).notNull(),
|
||||
contactId: uuid('contact_id').references(() => contacts.id, { onDelete: 'cascade' }),
|
||||
companyId: uuid('company_id').references(() => companies.id, { onDelete: 'set null' }),
|
||||
jmapId: text('jmap_id').unique(),
|
||||
messageId: text('message_id').unique(),
|
||||
threadId: text('thread_id'),
|
||||
|
||||
Reference in New Issue
Block a user