Update database migrations to be in their own files.

fork-5.53.8
Greyson Parrelli 2022-08-30 16:56:19 -04:00
rodzic a1a677a3e2
commit 8543325d59
8 zmienionych plików z 2887 dodań i 2724 usunięć

Wyświetl plik

@ -7,7 +7,7 @@ import net.zetetic.database.sqlcipher.SQLiteDatabase
* Adding an urgent flag to message envelopes to help with notifications. Need to track flag in
* MSL table so can be resent with the correct urgency.
*/
object UrgentMslFlagMigration : SignalDatabaseMigration {
object V150_UrgentMslFlagMigration : SignalDatabaseMigration {
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("ALTER TABLE msl_payload ADD COLUMN urgent INTEGER NOT NULL DEFAULT 1")
}

Wyświetl plik

@ -13,9 +13,9 @@ import org.thoughtcrime.securesms.util.Base64
/**
* Performs a check and ensures that MyStory exists at the correct distribution list id and correct distribution id.
*/
object MyStoryMigration : SignalDatabaseMigration {
object V151_MyStoryMigration : SignalDatabaseMigration {
private val TAG = Log.tag(MyStoryMigration::class.java)
private val TAG = Log.tag(V151_MyStoryMigration::class.java)
private const val TABLE_NAME = "distribution_list"
private const val NAME = "name"

Wyświetl plik

@ -0,0 +1,19 @@
package org.thoughtcrime.securesms.database.helpers.migration
import android.app.Application
import net.zetetic.database.sqlcipher.SQLiteDatabase
/**
* Marks story recipients with a new group type constant.
*/
object V152_StoryGroupTypesMigration : SignalDatabaseMigration {
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL(
"""
UPDATE recipient
SET group_type = 4
WHERE distribution_list_id IS NOT NULL
""".trimIndent()
)
}
}

Wyświetl plik

@ -0,0 +1,130 @@
package org.thoughtcrime.securesms.database.helpers.migration
import android.app.Application
import android.database.Cursor
import androidx.core.content.contentValuesOf
import net.zetetic.database.sqlcipher.SQLiteDatabase
import org.signal.core.util.CursorUtil
import org.signal.core.util.SqlUtil
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.storage.StorageSyncHelper
import org.thoughtcrime.securesms.util.Base64
/**
* Performs a check and ensures that MyStory exists at the correct distribution list id and correct distribution id.
*/
object V153_MyStoryMigration : SignalDatabaseMigration {
private val TAG = Log.tag(V153_MyStoryMigration::class.java)
private const val TABLE_NAME = "distribution_list"
private const val NAME = "name"
private const val DISTRIBUTION_LIST_ID = "_id"
private const val DISTRIBUTION_ID = "distribution_id"
private const val RECIPIENT_ID = "recipient_id"
private const val PRIVACY_MODE = "privacy_mode"
private const val MY_STORY_DISTRIBUTION_LIST_ID = 1
private const val MY_STORY_DISTRIBUTION_ID = "00000000-0000-0000-0000-000000000000"
private const val MY_STORY_PRIVACY_MODE = 2
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
val result: MyStoryExistsResult = getMyStoryCursor(db).use { cursor ->
if (cursor.moveToNext()) {
val distributionId = CursorUtil.requireString(cursor, DISTRIBUTION_ID)
if (distributionId != MY_STORY_DISTRIBUTION_ID) {
Log.d(TAG, "[migrate] Invalid MyStory DistributionId: $distributionId")
MyStoryExistsResult.REQUIRES_DISTRIBUTION_ID_UPDATE
} else {
Log.d(TAG, "[migrate] MyStory DistributionId matches expected value.")
MyStoryExistsResult.MATCHES_EXPECTED_VALUE
}
} else {
Log.d(TAG, "[migrate] My Story does not exist.")
MyStoryExistsResult.DOES_NOT_EXIST
}
}
when (result) {
MyStoryExistsResult.REQUIRES_DISTRIBUTION_ID_UPDATE -> updateDistributionIdToExpectedValue(db)
MyStoryExistsResult.MATCHES_EXPECTED_VALUE -> Unit
MyStoryExistsResult.DOES_NOT_EXIST -> createMyStory(db)
}
}
private fun updateDistributionIdToExpectedValue(db: SQLiteDatabase) {
Log.d(TAG, "[updateDistributionIdToExpectedValue] Overwriting My Story DistributionId with expected value.")
db.update(
TABLE_NAME,
contentValuesOf(DISTRIBUTION_ID to MY_STORY_DISTRIBUTION_ID),
"$DISTRIBUTION_LIST_ID = ?",
arrayOf(MY_STORY_DISTRIBUTION_LIST_ID.toString())
)
}
private fun createMyStory(db: SQLiteDatabase) {
Log.d(TAG, "[createMyStory] Attempting to create My Story.")
val recipientId: Long = getMyStoryRecipientId(db) ?: createMyStoryRecipientId(db)
db.insert(
TABLE_NAME,
null,
contentValuesOf(
DISTRIBUTION_LIST_ID to MY_STORY_DISTRIBUTION_LIST_ID,
NAME to MY_STORY_DISTRIBUTION_ID,
DISTRIBUTION_ID to MY_STORY_DISTRIBUTION_ID,
RECIPIENT_ID to recipientId,
PRIVACY_MODE to MY_STORY_PRIVACY_MODE
)
)
}
private fun createMyStoryRecipientId(db: SQLiteDatabase): Long {
return db.insert(
"recipient",
null,
contentValuesOf(
"group_type" to 4,
"distribution_list_id" to MY_STORY_DISTRIBUTION_LIST_ID,
"storage_service_key" to Base64.encodeBytes(StorageSyncHelper.generateKey()),
"profile_sharing" to 1
)
)
}
private fun getMyStoryRecipientId(db: SQLiteDatabase): Long? {
return db.query(
"recipient",
arrayOf("_id"),
"distribution_list_id = ?",
SqlUtil.buildArgs(MY_STORY_DISTRIBUTION_LIST_ID),
null,
null,
null
).use {
if (it.moveToNext()) {
CursorUtil.requireLong(it, "_id")
} else {
null
}
}
}
private fun getMyStoryCursor(db: SQLiteDatabase): Cursor {
return db.query(
TABLE_NAME,
arrayOf(DISTRIBUTION_ID),
"$DISTRIBUTION_LIST_ID = ?",
arrayOf(MY_STORY_DISTRIBUTION_LIST_ID.toString()),
null,
null,
null
)
}
private enum class MyStoryExistsResult {
REQUIRES_DISTRIBUTION_ID_UPDATE,
MATCHES_EXPECTED_VALUE,
DOES_NOT_EXIST
}
}

Wyświetl plik

@ -6,7 +6,7 @@ import net.zetetic.database.sqlcipher.SQLiteDatabase
/**
* Introduces the tables and fields required to keep track of whether we need to send a PNI signature message and if the ones we've sent out have been received.
*/
object PniSignaturesMigration : SignalDatabaseMigration {
object V154_PniSignaturesMigration : SignalDatabaseMigration {
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("ALTER TABLE recipient ADD COLUMN needs_pni_signature")

Wyświetl plik

@ -6,7 +6,7 @@ import net.zetetic.database.sqlcipher.SQLiteDatabase
/**
* Adds necessary book-keeping columns to SMS and MMS tables for SMS export.
*/
object SmsExporterMigration : SignalDatabaseMigration {
object V155_SmsExporterMigration : SignalDatabaseMigration {
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("ALTER TABLE mms ADD COLUMN export_state BLOB DEFAULT NULL")
db.execSQL("ALTER TABLE mms ADD COLUMN exported INTEGER DEFAULT 0")