Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room Persistence Library: Weird Error during migration

I am scratching my head with this error. I couldn't find any answer so far. I have old database which I am migrating to Persistence Room library. However whenever I do migration, I am getting following error,

java.lang.IllegalStateException: Migration didn't properly handle. 

Code I am using is as follows:

@Entity(tableName = ROUTE_TABLE)
public class RouteData {

    static final String ROUTE_TABLE = "name";

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private int id;
    @ColumnInfo(name = "col1")
    private String col1;
    //Other columns with exactly same as 'col1' just different names
    //Getters and Setters
}

For migration,

Room.databaseBuilder(context.getApplicationContext(), MyData.class, DATABASE_NAME)
            .addMigrations(MIGRATION_LATEST)
            .fallbackToDestructiveMigration() 
            .build();

private static final Migration MIGRATION_LATEST = new Migration(9, 10) {
    @Override
    public void migrate(SupportSQLiteDatabase db) {
        //Log.i("Tag" , "Migration Started");
        //Migration logic
        //Log.i("Tag" , "Migration Ended");
    }
};

When I run program. I get "Migration Ended" log in my LogCat but when I am trying access database again, it is giving me following error.

 Caused by: java.lang.IllegalStateException: Migration didn't properly handle xxx.
                                                                              Expected:
                                                                             TableInfo{name='name', columns={col1=Column{name='col1', type='TEXT', notNull=false, primaryKeyPosition=0}, ....}, foreignKeys=[]}
                                                                              Found:
                                                                             TableInfo{name='name', columns={col1=Column{name='col1', type='INTEGER', notNull=false, primaryKeyPosition=0}, ....}, foreignKeys=[]}

I have no idea where where this "INTEGER" is coming. I tried uninstalling, invalidating cache and any other closely related solution. Any idea what is going on? It works perfectly fine if you freshly install app. Error is coming up only after migration. According to my log cat, all migration steps seems to be completed but it is still showing migration not handled properly.

like image 515
Dexter Avatar asked Oct 24 '17 14:10

Dexter


1 Answers

Thank you all for trying to help me, however I have finally found the answer after days of frustrating debugging. In my app manifest auto backup was on,

android:allowBackup="true"

So while trying out various databases, google was actually backing up my any newly created databases and their structure and restoring them automatically when I was reinstalling app. Hence I was getting this wired error. Once I switch of auto backup android:allowBackup="false" and reinstall app, I can test migration properly.

My suggestion for all developers in future encountering such problem, SWITCH OFF auto backup while development. You can switch it on once you have tested your migration.

like image 84
Dexter Avatar answered Oct 19 '22 11:10

Dexter