Skip to content

Commit

Permalink
Merge pull request #6366 from grzesiek2010/COLLECT-6315
Browse files Browse the repository at this point in the history
Explained the need to support database downgrading
  • Loading branch information
seadowg authored Aug 28, 2024
2 parents 9d5e3ae + aed1892 commit 9493502
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,4 @@ private class EntitiesDatabaseMigrator :
}

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int) = Unit

override fun onDowngrade(db: SQLiteDatabase?) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion) throws SQLException {
}
}

public void onDowngrade(SQLiteDatabase db) throws SQLException {
SQLiteUtils.dropTable(db, FORMS_TABLE_NAME);
createFormsTableV13(db);
}

private void upgradeToVersion2(SQLiteDatabase db) {
SQLiteUtils.dropTable(db, FORMS_TABLE_NAME);
onCreate(db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion) {
}
}

public void onDowngrade(SQLiteDatabase db) {
SQLiteUtils.dropTable(db, INSTANCES_TABLE_NAME);
createInstancesTableV8(db);
}

private void upgradeToVersion2(SQLiteDatabase db) {
if (!doesColumnExist(db, INSTANCES_TABLE_NAME, CAN_EDIT_WHEN_COMPLETE)) {
SQLiteUtils.addColumn(db, INSTANCES_TABLE_NAME, CAN_EDIT_WHEN_COMPLETE, "text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.odk.collect.android.database.savepoints.DatabaseSavepointsColumns.INS
import org.odk.collect.android.database.savepoints.DatabaseSavepointsColumns.INSTANCE_FILE_PATH
import org.odk.collect.android.database.savepoints.DatabaseSavepointsColumns.SAVEPOINT_FILE_PATH
import org.odk.collect.db.sqlite.DatabaseMigrator
import org.odk.collect.db.sqlite.SQLiteUtils

class SavepointsDatabaseMigrator :
DatabaseMigrator {
Expand All @@ -25,11 +24,6 @@ class SavepointsDatabaseMigrator :
}
}

override fun onDowngrade(db: SQLiteDatabase) {
SQLiteUtils.dropTable(db, SAVEPOINTS_TABLE_NAME)
createSavepointsTableV1(db)
}

private fun createSavepointsTableV1(db: SQLiteDatabase) {
db.execSQL(
"CREATE TABLE IF NOT EXISTS $SAVEPOINTS_TABLE_NAME (" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.database.forms.FormDatabaseMigrator;
import org.odk.collect.db.sqlite.SQLiteUtils;

import java.util.List;

Expand Down Expand Up @@ -309,59 +308,6 @@ public void onUpgrade_fromVersion7() {
}
}

@Test
public void onDowngrade_fromVersionWithExtraColumn() {
FormDatabaseMigrator formDatabaseMigrator = new FormDatabaseMigrator();
formDatabaseMigrator.onCreate(database);
SQLiteUtils.addColumn(database, FORMS_TABLE_NAME, "new_column", "text");
ContentValues contentValues = createVersion8Form();
contentValues.put("new_column", "blah");
database.insert(FORMS_TABLE_NAME, null, contentValues);

formDatabaseMigrator.onDowngrade(database);

try (Cursor cursor = database.rawQuery("SELECT * FROM " + FORMS_TABLE_NAME + ";", new String[]{})) {
assertThat(cursor.getColumnCount(), is(18));
assertThat(cursor.getCount(), is(0));
assertThat(asList(cursor.getColumnNames()), is(CURRENT_VERSION_COLUMNS));
}
}

@Test
public void onDowngrade_fromVersionWithMissingColumn() {
// Create form table with out JR Cache column
FormDatabaseMigrator formDatabaseMigrator = new FormDatabaseMigrator();
database.execSQL("CREATE TABLE IF NOT EXISTS " + FORMS_TABLE_NAME + " ("
+ _ID + " integer primary key, "
+ DISPLAY_NAME + " text not null, "
+ DESCRIPTION + " text, "
+ JR_FORM_ID + " text not null, "
+ JR_VERSION + " text, "
+ MD5_HASH + " text not null, "
+ DATE + " integer not null, "
+ FORM_MEDIA_PATH + " text not null, "
+ FORM_FILE_PATH + " text not null, "
+ LANGUAGE + " text, "
+ SUBMISSION_URI + " text, "
+ BASE64_RSA_PUBLIC_KEY + " text, "
+ AUTO_SEND + " text, "
+ AUTO_DELETE + " text, "
+ LAST_DETECTED_FORM_VERSION_HASH + " text, "
+ GEOMETRY_XPATH + " text);");

ContentValues contentValues = createVersion8Form();
contentValues.remove(JRCACHE_FILE_PATH);
database.insert(FORMS_TABLE_NAME, null, contentValues);

formDatabaseMigrator.onDowngrade(database);

try (Cursor cursor = database.rawQuery("SELECT * FROM " + FORMS_TABLE_NAME + ";", new String[]{})) {
assertThat(cursor.getColumnCount(), is(18));
assertThat(cursor.getCount(), is(0));
assertThat(asList(cursor.getColumnNames()), is(CURRENT_VERSION_COLUMNS));
}
}

private ContentValues createVersion8Form() {
ContentValues contentValues = new ContentValues();
contentValues.put(DISPLAY_NAME, "DisplayName");
Expand Down
12 changes: 1 addition & 11 deletions db/src/main/java/org/odk/collect/db/sqlite/DatabaseConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ open class DatabaseConnection @JvmOverloads constructor(
}

/**
* [SQLiteOpenHelper] that delegates `onCreate`, `onUpdate`, `onDowngrade` to a [DatabaseMigrator].
* [SQLiteOpenHelper] that delegates `onCreate`, `onUpdate` to a [DatabaseMigrator].
*/
private class DatabaseMigratorSQLiteOpenHelper(
context: Context,
Expand All @@ -120,14 +120,4 @@ private class DatabaseMigratorSQLiteOpenHelper(
newVersion
)
}

override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
Timber.i("Downgrading database from version %d to %d", oldVersion, newVersion)
databaseMigrator.onDowngrade(db)
Timber.i(
"Downgrading database from %d to %d completed with success.",
oldVersion,
newVersion
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ public interface DatabaseMigrator {
void onCreate(SQLiteDatabase db);

void onUpgrade(SQLiteDatabase db, int oldVersion);

void onDowngrade(SQLiteDatabase db);
}

0 comments on commit 9493502

Please sign in to comment.