From 670000651d36a85ac7f8ff865d8f3cac148f66b0 Mon Sep 17 00:00:00 2001 From: viettranx Date: Thu, 2 Apr 2026 08:22:54 +0700 Subject: [PATCH] feat(sqlite): add thread_id columns to contacts schema Update SQLite schema to include thread_id and thread_type columns. --- internal/store/sqlitestore/schema.go | 8 +++++++- internal/store/sqlitestore/schema.sql | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/store/sqlitestore/schema.go b/internal/store/sqlitestore/schema.go index cf8fd1e1..26dcad74 100644 --- a/internal/store/sqlitestore/schema.go +++ b/internal/store/sqlitestore/schema.go @@ -14,7 +14,7 @@ var schemaSQL string // SchemaVersion is the current SQLite schema version. // Bump this when adding new migration steps below. -const SchemaVersion = 4 +const SchemaVersion = 5 // migrations maps version → SQL to apply when upgrading FROM that version. // schema.sql always represents the LATEST full schema (for fresh DBs). @@ -42,6 +42,12 @@ UPDATE cron_jobs SET deliver_to = COALESCE(json_extract(payload, '$.to'), ''), wake_heartbeat = COALESCE(json_extract(payload, '$.wake_heartbeat'), 0) WHERE payload IS NOT NULL;`, + // Version 4 → 5: add thread_id, thread_type columns to channel_contacts for forum topic support. + 4: `ALTER TABLE channel_contacts ADD COLUMN thread_id VARCHAR(100); +ALTER TABLE channel_contacts ADD COLUMN thread_type VARCHAR(20); +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, ''));`, // Version 3 → 4: add subagent_tasks table for subagent lifecycle persistence. 3: `CREATE TABLE IF NOT EXISTS subagent_tasks ( id TEXT PRIMARY KEY, diff --git a/internal/store/sqlitestore/schema.sql b/internal/store/sqlitestore/schema.sql index 69463644..a869974a 100644 --- a/internal/store/sqlitestore/schema.sql +++ b/internal/store/sqlitestore/schema.sql @@ -1042,6 +1042,8 @@ CREATE TABLE IF NOT EXISTS channel_contacts ( avatar_url TEXT, peer_kind VARCHAR(20), contact_type VARCHAR(20) NOT NULL DEFAULT 'user', + thread_id VARCHAR(100), + thread_type VARCHAR(20), metadata TEXT DEFAULT '{}', merged_id TEXT, tenant_id TEXT NOT NULL REFERENCES tenants(id), @@ -1049,8 +1051,8 @@ CREATE TABLE IF NOT EXISTS channel_contacts ( last_seen_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) ); --- tenant-scoped unique (migration 27 Phase I) -CREATE UNIQUE INDEX IF NOT EXISTS idx_channel_contacts_tenant_type_sender ON channel_contacts(tenant_id, channel_type, sender_id); +-- tenant-scoped unique including thread_id for topic contacts (migration 35) +CREATE UNIQUE INDEX IF NOT EXISTS idx_channel_contacts_tenant_type_sender ON channel_contacts(tenant_id, channel_type, sender_id, COALESCE(thread_id, '')); CREATE INDEX IF NOT EXISTS idx_channel_contacts_instance ON channel_contacts(channel_instance) WHERE channel_instance IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_channel_contacts_merged ON channel_contacts(merged_id) WHERE merged_id IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_channel_contacts_search ON channel_contacts(display_name, username);