Skip to content

FTS triggers missing for automatic full-text search #29

@dvdsgl

Description

@dvdsgl

FTS Triggers Missing for Automatic Full-Text Search

Issue Summary

The messages_fts table is created during database initialization (schema_sqlite.sql), but there are no SQLite triggers to automatically populate it when new messages are inserted.

Current Behavior

  • FTS table exists and is created properly
  • Messages are inserted via UpsertMessage() in internal/store/messages.go
  • FTS table remains empty after sync, requiring manual population
  • Bare word searches fail until FTS is manually populated

Expected Behavior

Full-text search should work automatically without requiring manual FTS population.

Root Cause

The UpsertMessage() function only inserts into the messages table but doesn't populate the corresponding messages_fts virtual table. SQLite FTS tables need to be kept in sync with the source tables via triggers or explicit INSERTs.

Proposed Fix

Add SQLite triggers to automatically populate messages_fts when messages are inserted/updated/deleted:

CREATE TRIGGER messages_fts_insert_trigger
AFTER INSERT ON messages
BEGIN
    INSERT INTO messages_fts(rowid, message_id, subject, body, from_addr, to_addr, cc_addr)
    SELECT NEW.id, NEW.id, NEW.subject, 
           COALESCE((SELECT body_text FROM message_bodies WHERE message_id = NEW.id), ''),
           COALESCE((SELECT p.email_address FROM message_recipients mr JOIN participants p ON p.id = mr.participant_id WHERE mr.message_id = NEW.id AND mr.recipient_type = 'from' LIMIT 1), ''),
           COALESCE((SELECT GROUP_CONCAT(p.email_address, ' ') FROM message_recipients mr JOIN participants p ON p.id = mr.participant_id WHERE mr.message_id = NEW.id AND mr.recipient_type = 'to'), ''),
           COALESCE((SELECT GROUP_CONCAT(p.email_address, ' ') FROM message_recipients mr JOIN participants p ON p.id = mr.participant_id WHERE mr.message_id = NEW.id AND mr.recipient_type = 'cc'), '');
END;

-- Similar UPDATE and DELETE triggers needed

This ensures full-text search works immediately for new messages without requiring manual intervention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions