fix: address message audit review findings

This commit is contained in:
姚章浩
2026-07-21 10:52:59 +08:00
parent d857853ea3
commit 32b76b84cb
4 changed files with 124 additions and 16 deletions
+1 -7
View File
@@ -17,9 +17,6 @@ CREATE TABLE IF NOT EXISTS channel_actors (
UNIQUE (tenant_key, channel, external_actor_id)
);
CREATE INDEX IF NOT EXISTS idx_channel_actors_lookup
ON channel_actors (tenant_key, channel, external_actor_id);
-- ============================================================
-- 2. Channel Conversations
-- ============================================================
@@ -36,9 +33,6 @@ CREATE TABLE IF NOT EXISTS channel_conversations (
UNIQUE (tenant_key, channel, external_conversation_id)
);
CREATE INDEX IF NOT EXISTS idx_channel_conversations_lookup
ON channel_conversations (tenant_key, channel, external_conversation_id);
-- ============================================================
-- 3. Messages
-- ============================================================
@@ -75,7 +69,7 @@ CREATE INDEX IF NOT EXISTS idx_messages_tenant_conv_created
-- Index for correlation lookups
CREATE INDEX IF NOT EXISTS idx_messages_correlation
ON messages (correlation_id);
ON messages (tenant_key, correlation_id);
-- ============================================================
-- 4. Extend turns table
@@ -197,15 +197,22 @@ func TestUp003MessagesPartialUniqueIndex(t *testing.T) {
// TestUp003MessagesIndexes checks required indexes on messages.
func TestUp003MessagesIndexes(t *testing.T) {
up := read003UpMigration(t)
upper := strings.ToUpper(up)
// Tenant + conversation + created_at
if !strings.Contains(upper, "TENANT_KEY") || !strings.Contains(upper, "CONVERSATION_ID") || !strings.Contains(upper, "CREATED_AT") {
t.Error("messages must have index on tenant_key, conversation_id, created_at")
conversationIndex := regexp.MustCompile(`(?is)CREATE\s+INDEX\s+IF\s+NOT\s+EXISTS\s+idx_messages_tenant_conv_created\s+ON\s+messages\s*\(\s*tenant_key\s*,\s*conversation_id\s*,\s*created_at\s+DESC\s*\)`)
if !conversationIndex.MatchString(up) {
t.Error("messages must have index on tenant_key, conversation_id, created_at DESC")
}
// correlation_id index
if !strings.Contains(upper, "CORRELATION_ID") {
t.Error("messages must have index on correlation_id")
correlationIndex := regexp.MustCompile(`(?is)CREATE\s+INDEX\s+IF\s+NOT\s+EXISTS\s+idx_messages_correlation\s+ON\s+messages\s*\(\s*tenant_key\s*,\s*correlation_id\s*\)`)
if !correlationIndex.MatchString(up) {
t.Error("messages correlation index must be tenant-scoped: (tenant_key, correlation_id)")
}
}
func TestUp003AvoidsRedundantLookupIndexes(t *testing.T) {
up := strings.ToLower(read003UpMigration(t))
for _, redundant := range []string{"idx_channel_actors_lookup", "idx_channel_conversations_lookup"} {
if strings.Contains(up, redundant) {
t.Errorf("migration must not create redundant index %s; UNIQUE already provides it", redundant)
}
}
}