Android Architecture Components: Room Migration

Android Architecture Components: Room Migration

Hi! This is the last post in the series of Architecture Components articles. This time I’d like to present you migration of a Room database.

If you haven’t read previous articles, you can do it here:

Android Architecture Components: Room — Introduction

Android Architecture Components: Room — Relationships

Android Architecture Components: Room — Custom Types

Either way, I encourage you to read this post. Let’s start!

Migration

Sometimes there’a s need to change our existing database schema. If we’ll add, update or delete some fields in the database and then run our application, we’ll see exception from Room:

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you’ve changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

Room will discover that it’s schema has changed and it‘s no more consistent with database version. Now if we’ll increment version number and run application once again, we’ll see another exception:

java.lang.IllegalStateException: A migration from 1 to 2 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables.

Now Room doesn’t know how to migrate database from version 1 to version 2. In this error Room suggest us two solutions:

  • drop and recreate the whole database
  • upgrade existing database schema to the newer version

First option is the easiest one, but it’s not the best for the users, as we’ll clear the whole database…LIKE WE DON’T CARE!!!

Despite I don’t recommend this solution, it’s possible with Room. To do it we need to add one line to the database builder:

But, If we’d like to have our application more user-friendly, we can provide migration of our database. With Room it’s also quite easy. To do it we need to declare migration in database builder:

To the Migration object we simply pass version numbers and SQL statement to make a migration.

Now, something what’s the biggest advantage of Room, auto-generation of SQL statements, is unfortunately something what may bother us upon database migration as we don’t have proper statements. Luckily, there’s a solution for that:

Thanks to this code fragment added to build.gradle file we’ll be able to get those auto-generated by Room SQL statements and use them to make a migration:

Files are located accordingly in app/schemas/your.package.name.RepoDatabase/ directory.

Conclusion

That’s all! I hope you liked that post. If you did — don’t forget subscribe👏 ! Remember to leave comments and questions down below 🙂

Happy Coding!

 

 

More From Android Study:

2 Replies to “Android Architecture Components: Room Migration”

  1. Hello, what if an existing app already has an SQLite db and I want to migrate it to Room without users loosing data. Db maintains same structure.

Leave a Reply

Your email address will not be published. Required fields are marked *