diff --git a/app/src/main/java/com/nextcloud/client/database/NextcloudDatabase.kt b/app/src/main/java/com/nextcloud/client/database/NextcloudDatabase.kt index 676da368baa0..556acc642266 100644 --- a/app/src/main/java/com/nextcloud/client/database/NextcloudDatabase.kt +++ b/app/src/main/java/com/nextcloud/client/database/NextcloudDatabase.kt @@ -92,7 +92,7 @@ abstract class NextcloudDatabase : RoomDatabase() { INSTANCE = Room .databaseBuilder(context, NextcloudDatabase::class.java, ProviderMeta.DB_NAME) .allowMainThreadQueries() - .addLegacyMigrations(clock) + .addLegacyMigrations(clock, context) .addMigrations(RoomMigration()) .addMigrations(Migration67to68()) .addMigrations(Migration70to71()) diff --git a/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigration.kt b/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigration.kt index abd77b7c734f..d8fe46585220 100644 --- a/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigration.kt +++ b/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigration.kt @@ -22,6 +22,7 @@ package com.nextcloud.client.database.migrations +import android.content.Context import androidx.room.RoomDatabase import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase @@ -36,12 +37,13 @@ private const val MIN_SUPPORTED_DB_VERSION = 24 class LegacyMigration( private val from: Int, private val to: Int, - private val clock: Clock + private val clock: Clock, + private val context: Context ) : Migration(from, to) { override fun migrate(database: SupportSQLiteDatabase) { - LegacyMigrationHelper(clock) - .onUpgrade(database, from, to) + LegacyMigrationHelper(clock, context) + .tryUpgrade(database, from, to) } } @@ -52,10 +54,11 @@ class LegacyMigration( */ @Suppress("ForEachOnRange") fun RoomDatabase.Builder.addLegacyMigrations( - clock: Clock + clock: Clock, + context: Context ): RoomDatabase.Builder { (MIN_SUPPORTED_DB_VERSION until NextcloudDatabase.FIRST_ROOM_DB_VERSION - 1) - .map { from -> LegacyMigration(from, from + 1, clock) } + .map { from -> LegacyMigration(from, from + 1, clock, context) } .forEach { migration -> this.addMigrations(migration) } return this } diff --git a/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigrationHelper.java b/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigrationHelper.java index a5b8b1098c5a..af248c2c207e 100644 --- a/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigrationHelper.java +++ b/app/src/main/java/com/nextcloud/client/database/migrations/LegacyMigrationHelper.java @@ -22,6 +22,7 @@ package com.nextcloud.client.database.migrations; +import android.app.ActivityManager; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteException; @@ -31,11 +32,11 @@ import com.owncloud.android.db.ProviderMeta; import com.owncloud.android.files.services.NameCollisionPolicy; import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.providers.FileContentProvider; import java.util.Locale; import androidx.sqlite.db.SupportSQLiteDatabase; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class LegacyMigrationHelper { @@ -52,12 +53,29 @@ public class LegacyMigrationHelper { private static final String UPGRADE_VERSION_MSG = "OUT of the ADD in onUpgrade; oldVersion == %d, newVersion == %d"; private final Clock clock; + private final Context context; - public LegacyMigrationHelper(Clock clock) { + public LegacyMigrationHelper(Clock clock, Context context) { this.clock = clock; + this.context = context; } - public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) { + public void tryUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) { + try { + upgrade(db, oldVersion, newVersion); + } catch (Throwable t) { + Log_OC.i(TAG, "Migration upgrade failed due to " + t); + clearStorage(); + } + } + + @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") + private void clearStorage() { + context.getCacheDir().delete(); + ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).clearApplicationUserData(); + } + + private void upgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) { Log_OC.i(TAG, "Entering in onUpgrade"); boolean upgraded = false;