diff --git a/libs/migrate/m20250628_000080_add_message_reactions_and_search.rs b/libs/migrate/m20250628_000080_add_message_reactions_and_search.rs index cae6b57..a0ab892 100644 --- a/libs/migrate/m20250628_000080_add_message_reactions_and_search.rs +++ b/libs/migrate/m20250628_000080_add_message_reactions_and_search.rs @@ -19,9 +19,7 @@ impl MigrationTrait for Migration { ) .await?; - let sql = "DROP TRIGGER IF EXISTS room_message_tsv_update ON room_message; - DROP FUNCTION IF EXISTS room_message_tsv_trigger; - DROP INDEX IF EXISTS idx_room_message_content_tsv; + let sql = "DROP INDEX IF EXISTS idx_room_message_content_tsv; ALTER TABLE room_message DROP COLUMN IF EXISTS content_tsv;"; super::execute_sql(manager, sql).await } diff --git a/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql b/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql index f65db89..0096d9f 100644 --- a/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql +++ b/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql @@ -15,34 +15,9 @@ CREATE INDEX IF NOT EXISTS idx_room_message_reaction_message ON room_message_rea CREATE INDEX IF NOT EXISTS idx_room_message_reaction_user ON room_message_reaction ("user"); CREATE INDEX IF NOT EXISTS idx_room_message_reaction_room ON room_message_reaction (room); --- Add full-text search index for message content (PostgreSQL tsvector) +-- Add full-text search column for message content ALTER TABLE room_message ADD COLUMN IF NOT EXISTS content_tsv TSVECTOR; --- Update existing messages with tsvector -UPDATE room_message -SET content_tsv = to_tsvector('english', content) -WHERE content_tsv IS NULL; - -- Create index for full-text search CREATE INDEX IF NOT EXISTS idx_room_message_content_tsv ON room_message USING GIN (content_tsv); - --- Trigger to auto-update content_tsv on insert/update -CREATE OR REPLACE FUNCTION room_message_tsv_trigger() RETURNS TRIGGER AS -$$ -BEGIN - NEW.content_tsv := to_tsvector('simple', COALESCE(NEW.content, '')); - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE TRIGGER room_message_tsv_update - BEFORE INSERT OR UPDATE - ON room_message - FOR EACH ROW - EXECUTE FUNCTION room_message_tsv_trigger(); - --- Backfill existing rows -UPDATE room_message -SET content_tsv = to_tsvector('simple', content) -WHERE content_tsv IS NULL;