fix(conversations): 修复会话标识与解决后跳转
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
DROP INDEX IF EXISTS idx_conversations_account_display_id;
|
||||
CREATE INDEX idx_conversations_account_display_id
|
||||
ON conversations(account_id, display_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
|
||||
-- Repaired display IDs are intentionally retained: their previous duplicate
|
||||
-- values cannot be restored without reintroducing ambiguous conversation URLs.
|
||||
@@ -0,0 +1,57 @@
|
||||
-- Repair duplicate and missing account-scoped conversation display IDs before
|
||||
-- enforcing the Chatwoot routing invariant. Keep already-unique IDs stable so
|
||||
-- existing conversation URLs continue to resolve to the same records.
|
||||
WITH ranked AS (
|
||||
SELECT
|
||||
id,
|
||||
account_id,
|
||||
display_id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY account_id, display_id
|
||||
ORDER BY id
|
||||
) AS duplicate_rank
|
||||
FROM conversations
|
||||
WHERE deleted_at IS NULL
|
||||
AND display_id IS NOT NULL
|
||||
AND display_id > 0
|
||||
),
|
||||
needs_repair AS (
|
||||
SELECT id, account_id
|
||||
FROM ranked
|
||||
WHERE duplicate_rank > 1
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT id, account_id
|
||||
FROM conversations
|
||||
WHERE deleted_at IS NULL
|
||||
AND (display_id IS NULL OR display_id = 0)
|
||||
),
|
||||
account_max AS (
|
||||
SELECT account_id, COALESCE(MAX(display_id), 0) AS max_display_id
|
||||
FROM conversations
|
||||
WHERE deleted_at IS NULL
|
||||
AND display_id IS NOT NULL
|
||||
AND display_id > 0
|
||||
GROUP BY account_id
|
||||
),
|
||||
replacements AS (
|
||||
SELECT
|
||||
repair.id,
|
||||
COALESCE(account_max.max_display_id, 0)
|
||||
+ ROW_NUMBER() OVER (
|
||||
PARTITION BY repair.account_id
|
||||
ORDER BY repair.id
|
||||
) AS display_id
|
||||
FROM needs_repair AS repair
|
||||
LEFT JOIN account_max ON account_max.account_id = repair.account_id
|
||||
)
|
||||
UPDATE conversations AS conversation
|
||||
SET display_id = replacements.display_id
|
||||
FROM replacements
|
||||
WHERE conversation.id = replacements.id;
|
||||
|
||||
DROP INDEX IF EXISTS idx_conversations_account_display_id;
|
||||
CREATE UNIQUE INDEX idx_conversations_account_display_id
|
||||
ON conversations(account_id, display_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
Reference in New Issue
Block a user