Potential fix for missing FTS tables.

This commit is contained in:
Greyson Parrelli
2024-08-13 11:30:10 -04:00
parent 19f3219224
commit 1e4e6b6b41
3 changed files with 32 additions and 18 deletions

View File

@@ -236,26 +236,40 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
* Drops all tables and recreates them.
*/
@JvmOverloads
fun fullyResetTables(db: SQLiteDatabase = writableDatabase.sqlCipherDatabase) {
Log.w(TAG, "[fullyResetTables] Dropping tables and triggers...")
db.execSQL("DROP TABLE IF EXISTS $FTS_TABLE_NAME")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_config")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_content")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_data")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_idx")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_INSERT")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_DELETE")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_UPDATE")
fun fullyResetTables(db: SQLiteDatabase = writableDatabase.sqlCipherDatabase, useTransaction: Boolean = true) {
if (useTransaction) {
db.beginTransaction()
}
Log.w(TAG, "[fullyResetTables] Recreating table...")
CREATE_TABLE.forEach { db.execSQL(it) }
try {
Log.w(TAG, "[fullyResetTables] Dropping tables and triggers...")
db.execSQL("DROP TABLE IF EXISTS $FTS_TABLE_NAME")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_config")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_content")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_data")
db.execSQL("DROP TABLE IF EXISTS ${FTS_TABLE_NAME}_idx")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_INSERT")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_DELETE")
db.execSQL("DROP TRIGGER IF EXISTS $TRIGGER_AFTER_UPDATE")
Log.w(TAG, "[fullyResetTables] Recreating triggers...")
CREATE_TRIGGERS.forEach { db.execSQL(it) }
Log.w(TAG, "[fullyResetTables] Recreating table...")
CREATE_TABLE.forEach { db.execSQL(it) }
RebuildMessageSearchIndexJob.enqueue()
Log.w(TAG, "[fullyResetTables] Recreating triggers...")
CREATE_TRIGGERS.forEach { db.execSQL(it) }
Log.w(TAG, "[fullyResetTables] Done. Index will be rebuilt asynchronously)")
RebuildMessageSearchIndexJob.enqueue()
Log.w(TAG, "[fullyResetTables] Done. Index will be rebuilt asynchronously)")
if (useTransaction) {
db.setTransactionSuccessful()
}
} finally {
if (useTransaction) {
db.endTransaction()
}
}
}
/**

View File

@@ -291,7 +291,7 @@ open class SignalDatabase(private val context: Application, databaseSecret: Data
instance!!.messageTable.deleteAbandonedMessages()
instance!!.messageTable.trimEntriesForExpiredMessages()
instance!!.reactionTable.deleteAbandonedReactions()
instance!!.searchTable.fullyResetTables()
instance!!.searchTable.fullyResetTables(useTransaction = false)
instance!!.rawWritableDatabase.execSQL("DROP TABLE IF EXISTS key_value")
instance!!.rawWritableDatabase.execSQL("DROP TABLE IF EXISTS megaphone")
instance!!.rawWritableDatabase.execSQL("DROP TABLE IF EXISTS job_spec")

View File

@@ -171,7 +171,7 @@ class SqlCipherErrorHandler(private val databaseName: String) : DatabaseErrorHan
} catch (e: Exception) {
Log.w(TAG, "Failed to re-open as read-write!", e)
}
SignalDatabase.messageSearch.fullyResetTables(db)
SignalDatabase.messageSearch.fullyResetTables(db, useTransaction = false)
} catch (e: Throwable) {
Log.w(TAG, "Failed to clear full text search index.", e)
}