瀏覽代碼

Add DB migration

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 1 年之前
父節點
當前提交
079e527f21

+ 32 - 0
app/src/main/java/com/nextcloud/talk/data/source/local/Migrations.kt

@@ -40,6 +40,13 @@ object Migrations {
         }
     }
 
+    val MIGRATION_8_9 = object : Migration(8, 9) {
+        override fun migrate(database: SupportSQLiteDatabase) {
+            Log.i("Migrations", "Migrating 8 to 9")
+            migrateToDualPrimaryKeyArbitraryStorage(database)
+        }
+    }
+
     fun migrateToRoom(database: SupportSQLiteDatabase) {
         database.execSQL(
             "CREATE TABLE User_new (" +
@@ -92,4 +99,29 @@ object Migrations {
         database.execSQL("ALTER TABLE User_new RENAME TO User")
         database.execSQL("ALTER TABLE ArbitraryStorage_new RENAME TO ArbitraryStorage")
     }
+
+    fun migrateToDualPrimaryKeyArbitraryStorage(database: SupportSQLiteDatabase) {
+        database.execSQL(
+            "CREATE TABLE ArbitraryStorage_dualPK (" +
+                "accountIdentifier INTEGER NOT NULL, " +
+                "\"key\" TEXT  NOT NULL, " +
+                "object TEXT, " +
+                "value TEXT, " +
+                "PRIMARY KEY(accountIdentifier, \"key\")" +
+                ")"
+        )
+        // Copy the data
+        database.execSQL(
+            "INSERT INTO ArbitraryStorage_dualPK (" +
+                "accountIdentifier, \"key\", object, value) " +
+                "SELECT " +
+                "accountIdentifier, \"key\", object, value " +
+                "FROM ArbitraryStorage"
+        )
+        // Remove the old table
+        database.execSQL("DROP TABLE ArbitraryStorage")
+
+        // Change the table name to the correct one
+        database.execSQL("ALTER TABLE ArbitraryStorage_dualPK RENAME TO ArbitraryStorage")
+    }
 }

+ 1 - 1
app/src/main/java/com/nextcloud/talk/data/source/local/TalkDatabase.kt

@@ -97,7 +97,7 @@ abstract class TalkDatabase : RoomDatabase() {
                 .databaseBuilder(context.applicationContext, TalkDatabase::class.java, dbName)
                 // comment out openHelperFactory to view the database entries in Android Studio for debugging
                 .openHelperFactory(factory)
-                .addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8)
+                .addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8, Migrations.MIGRATION_8_9)
                 .allowMainThreadQueries()
                 .addCallback(
                     object : RoomDatabase.Callback() {