Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explained the need to support database downgrading #6366

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}