From b41364c709dc1c076a7057179eb4e5654a63fa6a Mon Sep 17 00:00:00 2001 From: haffenloher Date: Fri, 4 Dec 2015 22:47:33 +0100 Subject: [PATCH] Add inbox delivery status migration Fixes #4799 Closes #4809 // FREEBIE --- .../securesms/database/DatabaseFactory.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java index 8044742c3..d3ac714a4 100644 --- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java +++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java @@ -70,7 +70,8 @@ public class DatabaseFactory { private static final int INTRODUCED_CONVERSATION_LIST_THUMBNAILS_VERSION = 23; private static final int INTRODUCED_ARCHIVE_VERSION = 24; private static final int INTRODUCED_CONVERSATION_LIST_STATUS_VERSION = 25; - private static final int DATABASE_VERSION = 25; + private static final int MIGRATED_CONVERSATION_LIST_STATUS_VERSION = 26; + private static final int DATABASE_VERSION = 26; private static final String DATABASE_NAME = "messages.db"; private static final Object lock = new Object(); @@ -790,6 +791,28 @@ public class DatabaseFactory { db.execSQL("ALTER TABLE thread ADD COLUMN delivery_receipt_count INTEGER DEFAULT 0"); } + if (oldVersion < MIGRATED_CONVERSATION_LIST_STATUS_VERSION) { + Cursor threadCursor = db.query("thread", new String[] {"_id"}, null, null, null, null, null); + + while (threadCursor != null && threadCursor.moveToNext()) { + long threadId = threadCursor.getLong(threadCursor.getColumnIndexOrThrow("_id")); + + Cursor cursor = db.rawQuery("SELECT DISTINCT date AS date_received, status, " + + "delivery_receipt_count FROM sms WHERE (thread_id = ?1) " + + "UNION ALL SELECT DISTINCT date_received, -1 AS status, " + + "delivery_receipt_count FROM mms WHERE (thread_id = ?1) " + + "ORDER BY date_received DESC LIMIT 1", new String[]{threadId + ""}); + + if (cursor != null && cursor.moveToNext()) { + int status = cursor.getInt(cursor.getColumnIndexOrThrow("status")); + int receiptCount = cursor.getInt(cursor.getColumnIndexOrThrow("delivery_receipt_count")); + + db.execSQL("UPDATE thread SET status = ?, delivery_receipt_count = ? WHERE _id = ?", + new String[]{status + "", receiptCount + "", threadId + ""}); + } + } + } + db.setTransactionSuccessful(); db.endTransaction(); }