Fix backup restore crash.

Fixes #8011
This commit is contained in:
Greyson Parrelli 2019-03-10 21:15:18 -07:00
parent f469ce049d
commit 923016f12c
3 changed files with 26 additions and 25 deletions

View file

@ -87,8 +87,6 @@ public class FullBackupImporter extends FullBackupBase {
else if (frame.hasAvatar()) processAvatar(context, frame.getAvatar(), inputStream); else if (frame.hasAvatar()) processAvatar(context, frame.getAvatar(), inputStream);
} }
trimEntriesForExpiredMessages(context, db);
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
db.endTransaction(); db.endTransaction();
@ -191,28 +189,6 @@ public class FullBackupImporter extends FullBackupBase {
} }
} }
private static void trimEntriesForExpiredMessages(@NonNull Context context, @NonNull SQLiteDatabase db) {
String trimmedCondition = " NOT IN (SELECT " + MmsDatabase.ID + " FROM " + MmsDatabase.TABLE_NAME + ")";
db.delete(GroupReceiptDatabase.TABLE_NAME, GroupReceiptDatabase.MMS_ID + trimmedCondition, null);
String[] columns = new String[] { AttachmentDatabase.ROW_ID, AttachmentDatabase.UNIQUE_ID };
String where = AttachmentDatabase.MMS_ID + trimmedCondition;
try (Cursor cursor = db.query(AttachmentDatabase.TABLE_NAME, columns, where, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
DatabaseFactory.getAttachmentDatabase(context).deleteAttachment(new AttachmentId(cursor.getLong(0), cursor.getLong(1)));
}
}
try (Cursor cursor = db.query(ThreadDatabase.TABLE_NAME, new String[] { ThreadDatabase.ID }, ThreadDatabase.EXPIRES_IN + " > 0", null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
DatabaseFactory.getThreadDatabase(context).update(cursor.getLong(0), false);
}
}
}
private static class BackupRecordInputStream extends BackupStream { private static class BackupRecordInputStream extends BackupStream {
private final InputStream in; private final InputStream in;

View file

@ -152,6 +152,7 @@ public class DatabaseFactory {
public static void upgradeRestored(Context context, SQLiteDatabase database){ public static void upgradeRestored(Context context, SQLiteDatabase database){
getInstance(context).databaseHelper.onUpgrade(database, database.getVersion(), -1); getInstance(context).databaseHelper.onUpgrade(database, database.getVersion(), -1);
getInstance(context).databaseHelper.markCurrent(database); getInstance(context).databaseHelper.markCurrent(database);
getInstance(context).mms.trimEntriesForExpiredMessages();
} }
private DatabaseFactory(@NonNull Context context) { private DatabaseFactory(@NonNull Context context) {
@ -205,5 +206,4 @@ public class DatabaseFactory {
listener); listener);
} }
} }
} }

View file

@ -531,6 +531,31 @@ public class MmsDatabase extends MessagingDatabase {
updateMessageBodyAndType(messageId, body, Types.ENCRYPTION_MASK, type); updateMessageBodyAndType(messageId, body, Types.ENCRYPTION_MASK, type);
} }
/**
* Trims data related to expired messages. Only intended to be run after a backup restore.
*/
void trimEntriesForExpiredMessages() {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
String trimmedCondition = " NOT IN (SELECT " + MmsDatabase.ID + " FROM " + MmsDatabase.TABLE_NAME + ")";
database.delete(GroupReceiptDatabase.TABLE_NAME, GroupReceiptDatabase.MMS_ID + trimmedCondition, null);
String[] columns = new String[] { AttachmentDatabase.ROW_ID, AttachmentDatabase.UNIQUE_ID };
String where = AttachmentDatabase.MMS_ID + trimmedCondition;
try (Cursor cursor = database.query(AttachmentDatabase.TABLE_NAME, columns, where, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
DatabaseFactory.getAttachmentDatabase(context).deleteAttachment(new AttachmentId(cursor.getLong(0), cursor.getLong(1)));
}
}
try (Cursor cursor = database.query(ThreadDatabase.TABLE_NAME, new String[] { ThreadDatabase.ID }, ThreadDatabase.EXPIRES_IN + " > 0", null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
DatabaseFactory.getThreadDatabase(context).update(cursor.getLong(0), false);
}
}
}
private Pair<Long, Long> updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) { private Pair<Long, Long> updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) {
SQLiteDatabase db = databaseHelper.getWritableDatabase(); SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.execSQL("UPDATE " + TABLE_NAME + " SET " + BODY + " = ?, " + db.execSQL("UPDATE " + TABLE_NAME + " SET " + BODY + " = ?, " +