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

Only select existing columns in instance database migration and make form and instance migrations follow recommended patterns #3250

Merged
merged 6 commits into from
Aug 1, 2019
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.odk.collect.android.database.helpers;

import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.utilities.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;

@RunWith(Parameterized.class)
public class InstancesDatabaseHelperTest {
@Parameterized.Parameter
public String description;

@Parameterized.Parameter(1)
public String dbFilename;

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
{"Downgrading from version with extra column drops that column", "instances_v7000_added_fakeColumn.db"},
{"Downgrading from version with missing column adds that column", "instances_v7000_removed_jrVersion.db"},

{"Upgrading from version with extra column drops that column", "instances_v4_real.db"},
{"Upgrading from version with missing column adds that column", "instances_v4_removed_jrVersion.db"}
});
}

private static final String DATABASE_PATH = Collect.METADATA_PATH + File.separator + InstancesDatabaseHelper.DATABASE_NAME;
private static final String TEMPORARY_EXTENSION = ".real";

@Before
public void saveRealDb() {
FileUtils.copyFile(new File(DATABASE_PATH), new File(DATABASE_PATH + TEMPORARY_EXTENSION));
}

@After
public void restoreRealDb() {
FileUtils.copyFile(new File(DATABASE_PATH + TEMPORARY_EXTENSION), new File(DATABASE_PATH));
}

@Test
public void testMigration() throws IOException {
writeDatabaseFile("database" + File.separator + dbFilename);
InstancesDatabaseHelper databaseHelper = new InstancesDatabaseHelper();
ensureMigrationAppliesFully(databaseHelper);

SQLiteDatabase db = databaseHelper.getReadableDatabase();
assertThat(db.getVersion(), is(InstancesDatabaseHelper.DATABASE_VERSION));

List<String> newColumnNames = InstancesDatabaseHelper.getInstancesColumnNames(db);

assertThat(newColumnNames, contains(InstancesDatabaseHelper.CURRENT_VERSION_COLUMN_NAMES));
}

private void writeDatabaseFile(String dbPath) throws IOException {
AssetManager assetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets();
try (InputStream input = assetManager.open(dbPath);
OutputStream output = new FileOutputStream(DATABASE_PATH)) {
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
}
}

/**
* Gets a read-only reference to the instances database and then immediately releases it.
*
* Without this, it appears that the migrations only get partially applied. It's not clear how
* this is possible since calls to onDowngrade and onUpgrade are wrapped in transactions. See
* discussion at https://github.com/opendatakit/collect/pull/3250#issuecomment-516439704
*/
private void ensureMigrationAppliesFully(InstancesDatabaseHelper databaseHelper) {
databaseHelper.getReadableDatabase().close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,7 @@ public void savingComplete(SaveResult saveResult) {
String message;
formController.getAuditEventLogger().logEvent(AuditEvent.AuditEventType.SAVE_ERROR, true);
if (saveResult.getSaveErrorMessage() != null) {
message = getString(R.string.data_saved_error) + ": "
message = getString(R.string.data_saved_error) + " "
+ saveResult.getSaveErrorMessage();
} else {
message = getString(R.string.data_saved_error);
Expand Down
Loading