Handling updates and migrations in the Room database
FavoritePetsScreen
doesn’t have any functionality yet. We are going to add the functionality to favorite pets and update this information in the Room database. To achieve this, we need to do the following:
- Set up a Room schema directory.
- Add a new column to our
CatEntity
class to store the favorite status of the cat. - Add a new function to
CatDao
to update the favorite status of the cat. - Update our UIs with a favorite icon and, once clicked, update the favorite status of the cat. This means the ViewModel and repository classes will also be updated in the process.
Let’s get started with the steps:
- Let us start by setting up the schema directory. In our app level
build.gradle.kts
file, add the following code:ksp { arg("room.schemaLocation", "$projectDir/schemas") }
Do a Gradle sync and then build the project. This generates a
schema json
file...