feat(db): add migration 35 for contact thread_id columns

Add thread_id, thread_type columns to track forum topics and threaded conversations.
This commit is contained in:
viettranx committed 2026-04-02 08:22:53 +07:00
1 parent 28aa667c77
commit 5d632a522e
3 files changed
+28 -1

No files matched your search

+1 -1
View File
@@ -2,4 +2,4 @@ package upgrade
// RequiredSchemaVersion is the schema migration version this binary requires.
// Bump this whenever adding a new SQL migration file.
const RequiredSchemaVersion uint = 34
const RequiredSchemaVersion uint = 35
@@ -0,0 +1,5 @@
DROP INDEX IF EXISTS idx_channel_contacts_tenant_type_sender;
ALTER TABLE channel_contacts DROP COLUMN IF EXISTS thread_type;
ALTER TABLE channel_contacts DROP COLUMN IF EXISTS thread_id;
CREATE UNIQUE INDEX idx_channel_contacts_tenant_type_sender
ON channel_contacts (tenant_id, channel_type, sender_id);
@@ -0,0 +1,22 @@
ALTER TABLE channel_contacts ADD COLUMN thread_id VARCHAR(100);
ALTER TABLE channel_contacts ADD COLUMN thread_type VARCHAR(20);
-- Fix sender_id: strip "|username" suffix, keep only numeric ID.
-- Step 1: Delete "|username" rows where a numeric-only row already exists (avoid UNIQUE conflict).
DELETE FROM channel_contacts
WHERE sender_id LIKE '%|%'
AND EXISTS (
SELECT 1 FROM channel_contacts c2
WHERE c2.tenant_id = channel_contacts.tenant_id
AND c2.channel_type = channel_contacts.channel_type
AND c2.sender_id = split_part(channel_contacts.sender_id, '|', 1)
AND COALESCE(c2.thread_id, '') = COALESCE(channel_contacts.thread_id, '')
);
-- Step 2: Update remaining "|username" rows (no numeric counterpart) to strip suffix.
UPDATE channel_contacts
SET sender_id = split_part(sender_id, '|', 1)
WHERE sender_id LIKE '%|%';
DROP INDEX IF EXISTS idx_channel_contacts_tenant_type_sender;
CREATE UNIQUE INDEX idx_channel_contacts_tenant_type_sender
ON channel_contacts (tenant_id, channel_type, sender_id, COALESCE(thread_id, ''));