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
SQLcommands to update the database
Roomapplies migrations, detailing what happens if your user has missed some app updates and is jumping several DB versions
fallbackToDestructiveMigration()does, and why that can be helpful during development