Naveen Kumar
Naveen Kumar

Reputation: 86

Room: java.lang.IllegalStateException: Migration didn't properly for existing Database

Room version used:- 1.1.1-rc1

I had an existing database which is implemented by ormlite. I am trying to migrate to Room with the existing sqlite file generated by ormlite.

When we used long datatype for table column in ormlite it gets coverted to BIGINT in sqlite. so the schema created contains column which is of type BIGINT which is unknown type for room.When I try to upgrade the app with Room I am getting migration exception.

java.lang.IllegalStateException: Migration didn't properly

Process: com.sample.playground, PID: 5587
java.lang.IllegalStateException: Migration didn't properly handle SampleReports(com.sample.playground.model.SampleReport
 Expected:
TableInfo{name='SampleReports', columns={timeslot=Column{name='timeslot', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, environment=Column{name='environment', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, trigger=Column{name='trigger', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, imeisvSvn=Column{name='imeisvSvn', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='SampleReports', columns={environment=Column{name='environment', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, timeslot=Column{name='timeslot', type='BIGINT', affinity='3', notNull=true, primaryKeyPosition=0}, trigger=Column{name='trigger', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='BIGINT', affinity='3', notNull=true, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
    at com.sample.playground.model.SampleReport.SampleReportRoomDbHelper_Impl$1.validateMigration(SampleReportRoomDbHelper_Impl.java:77)
    at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:87)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:133)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
    at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:233)
    at com.sample.playground.model.SampleReportDao_Impl.getReports(SampleReportDao_Impl.java:300)

Tried with TypeConverter using BigInteger still it didn't worked.

Upvotes: 3

Views: 2505

Answers (2)

HRankit
HRankit

Reputation: 481

Check out this answer https://stackoverflow.com/a/52127819/7433710 This error is caused as the datatypes do not match or the most common Integer not null not given in earlier Sqlite table and you try to initialize by adding an int variable. Do make sure to add NOT NULL to all the int columns.

Check the difference which I got using your logcat output using the link below. Not enough reputation to post images :(

Comparison using your logcat output

Full Image of the difference

Upvotes: 0

MikeT
MikeT

Reputation: 57063

I think you need to convert the table accordingly by overriding the migrate method so that it - creates an intermediate table based the expected columns, - copies the data to the intermediate table, - drops the original table and - then changes the name of the intermediate table to the original name

e.g based upon :-

DROP TABLE IF EXISTS SampleReports_intermediate;
CREATE TABLE IF NOT EXISTS SampleReports_intermediate (
    timeslot INTEGER NOT NULL, 
    environment TEXT NOT NULL,
    timestamp INTEGER NOT NULL,
    `trigger` INTEGER NOT NULL,
    imeisvSvn TEXT,
    _id INTEGER PRIMARY KEY
);
INSERT INTO SampleReports_intermediate (environment, timeslot, `trigger`, timestamp, _id)
    SELECT environment, timeslot,`trigger`, timestamp, _id FROM SampleReports
;
DROP TABLE IF EXISTS SampleReports;
ALTER TABLE SampleReports_intermediate RENAME TO SampleReports;

If you aren't using room 1.1 try using that as that has :-

Room 1.1 added support for Sqlite types that were not in Room

Room Database Migration didn't properly handle conversion

Upvotes: 1

Related Questions