Skip to content

Commit

Permalink
Merge pull request #2023 from dimagi/fileReferencesFix
Browse files Browse the repository at this point in the history
Fixes bug in updating File path for an existing Form with new file path
  • Loading branch information
shubham1g5 authored Jul 23, 2018
2 parents 9479848 + a742719 commit 97b6e57
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void updateFilePath(SqlStorage<FormDefRecord> formDefRecordStorage, Strin
}

// Set new values now
mFormFilePath = newFilePath;
mFormMediaPath = getMediaPath(newFilePath);
formDefRecordStorage.write(this);
}
Expand Down
40 changes: 40 additions & 0 deletions app/src/org/commcare/models/database/app/AppDatabaseUpgrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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<FormDefRecord> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_";

Expand Down

0 comments on commit 97b6e57

Please sign in to comment.