Android Room Migrations

Talking about my experience with Room db migrations

Android Room Migrations

I want to take the time to talk about Room migrations. I've kinda used Room but in the quest to get better at tooling I thought an app with Room would be the perfect "entry" test.

When I was first building I had .fallbackMigration which deletes your entire table on update. I was confused because that's what I've always used. Not to self, what you default to is not always right. Anyway insert migrations. I had never written a migration before and I thought Google docs would get me through. That was also a bad assumption.

Getting the first REAL green check took ~8hrs. What was wrong? Glad you asked. I was able to somehow google to get a base test going. By base I mean no assertions. It was showing green which is cool. What I discovered is that because we close the db after the test I couldn't prove a thing. How did I know what I inserted matched the new schema?

Well I don't know how but I found this really good write up to pair my migration test with assertions. I would like to stop here and say if you are seeing some weird errors it's not the dao line (my original thought), but you need to restart Android Studios a few times using invalidate and restart. After a few more hours of frustration and restarts I finally say a wonderful red assertion fail. It was mostly a fail because I switched the actual and expected values. 😅

So suggestions so you don't get stuck:
1. Read the google docs. I think understanding the overall structure really helps.

2. Read the article by Jeremy. I think this is a great breakdown by them especially if your new to testing.

3. High Level test sqlite statements here. I was originally trying to make sql statements work (some commands look the same) before I realized that mental assumption was breaking things.

4. Use the database inspector and alter 1 line in your db so that you get the proper sql statements for migration. You have to have the app running to do this but its super helpful. I was able to see the steps to one line and then I just copy and pasted where needed.


I'm planning to write more about my testing journey as this is most interesting to me. If there is anything I should check out let me know.