Make FTS recovery more resiliant.

This commit is contained in:
Greyson Parrelli
2024-09-12 15:40:33 -04:00
committed by Cody Henthorne
parent 5c6644d1a1
commit 850515b363
2 changed files with 31 additions and 11 deletions

View File

@@ -260,15 +260,24 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
try {
Log.w(TAG, "[fullyResetTables] Dropping tables and triggers...")
// 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
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)")
db.execSQL("DROP TABLE $FTS_TABLE_NAME")
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)
// 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)")
db.execSQL("DROP TABLE $FTS_TABLE_NAME")
}
}
}
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")
@@ -283,11 +292,11 @@ class SearchTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa
Log.w(TAG, "[fullyResetTables] Done. Index will be rebuilt asynchronously)")
if (useTransaction) {
if (useTransaction && db.inTransaction()) {
db.setTransactionSuccessful()
}
} finally {
if (useTransaction) {
if (useTransaction && db.inTransaction()) {
db.endTransaction()
}
}