diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt index 57ddff5ec9..8cdc366cfa 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt @@ -15,8 +15,6 @@ import org.thoughtcrime.securesms.database.helpers.PreKeyMigrationHelper import org.thoughtcrime.securesms.database.helpers.SQLCipherMigrationHelper import org.thoughtcrime.securesms.database.helpers.SessionStoreMigrationHelper import org.thoughtcrime.securesms.database.helpers.SignalDatabaseMigrations -import org.thoughtcrime.securesms.database.helpers.SignalDatabaseMigrations.migrate -import org.thoughtcrime.securesms.database.helpers.SignalDatabaseMigrations.migratePostTransaction import org.thoughtcrime.securesms.database.model.AvatarPickerDatabase import org.thoughtcrime.securesms.jobs.PreKeysSyncJob import org.thoughtcrime.securesms.migrations.LegacyMigrationJob @@ -164,21 +162,15 @@ open class SignalDatabase(private val context: Application, databaseSecret: Data Log.i(TAG, "Upgrading database: $oldVersion, $newVersion") val startTime = System.currentTimeMillis() - db.beginTransaction() try { - migrate(context, db, oldVersion, newVersion) - db.version = newVersion - db.setTransactionSuccessful() + // Transactions and version bumps are handled in the migrate method + SignalDatabaseMigrations.migrate(context, db, oldVersion, newVersion) } finally { - if (db.inTransaction()) { - db.endTransaction() - } - // We have to re-begin the transaction for the calling code (see comment at start of method) db.beginTransaction() } - migratePostTransaction(context, oldVersion) + SignalDatabaseMigrations.migratePostTransaction(context, oldVersion) Log.i(TAG, "Upgrade complete. Took " + (System.currentTimeMillis() - startTime) + " ms.") } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt index 50f068beb3..fbbaef7134 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt @@ -4,6 +4,8 @@ import android.app.Application import android.content.Context import net.zetetic.database.sqlcipher.SQLiteDatabase import org.signal.core.util.logging.Log +import org.signal.core.util.withinTransaction +import org.thoughtcrime.securesms.database.helpers.migration.SignalDatabaseMigration import org.thoughtcrime.securesms.database.helpers.migration.V149_LegacyMigrations import org.thoughtcrime.securesms.database.helpers.migration.V150_UrgentMslFlagMigration import org.thoughtcrime.securesms.database.helpers.migration.V151_MyStoryMigration @@ -76,258 +78,85 @@ object SignalDatabaseMigrations { const val DATABASE_VERSION = 211 + private val migrations: List> = listOf( + 149 to V149_LegacyMigrations, + 150 to V150_UrgentMslFlagMigration, + 151 to V151_MyStoryMigration, + 152 to V152_StoryGroupTypesMigration, + 153 to V153_MyStoryMigration, + 154 to V154_PniSignaturesMigration, + 155 to V155_SmsExporterMigration, + 156 to V156_RecipientUnregisteredTimestampMigration, + 157 to V157_RecipeintHiddenMigration, + 158 to V158_GroupsLastForceUpdateTimestampMigration, + 159 to V159_ThreadUnreadSelfMentionCount, + 160 to V160_SmsMmsExportedIndexMigration, + 161 to V161_StorySendMessageIdIndex, + 162 to V162_ThreadUnreadSelfMentionCountFixup, + 163 to V163_RemoteMegaphoneSnoozeSupportMigration, + 164 to V164_ThreadDatabaseReadIndexMigration, + 165 to V165_MmsMessageBoxPaymentTransactionIndexMigration, + 166 to V166_ThreadAndMessageForeignKeys, + 167 to V167_RecreateReactionTriggers, + 168 to V168_SingleMessageTableMigration, + 169 to V169_EmojiSearchIndexRank, + 170 to V170_CallTableMigration, + 171 to V171_ThreadForeignKeyFix, + 172 to V172_GroupMembershipMigration, + 173 to V173_ScheduledMessagesMigration, + 174 to V174_ReactionForeignKeyMigration, + 175 to V175_FixFullTextSearchLink, + 176 to V176_AddScheduledDateToQuoteIndex, + 177 to V177_MessageSendLogTableCleanupMigration, + 178 to V178_ReportingTokenColumnMigration, + 179 to V179_CleanupDanglingMessageSendLogMigration, + 180 to V180_RecipientNicknameMigration, + 181 to V181_ThreadTableForeignKeyCleanup, + 182 to V182_CallTableMigration, + 183 to V183_CallLinkTableMigration, + 184 to V184_CallLinkReplaceIndexMigration, + 185 to V185_MessageRecipientsAndEditMessageMigration, + 186 to V186_ForeignKeyIndicesMigration, + 187 to V187_MoreForeignKeyIndexesMigration, + 188 to V188_FixMessageRecipientsAndEditMessageMigration, + 189 to V189_CreateCallLinkTableColumnsAndRebuildFKReference, + 190 to V190_UniqueMessageMigration, + 191 to V191_UniqueMessageMigrationV2, + 192 to V192_CallLinkTableNullableRootKeys, + 193 to V193_BackCallLinksWithRecipient, + 194 to V194_KyberPreKeyMigration, + 195 to V195_GroupMemberForeignKeyMigration, + 196 to V196_BackCallLinksWithRecipientV2, + 197 to V197_DropAvatarColorFromCallLinks, + 198 to V198_AddMacDigestColumn, + 199 to V199_AddThreadActiveColumn, + 200 to V200_ResetPniColumn, + 201 to V201_RecipientTableValidations, + 202 to V202_DropMessageTableThreadDateIndex, + 203 to V203_PreKeyStaleTimestamp, + 204 to V204_GroupForeignKeyMigration, + 205 to V205_DropPushTable, + 206 to V206_AddConversationCountIndex, + 207 to V207_AddChunkSizeColumn, + // 208 was a bad migration that only manipulated data and did not change schema, replaced by 209 + 209 to V209_ClearRecipientPniFromAciColumn, + 210 to V210_FixPniPossibleColumns, + 211 to V211_ReceiptColumnRenames + ) + @JvmStatic fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { - if (oldVersion < 149) { - V149_LegacyMigrations.migrate(context, db, oldVersion, newVersion) - } + for (migrationData in migrations) { + val (version, migration) = migrationData - if (oldVersion < 150) { - V150_UrgentMslFlagMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 151) { - V151_MyStoryMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 152) { - V152_StoryGroupTypesMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 153) { - V153_MyStoryMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 154) { - V154_PniSignaturesMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 155) { - V155_SmsExporterMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 156) { - V156_RecipientUnregisteredTimestampMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 157) { - V157_RecipeintHiddenMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 158) { - V158_GroupsLastForceUpdateTimestampMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 159) { - V159_ThreadUnreadSelfMentionCount.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 160) { - V160_SmsMmsExportedIndexMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 161) { - V161_StorySendMessageIdIndex.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 162) { - V162_ThreadUnreadSelfMentionCountFixup.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 163) { - V163_RemoteMegaphoneSnoozeSupportMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 164) { - V164_ThreadDatabaseReadIndexMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 165) { - V165_MmsMessageBoxPaymentTransactionIndexMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 166) { - V166_ThreadAndMessageForeignKeys.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 167) { - V167_RecreateReactionTriggers.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 168) { - V168_SingleMessageTableMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 169) { - V169_EmojiSearchIndexRank.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 170) { - V170_CallTableMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 171) { - V171_ThreadForeignKeyFix.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 172) { - V172_GroupMembershipMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 173) { - V173_ScheduledMessagesMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 174) { - V174_ReactionForeignKeyMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 175) { - V175_FixFullTextSearchLink.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 176) { - V176_AddScheduledDateToQuoteIndex.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 177) { - V177_MessageSendLogTableCleanupMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 178) { - V178_ReportingTokenColumnMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 179) { - V179_CleanupDanglingMessageSendLogMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 180) { - V180_RecipientNicknameMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 181) { - V181_ThreadTableForeignKeyCleanup.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 182) { - V182_CallTableMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 183) { - V183_CallLinkTableMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 184) { - V184_CallLinkReplaceIndexMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 185) { - V185_MessageRecipientsAndEditMessageMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 186) { - V186_ForeignKeyIndicesMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 187) { - V187_MoreForeignKeyIndexesMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 188) { - V188_FixMessageRecipientsAndEditMessageMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 189) { - V189_CreateCallLinkTableColumnsAndRebuildFKReference.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 190) { - V190_UniqueMessageMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 191) { - V191_UniqueMessageMigrationV2.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 192) { - V192_CallLinkTableNullableRootKeys.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 193) { - V193_BackCallLinksWithRecipient.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 194) { - V194_KyberPreKeyMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 195) { - V195_GroupMemberForeignKeyMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 196) { - V196_BackCallLinksWithRecipientV2.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 197) { - V197_DropAvatarColorFromCallLinks.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 198) { - V198_AddMacDigestColumn.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 199) { - V199_AddThreadActiveColumn.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 200) { - V200_ResetPniColumn.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 201) { - V201_RecipientTableValidations.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 202) { - V202_DropMessageTableThreadDateIndex.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 203) { - V203_PreKeyStaleTimestamp.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 204) { - V204_GroupForeignKeyMigration.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 205) { - V205_DropPushTable.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 206) { - V206_AddConversationCountIndex.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 207) { - V207_AddChunkSizeColumn.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 208) { - // Bad migration that only manipulated data and did not change schema, replaced by 209 - } - - if (oldVersion < 209) { - V209_ClearRecipientPniFromAciColumn.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 210) { - V210_FixPniPossibleColumns.migrate(context, db, oldVersion, newVersion) - } - - if (oldVersion < 211) { - V211_ReceiptColumnRenames.migrate(context, db, oldVersion, newVersion) + if (oldVersion < version) { + Log.i(TAG, "Running migration for version $version: ${migration.javaClass.simpleName}") + db.withinTransaction { + migration.migrate(context, db, oldVersion, newVersion) + db.version = version + } + Log.i(TAG, "Successfully completed migration for version $version.") + } } }