From 4c3a9e8cc14b3ee638037a731847e4058b6cc599 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Tue, 14 Mar 2023 10:44:02 +1100 Subject: [PATCH] fix FTS table and global search --- Session.xcodeproj/project.pbxproj | 4 +++ SessionMessagingKit/Configuration.swift | 3 +- .../_011_AddPendingReadReceipts.swift | 7 ----- .../Migrations/_012_AddFTSIfNeeded.swift | 30 +++++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 SessionMessagingKit/Database/Migrations/_012_AddFTSIfNeeded.swift diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 32bd2af3c..ff99cdb97 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -113,6 +113,7 @@ 7B4C75CB26B37E0F0000AC89 /* UnsendRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */; }; 7B4C75CD26BB92060000AC89 /* DeletedMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */; }; 7B50D64D28AC7CF80086CCEC /* silence.aiff in Resources */ = {isa = PBXBuildFile; fileRef = 7B50D64C28AC7CF80086CCEC /* silence.aiff */; }; + 7B521E0829BFEAFF00C3C36A /* _012_AddFTSIfNeeded.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B521E0729BFEAFF00C3C36A /* _012_AddFTSIfNeeded.swift */; }; 7B7037432834B81F000DCF35 /* ReactionContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7037422834B81F000DCF35 /* ReactionContainerView.swift */; }; 7B7037452834BCC0000DCF35 /* ReactionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7037442834BCC0000DCF35 /* ReactionView.swift */; }; 7B7CB18E270D066F0079FF93 /* IncomingCallBanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7CB18D270D066F0079FF93 /* IncomingCallBanner.swift */; }; @@ -1181,6 +1182,7 @@ 7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnsendRequest.swift; sourceTree = ""; }; 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletedMessageView.swift; sourceTree = ""; }; 7B50D64C28AC7CF80086CCEC /* silence.aiff */ = {isa = PBXFileReference; lastKnownFileType = audio.aiff; path = silence.aiff; sourceTree = ""; }; + 7B521E0729BFEAFF00C3C36A /* _012_AddFTSIfNeeded.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = _012_AddFTSIfNeeded.swift; sourceTree = ""; }; 7B7037422834B81F000DCF35 /* ReactionContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionContainerView.swift; sourceTree = ""; }; 7B7037442834BCC0000DCF35 /* ReactionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionView.swift; sourceTree = ""; }; 7B7CB18D270D066F0079FF93 /* IncomingCallBanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IncomingCallBanner.swift; sourceTree = ""; }; @@ -3547,6 +3549,7 @@ 7BAA7B6528D2DE4700AE1489 /* _009_OpenGroupPermission.swift */, FD7115F128C6CB3900B47552 /* _010_AddThreadIdToFTS.swift */, FD432431299C6933008A0213 /* _011_AddPendingReadReceipts.swift */, + 7B521E0729BFEAFF00C3C36A /* _012_AddFTSIfNeeded.swift */, ); path = Migrations; sourceTree = ""; @@ -5432,6 +5435,7 @@ FD09797527FAB64300936362 /* ProfileManager.swift in Sources */, FD245C57285065F100B966DD /* Poller.swift in Sources */, FDA8EAFE280E8B78002B68E5 /* FailedMessageSendsJob.swift in Sources */, + 7B521E0829BFEAFF00C3C36A /* _012_AddFTSIfNeeded.swift in Sources */, FD245C6A2850666F00B966DD /* FileServerAPI.swift in Sources */, FDC4386927B4E6B800C60D73 /* String+Utlities.swift in Sources */, FD716E6628502EE200C96BF4 /* CurrentCallProtocol.swift in Sources */, diff --git a/SessionMessagingKit/Configuration.swift b/SessionMessagingKit/Configuration.swift index 27b375f99..b50ac3b94 100644 --- a/SessionMessagingKit/Configuration.swift +++ b/SessionMessagingKit/Configuration.swift @@ -25,7 +25,8 @@ public enum SNMessagingKit { // Just to make the external API nice _008_EmojiReacts.self, _009_OpenGroupPermission.self, _010_AddThreadIdToFTS.self, - _011_AddPendingReadReceipts.self + _011_AddPendingReadReceipts.self, + _012_AddFTSIfNeeded.self ] ] ) diff --git a/SessionMessagingKit/Database/Migrations/_011_AddPendingReadReceipts.swift b/SessionMessagingKit/Database/Migrations/_011_AddPendingReadReceipts.swift index 69e231b49..9c2e228d5 100644 --- a/SessionMessagingKit/Database/Migrations/_011_AddPendingReadReceipts.swift +++ b/SessionMessagingKit/Database/Migrations/_011_AddPendingReadReceipts.swift @@ -13,13 +13,6 @@ enum _011_AddPendingReadReceipts: Migration { static let minExpectedRunDuration: TimeInterval = 0.1 static func migrate(_ db: Database) throws { - // Can't actually alter a virtual table in SQLite so we need to drop and recreate it, - // luckily this is actually pretty quick - if try db.tableExists(Interaction.fullTextSearchTableName) { - try db.drop(table: Interaction.fullTextSearchTableName) - try db.dropFTS5SynchronizationTriggers(forTable: Interaction.fullTextSearchTableName) - } - try db.create(table: PendingReadReceipt.self) { t in t.column(.threadId, .text) .notNull() diff --git a/SessionMessagingKit/Database/Migrations/_012_AddFTSIfNeeded.swift b/SessionMessagingKit/Database/Migrations/_012_AddFTSIfNeeded.swift new file mode 100644 index 000000000..6b2fe3390 --- /dev/null +++ b/SessionMessagingKit/Database/Migrations/_012_AddFTSIfNeeded.swift @@ -0,0 +1,30 @@ +// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. + +import Foundation +import GRDB +import SessionUtilitiesKit + +/// This migration adds a table to track pending read receipts (it's possible to receive a read receipt message before getting the original +/// message due to how one-to-one conversations work, by storing pending read receipts we should be able to prevent this case) +enum _012_AddFTSIfNeeded: Migration { + static let target: TargetMigrations.Identifier = .messagingKit + static let identifier: String = "AddFTSIfNeeded" + static let needsConfigSync: Bool = false + static let minExpectedRunDuration: TimeInterval = 0.1 + + static func migrate(_ db: Database) throws { + // Fix an issue that the fullTextSearchTable was dropped unintentionally and global search won't work. + // This issue only happens to internal test users. + if try db.tableExists(Interaction.fullTextSearchTableName) == false { + try db.create(virtualTable: Interaction.fullTextSearchTableName, using: FTS5()) { t in + t.synchronize(withTable: Interaction.databaseTableName) + t.tokenizer = _001_InitialSetupMigration.fullTextSearchTokenizer + + t.column(Interaction.Columns.body.name) + t.column(Interaction.Columns.threadId.name) + } + } + + Storage.update(progress: 1, for: self, in: target) // In case this is the last migration + } +}