From b7282589de18e44acc3712d1d08e13e8dec15010 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 6 Jun 2018 09:07:38 -0700 Subject: [PATCH] Fixed issue with backup and restore when creating new tables. Fixes #7863 --- .../securesms/backup/FullBackupImporter.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java index 8f760467e3..2ac0224ea8 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java +++ b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java @@ -5,6 +5,7 @@ import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.content.SharedPreferences; +import android.database.Cursor; import android.support.annotation.NonNull; import android.util.Pair; @@ -62,6 +63,8 @@ public class FullBackupImporter extends FullBackupBase { try { db.beginTransaction(); + dropAllTables(db); + BackupFrame frame; while (!(frame = inputStream.readFrame()).getEnd()) { @@ -131,6 +134,19 @@ public class FullBackupImporter extends FullBackupBase { preferences.edit().putString(preference.getKey(), preference.getValue()).commit(); } + private static void dropAllTables(@NonNull SQLiteDatabase db) { + try (Cursor cursor = db.rawQuery("SELECT name, type FROM sqlite_master", null)) { + while (cursor != null && cursor.moveToNext()) { + String name = cursor.getString(0); + String type = cursor.getString(1); + + if ("table".equals(type)) { + db.execSQL("DROP TABLE IF EXISTS " + name); + } + } + } + } + private static class BackupRecordInputStream extends BackupStream { private final InputStream in;