From a76534ed458bd85391d1321c8a33eb0acd35b309 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Wed, 28 Apr 2021 15:22:33 -0700 Subject: [PATCH] Fix FTS trigger for messages with NULL body --- ts/sql/Server.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index 53cb11a58cb..9902706c664 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -1649,6 +1649,43 @@ async function updateToSchemaVersion26(currentVersion: number, db: Database) { })(); } +async function updateToSchemaVersion27(currentVersion: number, db: Database) { + if (currentVersion >= 27) { + return; + } + + db.transaction(() => { + db.exec(` + DELETE FROM messages_fts WHERE rowid IN + (SELECT rowid FROM messages WHERE body IS NULL); + + DROP TRIGGER messages_on_update; + + CREATE TRIGGER messages_on_update AFTER UPDATE ON messages + WHEN + new.body IS NULL OR + ((old.body IS NULL OR new.body != old.body) AND + (new.isViewOnce IS NULL OR new.isViewOnce != 1)) + BEGIN + DELETE FROM messages_fts WHERE rowid = old.rowid; + INSERT INTO messages_fts + (rowid, body) + VALUES + (new.rowid, new.body); + END; + + CREATE TRIGGER messages_on_view_once_update AFTER UPDATE ON messages + WHEN + new.body IS NOT NULL AND new.isViewOnce = 1 + BEGIN + DELETE FROM messages_fts WHERE rowid = old.rowid; + END; + `); + + db.pragma('user_version = 27'); + })(); +} + const SCHEMA_VERSIONS = [ updateToSchemaVersion1, updateToSchemaVersion2, @@ -1676,6 +1713,7 @@ const SCHEMA_VERSIONS = [ updateToSchemaVersion24, updateToSchemaVersion25, updateToSchemaVersion26, + updateToSchemaVersion27, ]; function updateSchema(db: Database): void {