fix(vault): expand legacy-backfill regex to cover all channel integrations

Extend migration 000056 backfill-2 vocabulary from {telegram, ws, delegate}
to the full channel set the gateway supports: discord, zalo, feishu, lark,
whatsapp, slack, line, messenger, wechat, viber (plus api transport).

Verified against staging data: identical chat_id extraction (91 rows,
9 distinct chats) — no regression. Now safe for tenants using any
non-Telegram integration.
This commit is contained in:
viettranx committed 2026-04-23 19:05:25 +07:00
1 parent 9625a20e8b
commit 2979f37050
1 file changed
+30 -25
+30 -25
View File
@@ -27,40 +27,45 @@ WHERE vd.team_id = t.id
-- in isolated-team search because the `searchChatFilter` predicate cannot
-- distinguish them.
--
-- Path layouts handled (ordered most-specific → most-general in COALESCE):
-- telegram/group_telegram_<chat>/... (nested legacy)
-- <agent_key>/telegram/group_telegram_<chat>/. (agent-owned nested)
-- group_telegram_<chat>/... (bare legacy)
-- /telegram/<chat>/... or leading telegram/<chat>/...
-- tenants/<slug>/ws/<chat>/... (non-master tenant WS)
-- ws/<chat>/... or <agent_key>/ws/<chat>/... (WS direct)
-- <agent_key>/delegate/<chat>/... (delegated task)
-- <agent_key>/<botname>/group_<botname>_<chat>/... (legacy bot channel)
-- <agent_key>/<botname>/<chat>/... (bot + numeric/ws chat)
-- Subsystem vocabulary — any channel integration or delivery surface the
-- gateway writes under. Must match the channel names used as path segments
-- in internal/channels/* and workspace resolver (v2 + v3 layouts).
-- Channels: telegram | discord | zalo | feishu | lark | whatsapp | slack |
-- line | messenger | wechat | viber
-- Transports: ws (browser / WS direct) | api (HTTP) | delegate (subagent)
--
-- Chat IDs can be numeric (Telegram), `system`, user handles, etc.
-- Only populate when chat_id IS NULL so interceptor-stamped values are preserved.
-- Path layouts handled (in order of COALESCE priority):
-- <subsystem>/group_<anything>_<chat>/... (Telegram-style group prefix)
-- <subsystem>/<chat>/... (bare legacy)
-- <agent_key>/<subsystem>/group_<anything>_<chat>/... (agent-owned group)
-- <agent_key>/<subsystem>/<chat>/... (agent-owned direct)
-- tenants/<slug>/<subsystem>/<chat>/... (non-master tenant)
-- <agent_key>/<botname>/group_<botname>_<chat>/... (legacy bot channel)
-- <agent_key>/<botname>/<chat>/... (bot + numeric/ws chat)
--
-- Chat IDs: numeric (Telegram/Discord/Zalo), oc_xxx (Feishu/Lark), sanitized
-- JID (WhatsApp: "123_c_us"), `system`, user handles, UUIDs. Sanitizer
-- (workspace_resolver.go) replaces everything outside [a-zA-Z0-9_-] with `_`,
-- so the captured character class matches what's actually on disk.
--
-- Only populate when chat_id IS NULL so interceptor-stamped values survive.
-- -----------------------------------------------------------------------------
UPDATE vault_documents
SET chat_id = COALESCE(
(regexp_match(path, '^telegram/group_telegram_(-?[0-9]+)/'))[1],
(regexp_match(path, '/telegram/group_telegram_(-?[0-9]+)/'))[1],
(regexp_match(path, '^group_telegram_(-?[0-9]+)/'))[1],
(regexp_match(path, '/group_telegram_(-?[0-9]+)/'))[1],
(regexp_match(path, '^telegram/(-?[0-9a-zA-Z_-]+)/'))[1],
(regexp_match(path, '/telegram/([a-zA-Z0-9_-]+)/'))[1],
(regexp_match(path, '^tenants/[^/]+/ws/([^/]+)/'))[1],
(regexp_match(path, '^ws/([^/]+)/'))[1],
(regexp_match(path, '^[^/]+/ws/([^/]+)/'))[1],
(regexp_match(path, '^[^/]+/delegate/([^/]+)/'))[1],
(regexp_match(path, '^(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber)/group_[^/]+_(-?[0-9]+)/'))[1],
(regexp_match(path, '^(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber|ws|delegate|api)/([a-zA-Z0-9_-]+)/'))[1],
(regexp_match(path, '^[^/]+/(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber)/group_[^/]+_(-?[0-9]+)/'))[1],
(regexp_match(path, '^[^/]+/(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber|ws|delegate|api)/([a-zA-Z0-9_-]+)/'))[1],
(regexp_match(path, '^tenants/[^/]+/(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber|ws|delegate|api)/([a-zA-Z0-9_-]+)/'))[1],
(regexp_match(path, '^group_[^/]+_(-?[0-9]+)/'))[1],
(regexp_match(path, '^[^/]+/[^/]+/group_[^/]+_(-?[0-9]+)/'))[1],
(regexp_match(path, '^[^/]+/[^/]+/([a-zA-Z0-9_-]+)/'))[1]
)
WHERE chat_id IS NULL
AND team_id IS NULL
AND (
path ~ '(^|/)(group_telegram_|telegram/)'
OR path ~ '^(([^/]+/)?(tenants/[^/]+/)?)?ws/[^/]+/'
OR path ~ '^[^/]+/delegate/[^/]+/'
path ~ '^(?:[^/]+/)?(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber|ws|delegate|api)/[^/]+/'
OR path ~ '^tenants/[^/]+/(?:telegram|discord|zalo|feishu|lark|whatsapp|slack|line|messenger|wechat|viber|ws|delegate|api)/[^/]+/'
OR path ~ '^group_[^/]+_-?[0-9]+/'
OR path ~ '^[^/]+/[^/]+/(group_[^/]+_-?[0-9]+|[0-9]+)/'
);