mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-15 12:14:17 +00:00
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:
1 parent
28aa667c77
commit
5d632a522e
3 files changed
+28
-1
No files matched your search
@@ -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, ''));
|
||||
Reference in new issue
Block a user