From ad036b0d6a45e333afd8e83f40e8c3ede2fa8dee Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Fri, 7 Sep 2018 16:08:45 -0700 Subject: [PATCH] Fix backup restore issues from restoring newer Signal backups. Fixes #8184 --- .../database/helpers/SQLCipherOpenHelper.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index de885d2be2..991539c2a1 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -241,11 +241,13 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { } } - if (oldVersion < QUOTE_MISSING) { + // Note: This column only being checked due to upgrade issues as described in #8184 + if (oldVersion < QUOTE_MISSING && !columnExists(db, "mms", "quote_missing")) { db.execSQL("ALTER TABLE mms ADD COLUMN quote_missing INTEGER DEFAULT 0"); } - if (oldVersion < NOTIFICATION_CHANNELS) { + // Note: The column only being checked due to upgrade issues as described in #8184 + if (oldVersion < NOTIFICATION_CHANNELS && !columnExists(db, "recipient_preferences", "notification_channel")) { db.execSQL("ALTER TABLE recipient_preferences ADD COLUMN notification_channel TEXT DEFAULT NULL"); NotificationChannels.create(context); @@ -309,5 +311,19 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(statement); } + private static boolean columnExists(@NonNull SQLiteDatabase db, @NonNull String table, @NonNull String column) { + try (Cursor cursor = db.rawQuery("PRAGMA table_info(" + table + ")", null)) { + int nameColumnIndex = cursor.getColumnIndexOrThrow("name"); + while (cursor.moveToNext()) { + String name = cursor.getString(nameColumnIndex); + + if (name.equals(column)) { + return true; + } + } + } + + return false; + } }