From 760d5ab2ce61cd9c99eea68ca7cb12e282a7aa82 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 24 Sep 2024 12:51:56 -0400 Subject: [PATCH] Be even more cautious when repairing FTS tables. --- .../securesms/database/SearchTable.kt | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/SearchTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/SearchTable.kt index 54a4d7509e..7194d90b7c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/SearchTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/SearchTable.kt @@ -266,16 +266,28 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa try { db.execSQL("DROP TABLE IF EXISTS $FTS_TABLE_NAME") } catch (e: SQLiteException) { - Log.w(TAG, "Failed to drop $FTS_TABLE_NAME! Attempting to do so a safer way.", e) + Log.w(TAG, "[fullyResetTables] Failed to drop $FTS_TABLE_NAME! Attempting to do so a safer way.", e) // Due to issues we've had in the past, the delete sequence here is very particular. It mimics the "safe drop" process in the SQLite source code // that prevents weird vtable constructor issues when dropping potentially-corrupt tables. https://sqlite.org/src/info/4db9258a78?ln=1549-1592 db.withinTransaction { if (SqlUtil.tableExists(db, FTS_TABLE_NAME)) { - db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_data") - db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_config") - db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_data VALUES(10, X'0000000000')") - db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_config VALUES('version', 4)") + val dataExists = SqlUtil.tableExists(db, "${FTS_TABLE_NAME}_data") + val configExists = SqlUtil.tableExists(db, "${FTS_TABLE_NAME}_config") + + if (!dataExists) { + Log.w(TAG, "[fullyResetTables] Main FTS table exists, but _data does not!") + } + + if (!configExists) { + Log.w(TAG, "[fullyResetTables] Main FTS table exists, but _config does not!") + } + + if (dataExists) db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_data") + if (configExists) db.execSQL("DELETE FROM ${FTS_TABLE_NAME}_config") + if (dataExists) db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_data VALUES(10, X'0000000000')") + if (configExists) db.execSQL("INSERT INTO ${FTS_TABLE_NAME}_config VALUES('version', 4)") + db.execSQL("DROP TABLE $FTS_TABLE_NAME") } }