Sign in Go Pro

Storing Data in Android with Room

Database Migrations

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson




In this lesson, we'll discuss what database Migrations are, and how you can use them to safely make changes to your database schema.

When you make a change to your database, you'll need to tell Room how to handle this change in the form of a database Migration. A migration ensures your user's data is preserved while updating from the old database version to the new one.

In this lesson, we'll add a new field to an entity, and detail how to migrate the database so that the new field can be used without losing any of our database data.

Summary of Content:

  • Why database migrations are required
  • What the default behavior is if you update the database without providing a migration
  • Incrementing the database version number
  • What this error message means, and how to fix it:
    • "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."
  • Defining a database migration
  • Registering a migration with Room
  • Providing SQL commands to update the database
  • How Room applies migrations, detailing what happens if your user has missed some app updates and is jumping several DB versions
  • What fallbackToDestructiveMigration() does, and why that can be helpful during development





Excellent course! Thank you!

Our pleasure! Glad you're enjoying it! ☺️


Lessons in Storing Data in Android with Room