Skip to content

Database Migration

Devrath edited this page Jun 11, 2021 · 3 revisions
  • Migration is done when we have an app deployed in the play store and all the apps that are downloaded and being used by users.
  • Now we modify a table or add a column in the table and publish a new version of the application.
  • When the existing users download the application, There is the possibility of the application getting crashed since the new data field can't be handled by the existing database.
  • Solution to this is to instruct the new version of the app to handle the change in database structure.
  • Increase the version code of the database.
  • Add .addMigrations(migration_2_3) to the database builder method.
  • We have an option .fallBackToDestrictiveMigration() that will clear the existing database of the application.
  • We extend the object with migration that takes a previous version to newer version name.
  • If we use JSON to be stored we can use Gson to convert back and forth in migrate function.
val migration_2_3 = object : Migration(2, 3) {
  override fun migrate(database: SupportSQLiteDatabase) {
    database.execSQL("ALTER TABLE Review ADD COLUMN lastUpdatedDate INTEGER NOT NULL DEFAULT 0")
  }
}