From 1f7386d7cf68f194921926ac384c214f853f38cf Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 25 Aug 2023 09:29:42 -0700 Subject: [PATCH] Improve performance of Delete All Data --- ts/sql/Server.ts | 18 ++++++++++++++++++ ts/sql/migrations/45-stories.ts | 2 ++ 2 files changed, 20 insertions(+) diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index fddd225d9d..fd414de34f 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -5515,6 +5515,9 @@ async function removeAll(): Promise { db.transaction(() => { db.exec(` + --- Remove messages delete trigger for performance + DROP TRIGGER messages_on_delete; + DELETE FROM attachment_downloads; DELETE FROM badgeImageFiles; DELETE FROM badges; @@ -5546,6 +5549,21 @@ async function removeAll(): Promise { DELETE FROM uninstalled_sticker_packs; INSERT INTO messages_fts(messages_fts) VALUES('optimize'); + + --- Re-create the messages delete trigger + --- See migration 45 + CREATE TRIGGER messages_on_delete AFTER DELETE ON messages BEGIN + DELETE FROM messages_fts WHERE rowid = old.rowid; + DELETE FROM sendLogPayloads WHERE id IN ( + SELECT payloadId FROM sendLogMessageIds + WHERE messageId = old.id + ); + DELETE FROM reactions WHERE rowid IN ( + SELECT rowid FROM reactions + WHERE messageId = old.id + ); + DELETE FROM storyReads WHERE storyId = old.storyId; + END; `); })(); } diff --git a/ts/sql/migrations/45-stories.ts b/ts/sql/migrations/45-stories.ts index 3eeb84de85..0bc0ec16fd 100644 --- a/ts/sql/migrations/45-stories.ts +++ b/ts/sql/migrations/45-stories.ts @@ -75,6 +75,8 @@ export default function updateToSchemaVersion45( --- Update delete trigger to remove storyReads + --- Note: for future updates to this trigger, be sure to update Server.ts/removeAll() + --- (it deletes and re-adds this trigger for performance) DROP TRIGGER messages_on_delete; CREATE TRIGGER messages_on_delete AFTER DELETE ON messages BEGIN DELETE FROM messages_fts WHERE rowid = old.rowid;