mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
Closes CODAGT-721 Closes CODAGT-722 Closes CODAGT-723 Closes CODAGT-724 Closes CODAGT-725 This PR adds the database and API pieces necessary to support full-text chat message search. - Adds required chat schema for full-text search - Adds dbpurge job to populate search_tsv in the background - Adds `search` parameter to GetChats query - Adds `search` filter to `searchquery.Chats` - Wires chat search filter into chats API > Implemented by Coder Agents, reviewed and tested by a human.
69 lines
1.9 KiB
PL/PgSQL
69 lines
1.9 KiB
PL/PgSQL
-- Restore the original trigger bodies from 000519.
|
|
CREATE OR REPLACE FUNCTION set_chat_message_revision_before()
|
|
RETURNS trigger AS $$
|
|
DECLARE
|
|
chat_snapshot_version bigint;
|
|
BEGIN
|
|
IF TG_OP = 'INSERT' AND NEW.revision IS NOT NULL THEN
|
|
RAISE EXCEPTION 'chat_messages.revision must be assigned by trigger';
|
|
END IF;
|
|
|
|
IF TG_OP = 'UPDATE' THEN
|
|
IF OLD.chat_id IS DISTINCT FROM NEW.chat_id THEN
|
|
RAISE EXCEPTION 'chat_messages.chat_id is immutable';
|
|
END IF;
|
|
|
|
IF OLD.revision IS DISTINCT FROM NEW.revision THEN
|
|
RAISE EXCEPTION 'chat_messages.revision must be assigned by trigger';
|
|
END IF;
|
|
|
|
IF OLD IS NOT DISTINCT FROM NEW THEN
|
|
RETURN NEW;
|
|
END IF;
|
|
END IF;
|
|
|
|
SELECT snapshot_version INTO chat_snapshot_version
|
|
FROM chats WHERE id = NEW.chat_id;
|
|
|
|
IF chat_snapshot_version IS NULL THEN
|
|
RAISE EXCEPTION 'chat % does not exist', NEW.chat_id;
|
|
END IF;
|
|
|
|
NEW.revision = chat_snapshot_version;
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE FUNCTION update_chat_history_after_message_update()
|
|
RETURNS trigger AS $$
|
|
BEGIN
|
|
UPDATE chats c
|
|
SET history_version = c.snapshot_version,
|
|
generation_attempt = 0
|
|
FROM (
|
|
SELECT DISTINCT n.chat_id
|
|
FROM chat_message_history_new_rows n
|
|
JOIN chat_message_history_old_rows o ON o.id = n.id
|
|
WHERE o IS DISTINCT FROM n
|
|
) AS affected
|
|
WHERE c.id = affected.chat_id
|
|
AND (
|
|
c.history_version IS DISTINCT FROM c.snapshot_version
|
|
OR c.generation_attempt <> 0
|
|
);
|
|
RETURN NULL;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
DROP INDEX IF EXISTS idx_chat_diff_statuses_pr_title_fts;
|
|
|
|
DROP INDEX IF EXISTS idx_chats_title_fts;
|
|
|
|
DROP INDEX IF EXISTS idx_chat_messages_search_tsv_pending;
|
|
|
|
DROP INDEX IF EXISTS idx_chat_messages_search_tsv;
|
|
|
|
ALTER TABLE chat_messages DROP COLUMN IF EXISTS search_tsv;
|
|
|
|
DROP FUNCTION IF EXISTS chat_message_search_text(jsonb);
|