From a742719c42f0ea68cad2ae29250e326f35cf423c Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Mon, 23 Jul 2018 14:24:36 +0530 Subject: [PATCH] Corrects corrupted file references, Fixes bug in FormDef::updateFilePath --- .../database/app/models/FormDefRecord.java | 1 + .../database/app/AppDatabaseUpgrader.java | 40 +++++++++++++++++++ .../database/app/DatabaseAppOpenHelper.java | 3 +- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/src/org/commcare/android/database/app/models/FormDefRecord.java b/app/src/org/commcare/android/database/app/models/FormDefRecord.java index de141e566d..656f206a40 100644 --- a/app/src/org/commcare/android/database/app/models/FormDefRecord.java +++ b/app/src/org/commcare/android/database/app/models/FormDefRecord.java @@ -138,6 +138,7 @@ public void updateFilePath(SqlStorage formDefRecordStorage, Strin } // Set new values now + mFormFilePath = newFilePath; mFormMediaPath = getMediaPath(newFilePath); formDefRecordStorage.write(this); } diff --git a/app/src/org/commcare/models/database/app/AppDatabaseUpgrader.java b/app/src/org/commcare/models/database/app/AppDatabaseUpgrader.java index 4aaed10c0c..9b1a34d12f 100755 --- a/app/src/org/commcare/models/database/app/AppDatabaseUpgrader.java +++ b/app/src/org/commcare/models/database/app/AppDatabaseUpgrader.java @@ -3,6 +3,7 @@ import android.content.Context; import android.database.Cursor; import android.net.Uri; +import android.text.TextUtils; import android.util.Log; import net.sqlcipher.database.SQLiteDatabase; @@ -24,9 +25,11 @@ import org.commcare.provider.FormsProviderAPI; import org.commcare.resources.model.Resource; import org.commcare.util.LogTypes; +import org.commcare.utils.GlobalConstants; import org.javarosa.core.services.Logger; import org.javarosa.core.util.externalizable.PrototypeFactory; +import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -101,6 +104,13 @@ public void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) { oldVersion = 10; } } + + if (oldVersion == 10) { + if (upgradeTenEleven(db)) { + oldVersion = 11; + } + } + //NOTE: If metadata changes are made to the Resource model, they need to be //managed by changing the TwoThree updater to maintain that metadata. } @@ -273,6 +283,36 @@ private boolean upgradeNineTen(SQLiteDatabase db) { return true; } + // Corrects 'update' file references for FormDef Records that didn't + // change to 'install' path because of an earlier bug + private boolean upgradeTenEleven(SQLiteDatabase db) { + db.beginTransaction(); + try { + SqlStorage formDefRecordStorage = new SqlStorage<>( + FormDefRecord.STORAGE_KEY, + FormDefRecord.class, + new ConcreteAndroidDbHelper(context, db)); + for (FormDefRecord formDefRecord : formDefRecordStorage) { + String filePath = formDefRecord.getFilePath(); + File formFile = new File(filePath); + + // update the path for the record if it points to a non existent upgrade path and corresponding install path exists + if (!formFile.exists() && filePath.contains(GlobalConstants.FILE_CC_UPGRADE)) { + String newFilePath = filePath.replace(GlobalConstants.FILE_CC_UPGRADE, GlobalConstants.FILE_CC_INSTALL + "/"); + if (new File(newFilePath).exists()) { + formDefRecord.updateFilePath(formDefRecordStorage, newFilePath); + } else { + Logger.log(LogTypes.SOFT_ASSERT, "File not found at both upgrade and install path for form " + formDefRecord.getJrFormId()); + } + } + } + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + return true; + } + // migrate formProvider entries to db private void migrateFormProvider(SQLiteDatabase db) { Cursor cursor = null; diff --git a/app/src/org/commcare/models/database/app/DatabaseAppOpenHelper.java b/app/src/org/commcare/models/database/app/DatabaseAppOpenHelper.java index e5ce7ccf24..6a35725205 100755 --- a/app/src/org/commcare/models/database/app/DatabaseAppOpenHelper.java +++ b/app/src/org/commcare/models/database/app/DatabaseAppOpenHelper.java @@ -36,8 +36,9 @@ public class DatabaseAppOpenHelper extends SQLiteOpenHelper { * V.8 - Add fields to UserKeyRecord to support PIN auth * V.9 - Adds FormRecord and Instance Record tables, XFormAndroidInstaller: contentUri -> formDefId * V.10 - No Change, Added because of incomplete resource table migration for v8 to v9 + * V.11 - No Change, Corrects FormDef references if corrupt (because of an earlier bug) */ - private static final int DB_VERSION_APP = 10; + private static final int DB_VERSION_APP = 11; private static final String DB_LOCATOR_PREF_APP = "database_app_";