[FIXED] Room migration testing MigrationTestHelper version


Well, i am trying to test my database migration. Unfortunatly something looks like wrong.

public class MigrationTest {
    private static final String TEST_DB = "migration-test";

    public MigrationTestHelper helper;

    public MigrationTest() {
        helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
                new FrameworkSQLiteOpenHelperFactory());

    public void migrateAll() throws IOException {
        // Create earliest version of the database.
        SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);

        // Open latest version of the database. Room will validate the schema
        // once all migrations execute.
        AppDatabase appDb = Room.databaseBuilder(

    // Array of all migrations
    private static final Migration[] ALL_MIGRATIONS = new Migration[]{MIGRATION_1_2};


Migration code.

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE mytable ADD COLUMN reference_code TEXT");


All is working fine with real migration but in junit test case i have the following error.

E/SQLiteLog: (1) duplicate column name: reference_code
E/TestRunner: failed: migrateAll(com.apps.MigrationTest)
E/TestRunner: ----- begin exception -----
E/TestRunner: android.database.sqlite.SQLiteException: duplicate column name: reference_code (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE mytable ADD COLUMN reference_code TEXT
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
        at a

As i understand, it looks like SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1); is creating the schema V2 of my database (and not the version 1).
As a result the new column is taggued as duplicate.

To fix it i have to rollback my version = 1 to @Database class and then start my junit test again.

Can anyone help me on it ?

I follow the google guide here : https://developer.android.com/training/data-storage/room/migrating-db-versions.html


Well, i finally found it. It looks like a wrong schema has been generated in my assets folder.

To fix the issue, here is what i did.

  1. Delete 1.json and 2.json files from the assets folder (each file contains the structure of the database version)
  2. Rollback to the version 1 database (in my code), build > make projet
  3. You will see the 1.json in your assets folder
  4. Made my changes, i mean adding my new column in my Table.java file
  5. Build > make projet
  6. You will see the 2.json in your assets folder
  7. Run the junit test, working now

Here is the difference bewteen my java object and database version 1 et 2

@Entity(tableName = "Table")
public class Table implements Parcelable {

    @ColumnInfo(name = COLUMN_REFERENCE_CODE)
    private String referenceCode;

Hope this will help.

Answered By – Zhar

Answer Checked By – Willingham (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published