Skip to content

Commit

Permalink
Merge pull request #2820 from dimagi/copy-connect-v7-global-database-…
Browse files Browse the repository at this point in the history
…upgrades

Copy connect v7 global database upgrades
  • Loading branch information
avazirna authored Aug 22, 2024
2 parents 848eaa7 + d1a1832 commit 0c58c49
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,45 @@
import org.commcare.android.storage.framework.Persisted;
import org.commcare.models.framework.Persisting;
import org.commcare.modern.database.Table;
import org.commcare.modern.models.MetaField;

import kotlin.Metadata;

/**
* DB model for storing the encrypted/encoded Connect DB passphrase
*
* @author dviggiano
*/
@Table(ConnectKeyRecord.STORAGE_KEY)
public class ConnectKeyRecord extends Persisted {
public static final String STORAGE_KEY = "connect_key";

public static final String IS_LOCAL = "is_local";
@Persisting(1)
String userId;
@Persisting(2)
String encryptedPassphrase;

public ConnectKeyRecord() { }
public ConnectKeyRecord(String userId, String encryptedPassphrase) {
this.userId = userId;
@Persisting(2)
@MetaField(IS_LOCAL)
boolean isLocal;

public ConnectKeyRecord() {
}

public ConnectKeyRecord(String encryptedPassphrase, boolean isLocal) {
this.encryptedPassphrase = encryptedPassphrase;
this.isLocal = isLocal;
}

public String getEncryptedPassphrase() {
return encryptedPassphrase;
}
public void setEncryptedPassphrase(String passphrase) {
encryptedPassphrase = passphrase;
}
public boolean getIsLocal() {
return isLocal;
}

public String getUserID() { return userId; }
public String getEncryptedPassphrase() { return encryptedPassphrase; }
public static ConnectKeyRecord fromV6(ConnectKeyRecordV6 oldVersion) {
return new ConnectKeyRecord(oldVersion.getEncryptedPassphrase(), true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.commcare.android.database.global.models;

import org.commcare.android.storage.framework.Persisted;
import org.commcare.models.framework.Persisting;
import org.commcare.modern.database.Table;

/**
* DB model for storing the encrypted/encoded Connect DB passphrase
* Used up to global DB V6
*
* @author dviggiano
*/
@Table(ConnectKeyRecordV6.STORAGE_KEY)
public class ConnectKeyRecordV6 extends Persisted {
public static final String STORAGE_KEY = "connect_key";
@Persisting(1)
String encryptedPassphrase;

public ConnectKeyRecordV6() {
}

public ConnectKeyRecordV6(String encryptedPassphrase) {
this.encryptedPassphrase = encryptedPassphrase;
}

public String getEncryptedPassphrase() {
return encryptedPassphrase;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package org.commcare.models.database.global;

import android.content.Context;
Expand Down Expand Up @@ -34,8 +31,9 @@ public class DatabaseGlobalOpenHelper extends SQLiteOpenHelper {
* V.4 - Add table for storing force close log entries that occur outside of an active session
* V.5 - Add table for storing apps available for install
* V.6 - Add table for storing (encrypted) passphrase for ConnectId DB
* V.7 - Add is_local column to ConnectKeyRecord table (to store both local and server passphrase)
*/
private static final int GLOBAL_DB_VERSION = 6;
private static final int GLOBAL_DB_VERSION = 7;

private static final String GLOBAL_DB_LOCATOR = "database_global";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

import android.content.Context;

import java.io.File;

import net.sqlcipher.database.SQLiteDatabase;

import org.commcare.CommCareApplication;
import org.commcare.android.database.global.models.AppAvailableToInstall;
import org.commcare.android.database.global.models.ApplicationRecord;
import org.commcare.android.database.global.models.ApplicationRecordV1;
import org.commcare.android.database.global.models.ConnectKeyRecord;
import org.commcare.android.database.global.models.ConnectKeyRecordV6;
import org.commcare.android.logging.ForceCloseLogEntry;
import org.commcare.modern.database.TableBuilder;
import org.commcare.models.database.ConcreteAndroidDbHelper;
import org.commcare.models.database.DbUtil;
import org.commcare.models.database.MigrationException;
import org.commcare.models.database.SqlStorage;
import org.commcare.android.database.global.models.ApplicationRecord;
import org.commcare.android.database.global.models.ApplicationRecordV1;
import org.commcare.modern.database.TableBuilder;
import org.commcare.provider.ProviderUtils;
import org.javarosa.core.services.storage.Persistable;

import java.io.File;
import org.javarosa.core.services.storage.Persistable;

/**
* @author ctsims
Expand Down Expand Up @@ -56,6 +58,12 @@ public void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
oldVersion = 6;
}
}

if (oldVersion == 6) {
if (upgradeSixSeven(db)) {
oldVersion = 7;
}
}
}

private boolean upgradeOneTwo(SQLiteDatabase db, int oldVersion, int newVersion) {
Expand Down Expand Up @@ -124,7 +132,42 @@ private boolean upgradeFourFive(SQLiteDatabase db) {
}

private boolean upgradeFiveSix(SQLiteDatabase db) {
return addTableForNewModel(db, ConnectKeyRecord.STORAGE_KEY, new ConnectKeyRecord());
return addTableForNewModel(db, ConnectKeyRecord.STORAGE_KEY, new ConnectKeyRecordV6());
}

private boolean upgradeSixSeven(SQLiteDatabase db) {
db.beginTransaction();

//Migrate the old ConnectKeyRecord in storage to the new version including isLocal
try {
db.execSQL(DbUtil.addColumnToTable(
ConnectKeyRecord.STORAGE_KEY,
ConnectKeyRecord.IS_LOCAL,
"TEXT"));

SqlStorage<Persistable> oldStorage = new SqlStorage<>(
ConnectKeyRecordV6.STORAGE_KEY,
ConnectKeyRecordV6.class,
new ConcreteAndroidDbHelper(c, db));

SqlStorage<Persistable> newStorage = new SqlStorage<>(
ConnectKeyRecord.STORAGE_KEY,
ConnectKeyRecord.class,
new ConcreteAndroidDbHelper(c, db));

for (Persistable r : oldStorage) {
ConnectKeyRecordV6 oldRecord = (ConnectKeyRecordV6)r;
ConnectKeyRecord newRecord = ConnectKeyRecord.fromV6(oldRecord);
//set this new record to have same ID as the old one
newRecord.setID(oldRecord.getID());
newStorage.write(newRecord);
}

db.setTransactionSuccessful();
return true;
} finally {
db.endTransaction();
}
}

private static boolean addTableForNewModel(SQLiteDatabase db, String storageKey,
Expand All @@ -137,8 +180,6 @@ private static boolean addTableForNewModel(SQLiteDatabase db, String storageKey,

db.setTransactionSuccessful();
return true;
} catch (Exception e) {
return false;
} finally {
db.endTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public class FormStorageTest {
, "org.commcare.suite.model.EndpointAction"
, "org.commcare.suite.model.QueryGroup"
, "org.commcare.android.database.global.models.ConnectKeyRecord"
, "org.commcare.android.database.global.models.ConnectKeyRecordV6"
);


Expand Down

0 comments on commit 0c58c49

Please sign in to comment.