Skip to content

Commit

Permalink
WIP - Fixing Migration Errors, created MigrationsTest
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Dec 14, 2023
1 parent 587801c commit 072b202
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 5 deletions.
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import com.github.spotbugs.snom.SpotBugsTask

import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort
import com.github.spotbugs.snom.SpotBugsTask

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
Expand Down Expand Up @@ -91,6 +92,10 @@ android {
}
}

sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}

testInstrumentationRunnerArgument "TEST_SERVER_URL", "${NC_TEST_SERVER_BASEURL}"
testInstrumentationRunnerArgument "TEST_SERVER_USERNAME", "${NC_TEST_SERVER_USERNAME}"
testInstrumentationRunnerArgument "TEST_SERVER_PASSWORD", "${NC_TEST_SERVER_PASSWORD}"
Expand Down Expand Up @@ -238,6 +243,7 @@ dependencies {
implementation "androidx.room:room-rxjava2:${roomVersion}"
kapt "androidx.room:room-compiler:${roomVersion}"
implementation "androidx.room:room-ktx:${roomVersion}"
androidTestImplementation "androidx.room:room-testing:2.6.1"

implementation "org.parceler:parceler-api:$parcelerVersion"
implementation 'eu.davidea:flexible-adapter:5.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Nextcloud Talk application
*
* @author Julius Linus
* Copyright (C) 2023 Julius Linus <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.nextcloud.talk.migrations

import androidx.room.Room
import androidx.room.testing.MigrationTestHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.platform.app.InstrumentationRegistry
import com.nextcloud.talk.data.source.local.Migrations
import com.nextcloud.talk.data.source.local.TalkDatabase
import org.junit.Rule
import org.junit.Test
import java.io.IOException


class MigrationsTest {

@get:Rule
val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
TalkDatabase::class.java.canonicalName!!,
FrameworkSQLiteOpenHelperFactory()
)

@Test
@Throws(IOException::class)
fun migrateAll() {
// Create earliest version of the database.
helper.createDatabase(TEST_DB, 8).apply {
close()
}

// Open latest version of the database. Room validates the schema
// once all migrations execute.
Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,
TalkDatabase::class.java,
TEST_DB
).addMigrations(*migrations).build().apply {
openHelper.writableDatabase.close()
}
}

companion object {
private const val TEST_DB = "migration-test"
private val migrations = arrayOf(
Migrations.MIGRATION_6_8,
Migrations.MIGRATION_7_8,
Migrations.MIGRATION_8_9,
Migrations.MIGRATION_9_10
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class MainActivity : BaseActivity(), ActionBarProvider {
} else {
if (!appPreferences.isDbRoomMigrated) {
appPreferences.isDbRoomMigrated = true
Log.d("Julius", "isDbRoomMigrated set to true")
}

userManager.users.subscribe(object : SingleObserver<List<User>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ object Migrations {
}
}

val MIGRATION_9_10 = object : Migration(9, 10) {
override fun migrate(db: SupportSQLiteDatabase) {
Log.i("Migrations", "Migrating 9 to 10")
migrateToTriplePrimaryKeyArbitraryStorage(db)
}
}

fun migrateToRoom(db: SupportSQLiteDatabase) {
db.execSQL(
"CREATE TABLE User_new (" +
Expand Down Expand Up @@ -124,4 +131,29 @@ object Migrations {
// Change the table name to the correct one
db.execSQL("ALTER TABLE ArbitraryStorage_dualPK RENAME TO ArbitraryStorage")
}

fun migrateToTriplePrimaryKeyArbitraryStorage(db: SupportSQLiteDatabase) {
db.execSQL(
"CREATE TABLE ArbitraryStorage_dualPK (" +
"accountIdentifier INTEGER NOT NULL, " +
"\"key\" TEXT NOT NULL, " +
"object TEXT, " +
"value TEXT, " +
"PRIMARY KEY(accountIdentifier, \"key\", object)" +
")"
)
// Copy the data
db.execSQL(
"INSERT INTO ArbitraryStorage_dualPK (" +
"accountIdentifier, \"key\", object, value) " +
"SELECT " +
"accountIdentifier, \"key\", object, value " +
"FROM ArbitraryStorage"
)
// Remove the old table
db.execSQL("DROP TABLE ArbitraryStorage")

// Change the table name to the correct one
db.execSQL("ALTER TABLE ArbitraryStorage_dualPK RENAME TO ArbitraryStorage")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import java.util.Locale

@Database(
entities = [UserEntity::class, ArbitraryStorageEntity::class],
version = 9,
version = 10,
exportSchema = true
)
@TypeConverters(
Expand Down Expand Up @@ -76,7 +76,7 @@ abstract class TalkDatabase : RoomDatabase() {
val passCharArray = context.getString(R.string.nc_talk_database_encryption_key).toCharArray()
val passphrase: ByteArray = SQLiteDatabase.getBytes(passCharArray)

val factory = if (appPreferences.isDbRoomMigrated) {
val factory = if (appPreferences.isDbRoomMigrated) { // Why isn't this set??? This should be set
Log.i(TAG, "No cipher migration needed")
SupportFactory(passphrase)
} else {
Expand All @@ -95,8 +95,13 @@ abstract class TalkDatabase : RoomDatabase() {
return Room
.databaseBuilder(context.applicationContext, TalkDatabase::class.java, dbName)
// NOTE: comment out openHelperFactory to view the database entries in Android Studio for debugging
.openHelperFactory(factory)
.addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8, Migrations.MIGRATION_8_9)
// .openHelperFactory(factory) // FIXME crashes when database Migration works
.addMigrations(
Migrations.MIGRATION_6_8,
Migrations.MIGRATION_7_8,
Migrations.MIGRATION_8_9,
Migrations.MIGRATION_9_10
)
.allowMainThreadQueries()
.addCallback(
object : RoomDatabase.Callback() {
Expand Down

0 comments on commit 072b202

Please sign in to comment.