bmc_hub/migrations/156_backfill_email_thread_keys.sql
Christian 30d1be61eb feat: Add global search functionality and email results section
- Introduced a global search button and modal for enhanced user experience.
- Added a new section for displaying email results in the global search modal.
- Implemented functionality to fetch and display emails based on user queries.
- Updated the UI to include a reminders button and improved accessibility features.

fix: Update docker-compose to allow reload configuration

- Changed ENABLE_RELOAD environment variable to default to true for easier development.

chore: Update requirements for new dependencies

- Added brother_ql, pyzbar, and pypdfium2 to requirements for label printing and PDF processing.

feat: Implement Brother label printing service

- Created a new service for printing labels using Brother QL printers.
- Supports direct printing of case hardware labels with customizable layouts.

feat: Add Vaultwarden service for credential management

- Implemented a service to interact with Vaultwarden for secure credential storage and retrieval.

sql: Add migrations for email thread keys and document tokens

- Created migrations to backfill email thread keys and manage document tokens for work orders.
- Introduced new tables and updated existing structures to support token-based linking of scanned documents.

sql: Import links into the database

- Added a script to import a predefined set of links into the database with associated categories.
2026-04-01 21:34:58 +02:00

49 lines
1.9 KiB
SQL

-- Migration 156: Backfill email thread_keys from parent emails
-- Ensures replies inherit the same thread_key as their parent so they group together visually.
-- Step 1: For emails that have in_reply_to or email_references pointing to an existing
-- email with a thread_key, adopt the parent's thread_key.
UPDATE email_messages child
SET thread_key = parent.thread_key,
updated_at = CURRENT_TIMESTAMP
FROM email_messages parent
WHERE child.deleted_at IS NULL
AND parent.deleted_at IS NULL
AND parent.thread_key IS NOT NULL
AND TRIM(parent.thread_key) != ''
AND (
-- Match via in_reply_to -> parent message_id
(
child.in_reply_to IS NOT NULL
AND TRIM(child.in_reply_to) != ''
AND LOWER(REGEXP_REPLACE(parent.message_id, '[<>\s]', '', 'g'))
= LOWER(REGEXP_REPLACE(
(REGEXP_SPLIT_TO_ARRAY(TRIM(child.in_reply_to), E'[\\s,]+'))[1],
'[<>\s]', '', 'g'
))
)
OR
-- Match via first reference -> parent message_id
(
child.email_references IS NOT NULL
AND TRIM(child.email_references) != ''
AND LOWER(REGEXP_REPLACE(parent.message_id, '[<>\s]', '', 'g'))
= LOWER(REGEXP_REPLACE(
(REGEXP_SPLIT_TO_ARRAY(TRIM(child.email_references), E'[\\s,]+'))[1],
'[<>\s]', '', 'g'
))
)
)
-- Only update if the thread_key would actually change
AND (
child.thread_key IS NULL
OR TRIM(child.thread_key) = ''
OR LOWER(REGEXP_REPLACE(child.thread_key, '[<>\s]', '', 'g'))
!= LOWER(REGEXP_REPLACE(parent.thread_key, '[<>\s]', '', 'g'))
);
-- Step 2: REMOVED - was incorrectly forcing all emails in a SAG to share one thread_key.
-- Each SAG can have multiple independent email threads (different recipients/subjects).
-- Thread grouping is based on actual RFC 5322 threading headers, not SAG membership.
-- See migration 157 for the fix.