Add foreign key reference to notification profile members.

This commit is contained in:
Michelle Tang
2025-06-11 10:40:13 -04:00
committed by GitHub
parent f80d5d54ca
commit a46e1a451f
3 changed files with 51 additions and 5 deletions

View File

@@ -52,7 +52,7 @@ class NotificationProfileTables(context: Context, databaseHelper: SignalDatabase
val CREATE_TABLE: Array<String> = arrayOf(NotificationProfileTable.CREATE_TABLE, NotificationProfileScheduleTable.CREATE_TABLE, NotificationProfileAllowedMembersTable.CREATE_TABLE)
@JvmField
val CREATE_INDEXES: Array<String> = arrayOf(NotificationProfileScheduleTable.CREATE_INDEX, NotificationProfileAllowedMembersTable.CREATE_INDEX)
val CREATE_INDEXES: Array<String> = 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 {

View File

@@ -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) {

View File

@@ -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)")
}
}