From a46e1a451fe1ab0551a3cc1381216a9168eff359 Mon Sep 17 00:00:00 2001 From: Michelle Tang Date: Wed, 11 Jun 2025 10:40:13 -0400 Subject: [PATCH] Add foreign key reference to notification profile members. --- .../database/NotificationProfileTables.kt | 7 +-- .../helpers/SignalDatabaseMigrations.kt | 6 ++- .../V279_AddNotificationProfileForeignKey.kt | 43 +++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V279_AddNotificationProfileForeignKey.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/NotificationProfileTables.kt b/app/src/main/java/org/thoughtcrime/securesms/database/NotificationProfileTables.kt index 08f45dbb45..6c51cf766b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/NotificationProfileTables.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/NotificationProfileTables.kt @@ -52,7 +52,7 @@ class NotificationProfileTables(context: Context, databaseHelper: SignalDatabase val CREATE_TABLE: Array = arrayOf(NotificationProfileTable.CREATE_TABLE, NotificationProfileScheduleTable.CREATE_TABLE, NotificationProfileAllowedMembersTable.CREATE_TABLE) @JvmField - val CREATE_INDEXES: Array = arrayOf(NotificationProfileScheduleTable.CREATE_INDEX, NotificationProfileAllowedMembersTable.CREATE_INDEX) + val CREATE_INDEXES: Array = arrayOf(NotificationProfileScheduleTable.CREATE_INDEX, NotificationProfileAllowedMembersTable.CREATE_NOTIFICATION_PROFILE_INDEX, NotificationProfileAllowedMembersTable.CREATE_RECIPIENT_ID_INDEX) } object NotificationProfileTable { @@ -124,12 +124,13 @@ class NotificationProfileTables(context: Context, databaseHelper: SignalDatabase CREATE TABLE $TABLE_NAME ( $ID INTEGER PRIMARY KEY AUTOINCREMENT, $NOTIFICATION_PROFILE_ID INTEGER NOT NULL REFERENCES ${NotificationProfileTable.TABLE_NAME} (${NotificationProfileTable.ID}) ON DELETE CASCADE, - $RECIPIENT_ID INTEGER NOT NULL, + $RECIPIENT_ID INTEGER NOT NULL REFERENCES ${RecipientTable.TABLE_NAME} (${RecipientTable.ID}) ON DELETE CASCADE, UNIQUE($NOTIFICATION_PROFILE_ID, $RECIPIENT_ID) ON CONFLICT REPLACE ) """ - const val CREATE_INDEX = "CREATE INDEX notification_profile_allowed_members_profile_index ON $TABLE_NAME ($NOTIFICATION_PROFILE_ID)" + const val CREATE_NOTIFICATION_PROFILE_INDEX = "CREATE INDEX notification_profile_allowed_members_profile_index ON $TABLE_NAME ($NOTIFICATION_PROFILE_ID)" + const val CREATE_RECIPIENT_ID_INDEX = "CREATE INDEX notification_profile_allowed_members_recipient_index ON $TABLE_NAME ($RECIPIENT_ID)" } fun createProfile(name: String, emoji: String, color: AvatarColor, createdAt: Long): NotificationProfileChangeResult { 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 2a1601d468..3e60dc5019 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 @@ -133,6 +133,7 @@ import org.thoughtcrime.securesms.database.helpers.migration.V275_EnsureDefaultA import org.thoughtcrime.securesms.database.helpers.migration.V276_AttachmentCdnDefaultValueMigration import org.thoughtcrime.securesms.database.helpers.migration.V277_AddNotificationProfileStorageSync import org.thoughtcrime.securesms.database.helpers.migration.V278_BackupSnapshotTableVersions +import org.thoughtcrime.securesms.database.helpers.migration.V279_AddNotificationProfileForeignKey import org.thoughtcrime.securesms.database.SQLiteDatabase as SignalSqliteDatabase /** @@ -271,10 +272,11 @@ object SignalDatabaseMigrations { 275 to V275_EnsureDefaultAllChatsFolder, 276 to V276_AttachmentCdnDefaultValueMigration, 277 to V277_AddNotificationProfileStorageSync, - 278 to V278_BackupSnapshotTableVersions + 278 to V278_BackupSnapshotTableVersions, + 279 to V279_AddNotificationProfileForeignKey ) - const val DATABASE_VERSION = 278 + const val DATABASE_VERSION = 279 @JvmStatic fun migrate(context: Application, db: SignalSqliteDatabase, oldVersion: Int, newVersion: Int) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V279_AddNotificationProfileForeignKey.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V279_AddNotificationProfileForeignKey.kt new file mode 100644 index 0000000000..162e780985 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/migration/V279_AddNotificationProfileForeignKey.kt @@ -0,0 +1,43 @@ +package org.thoughtcrime.securesms.database.helpers.migration + +import android.app.Application +import org.thoughtcrime.securesms.database.SQLiteDatabase + +/** + * Adds a recipient foreign key constraint to notification profile members + */ +object V279_AddNotificationProfileForeignKey : SignalDatabaseMigration { + + override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL("DROP INDEX IF EXISTS notification_profile_allowed_members_profile_index") + + db.execSQL( + """ + CREATE TABLE notification_profile_allowed_members_tmp ( + _id INTEGER PRIMARY KEY AUTOINCREMENT, + notification_profile_id INTEGER NOT NULL REFERENCES notification_profile (_id) ON DELETE CASCADE, + recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE, + UNIQUE(notification_profile_id, recipient_id) ON CONFLICT REPLACE + ) + """.trimIndent() + ) + + db.execSQL( + """ + INSERT INTO notification_profile_allowed_members_tmp (_id, notification_profile_id, recipient_id) + SELECT + notification_profile_allowed_members._id, + notification_profile_allowed_members.notification_profile_id, + notification_profile_allowed_members.recipient_id + FROM notification_profile_allowed_members + INNER JOIN recipient ON notification_profile_allowed_members.recipient_id = recipient._id + """.trimIndent() + ) + + db.execSQL("DROP TABLE notification_profile_allowed_members") + db.execSQL("ALTER TABLE notification_profile_allowed_members_tmp RENAME TO notification_profile_allowed_members") + + db.execSQL("CREATE INDEX notification_profile_allowed_members_profile_index ON notification_profile_allowed_members (notification_profile_id)") + db.execSQL("CREATE INDEX notification_profile_allowed_members_recipient_index ON notification_profile_allowed_members (recipient_id)") + } +}