Skip to content

Commit

Permalink
Merge pull request #12160 from nextcloud/bugfix/owncloud-legacy-migra…
Browse files Browse the repository at this point in the history
…tion-crash

Bugfix Old Owncloud App to Nextcloud App Migration
  • Loading branch information
tobiasKaminsky authored Nov 15, 2023
2 parents 1f0b1ed + b369caa commit 6103234
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -52,10 +54,11 @@ class LegacyMigration(
*/
@Suppress("ForEachOnRange")
fun RoomDatabase.Builder<NextcloudDatabase>.addLegacyMigrations(
clock: Clock
clock: Clock,
context: Context
): RoomDatabase.Builder<NextcloudDatabase> {
(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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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;

Expand Down

0 comments on commit 6103234

Please sign in to comment.