Search code examples
androidandroid-sqlitedatabase-migrationandroid-room

How Migrate Room database by destroying and reloading database from assets


I am using Android Room for my app database. I need to change the version from 1 to 2, and the correct .db is embedded in my app folder assets/databases/

I can either specify a migration method with addMigrations() or use fallbackToDestructiveMigration()

fallbackToDestructiveMigration() empty my database and I don't know how to populate it again from the one in my folder assets/databases/. Maybe can I specify a callback when fallbackToDestructiveMigration happens ?

If I add a migration method, there is too many differences between expected & found, plus I don't know how to set some COLUMN as "NOT NULL".

Expected: TableInfo{name='poi', columns={sound_path=Column{name='sound_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, name_FR=Column{name='name_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, text_FR=Column{name='text_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, address=Column{name='address', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, city=Column{name='city', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, text_EN=Column{name='text_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, video_path=Column{name='video_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, opening_hour=Column{name='opening_hour', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, img_360_paths=Column{name='img_360_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_EN=Column{name='open_schedule_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closed_days=Column{name='closed_days', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_FR=Column{name='open_schedule_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, category_id=Column{name='category_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, img_paths=Column{name='img_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closing_hour=Column{name='closing_hour', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, price=Column{name='price', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, game_path=Column{name='game_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, can_skip=Column{name='can_skip', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, closed_months=Column{name='closed_months', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, vr_path=Column{name='vr_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, name_EN=Column{name='name_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]} Found: 08-09 17:27:47.990 22583-22583/com.rendrsoftworks.vrlib E/AndroidRuntime: TableInfo{name='poi', columns={sound_path=Column{name='sound_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, name_FR=Column{name='name_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, text_FR=Column{name='text_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, address=Column{name='address', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, city=Column{name='city', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, text_EN=Column{name='text_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, video_path=Column{name='video_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, opening_hour=Column{name='opening_hour', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, img_360_paths=Column{name='img_360_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_EN=Column{name='open_schedule_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closed_days=Column{name='closed_days', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, open_schedule_FR=Column{name='open_schedule_FR', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, category_id=Column{name='category_id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, img_paths=Column{name='img_paths', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, closing_hour=Column{name='closing_hour', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, price=Column{name='price', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, game_path=Column{name='game_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, can_skip=Column{name='can_skip', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, closed_months=Column{name='closed_months', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, vr_path=Column{name='vr_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, name_EN=Column{name='name_EN', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}


Solution

  • I have faced the same issue. You don't have to run these methods fallbackToDestructiveMigration() or addMigrations(), at least not in the mentioned case. Simply just make the poi class same as db table and check the notations, for instance, your poi table as showing in the error message appears to have one different field which is in your poi class "can_skip" notNull = true whereas in your db table "can_skip" notNull = false so all what you have to do is to remove the annotation @NonNull it should be like this

    @ColumnInfo(name = "can_skip") // @NonNull remove this private int mCanSkip;.

    After that uninstall your app from the emulator or your phone and then rebuild and it will work fine.

    Helpers:

    RoomSQLiteDifferenceFinder: allows you to determine differences between db table and its class.

    DB Browser for SQLite: allows you to manage your db easily.