[Android] Change the media slave table primary key to a composite one to avoid insertion collision

Nicolas Pomepuy git at videolan.org
Tue Jul 11 12:46:16 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Jul 11 09:28:20 2023 +0200| [c25d9c287873cb6444af1b422177c7db8b8beac9] | committer: Duncan McNamara

Change the media slave table primary key to a composite one to avoid insertion collision

Fixes #1555

> https://code.videolan.org/videolan/vlc-android/commit/c25d9c287873cb6444af1b422177c7db8b8beac9
---

 .../java/org/videolan/vlc/database/MediaDatabase.kt    |  4 ++--
 .../main/java/org/videolan/vlc/database/Migrations.kt  | 18 ++++++++++++++++++
 .../main/java/org/videolan/vlc/mediadb/models/Slave.kt |  4 +---
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt b/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt
index 04b3a68732..6780022ff9 100644
--- a/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt
+++ b/application/mediadb/src/main/java/org/videolan/vlc/database/MediaDatabase.kt
@@ -33,7 +33,7 @@ import org.videolan.vlc.mediadb.models.*
 
 private const val DB_NAME = "vlc_database"
 
- at Database(entities = [ExternalSub::class, Slave::class, BrowserFav::class, CustomDirectory::class, Widget::class], version = 34, exportSchema = false)
+ at Database(entities = [ExternalSub::class, Slave::class, BrowserFav::class, CustomDirectory::class, Widget::class], version = 35, exportSchema = false)
 @TypeConverters(Converters::class)
 abstract class MediaDatabase: RoomDatabase() {
     abstract fun externalSubDao(): ExternalSubDao
@@ -55,7 +55,7 @@ private fun buildDatabase(context: Context) = Room.databaseBuilder(context.appli
                 migration_21_22, migration_22_23, migration_23_24, migration_24_25,
                 migration_25_26, migration_26_27, migration_27_28, migration_28_29,
                 migration_29_30, migration_30_31, migration_31_32, migration_32_33,
-                migration_33_34)
+                migration_33_34, migration_34_35)
         .addCallback(object : RoomDatabase.Callback() {
             override fun onCreate(db: SupportSQLiteDatabase) { if (!AndroidDevices.isTv) populateDB(context) }
         })
diff --git a/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt b/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt
index 74c0fb5bf6..39e1e2134d 100644
--- a/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt
+++ b/application/mediadb/src/main/java/org/videolan/vlc/database/Migrations.kt
@@ -236,6 +236,24 @@ val migration_33_34 = object:Migration(33, 34) {
     }
 }
 
+val migration_34_35 = object:Migration(34, 35) {
+    override fun migrate(database: SupportSQLiteDatabase) {
+        val slavesTableNameTemp =  "${SLAVES_TABLE_NAME}_TEMP"
+        database.execSQL("CREATE TABLE IF NOT EXISTS $slavesTableNameTemp (" +
+                "slave_media_mrl TEXT NOT NULL, " +
+                "slave_type INTEGER NOT NULL, " +
+                "slave_priority INTEGER NOT NULL, " +
+                "slave_uri TEXT NOT NULL, " +
+                "PRIMARY KEY (slave_media_mrl, slave_uri));")
+
+        database.execSQL("INSERT INTO $slavesTableNameTemp(slave_media_mrl, slave_type, slave_priority, slave_uri) SELECT slave_media_mrl, slave_type, slave_priority, slave_uri FROM $SLAVES_TABLE_NAME")
+
+        database.execSQL("DROP TABLE $SLAVES_TABLE_NAME")
+        database.execSQL("ALTER TABLE $slavesTableNameTemp RENAME TO $SLAVES_TABLE_NAME")
+
+    }
+}
+
 @OptIn(DelicateCoroutinesApi::class)
 fun populateDB(context: Context) = GlobalScope.launch(Dispatchers.IO) {
     val uris = listOf(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI,
diff --git a/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/Slave.kt b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/Slave.kt
index 8411472fab..3ba394555f 100644
--- a/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/Slave.kt
+++ b/application/mediadb/src/main/java/org/videolan/vlc/mediadb/models/Slave.kt
@@ -22,11 +22,9 @@ package org.videolan.vlc.mediadb.models
 
 import androidx.room.ColumnInfo
 import androidx.room.Entity
-import androidx.room.PrimaryKey
 
- at Entity(tableName = "SLAVES_table")
+ at Entity(tableName = "SLAVES_table", primaryKeys = ["slave_media_mrl", "slave_uri"])
 data class Slave (
-    @PrimaryKey
     @ColumnInfo(name = "slave_media_mrl")
     val mediaPath: String,
     @ColumnInfo(name = "slave_type")



More information about the Android mailing list