Skip to content

Commit

Permalink
Fix bad unread mentions database migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Oct 31, 2022
1 parent 06c9dbe commit e00ed81
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Expand Up @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.database.helpers
import android.app.Application
import android.content.Context
import net.zetetic.database.sqlcipher.SQLiteDatabase
import org.signal.core.util.logging.Log
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
Expand All @@ -16,13 +17,16 @@ import org.thoughtcrime.securesms.database.helpers.migration.V158_GroupsLastForc
import org.thoughtcrime.securesms.database.helpers.migration.V159_ThreadUnreadSelfMentionCount
import org.thoughtcrime.securesms.database.helpers.migration.V160_SmsMmsExportedIndexMigration
import org.thoughtcrime.securesms.database.helpers.migration.V161_StorySendMessageIdIndex
import org.thoughtcrime.securesms.database.helpers.migration.V162_ThreadUnreadSelfMentionCountFixup

/**
* Contains all of the database migrations for [SignalDatabase]. Broken into a separate file for cleanliness.
*/
object SignalDatabaseMigrations {

const val DATABASE_VERSION = 161
val TAG: String = Log.tag(SignalDatabaseMigrations.javaClass)

const val DATABASE_VERSION = 162

@JvmStatic
fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
Expand Down Expand Up @@ -77,6 +81,10 @@ object SignalDatabaseMigrations {
if (oldVersion < 161) {
V161_StorySendMessageIdIndex.migrate(context, db, oldVersion, newVersion)
}

if (oldVersion < 162) {
V162_ThreadUnreadSelfMentionCountFixup.migrate(context, db, oldVersion, newVersion)
}
}

@JvmStatic
Expand Down
Expand Up @@ -58,7 +58,7 @@ import java.util.UUID
*/
object V149_LegacyMigrations : SignalDatabaseMigration {

private val TAG: String = Log.tag(SignalDatabaseMigrations.javaClass)
private val TAG: String = SignalDatabaseMigrations.TAG

private const val RECIPIENT_CALL_RINGTONE_VERSION = 2
const val MIGRATE_PREKEYS_VERSION = 3
Expand Down
@@ -0,0 +1,35 @@
package org.thoughtcrime.securesms.database.helpers.migration

import android.app.Application
import androidx.sqlite.db.SupportSQLiteDatabase
import net.zetetic.database.sqlcipher.SQLiteDatabase
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.database.helpers.SignalDatabaseMigrations

/**
* A bad cherry-pick for a database change requires us to attempt to alter the table again
* to fix it.
*/
object V162_ThreadUnreadSelfMentionCountFixup : SignalDatabaseMigration {

override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (columnMissing(db, "thread", "unread_self_mention_count")) {
Log.i(SignalDatabaseMigrations.TAG, "Fixing up thread table and unread_self_mention_count column")
db.execSQL("ALTER TABLE thread ADD COLUMN unread_self_mention_count INTEGER DEFAULT 0")
}
}

@Suppress("SameParameterValue")
private fun columnMissing(db: SupportSQLiteDatabase, table: String, column: String): Boolean {
db.query("PRAGMA table_info($table)", null).use { cursor ->
val nameColumnIndex = cursor.getColumnIndexOrThrow("name")
while (cursor.moveToNext()) {
val name = cursor.getString(nameColumnIndex)
if (name == column) {
return false
}
}
}
return true
}
}

0 comments on commit e00ed81

Please sign in to comment.