packages/expo-updates/guides/migrations.md
Last updated: 2022-10-13
This document provides instructions for adding migrations to expo-updates' local SQLite db schema.
It's critical to get these types of migrations right, because they will take place on thousands of users' devices over which we have no centralized control if something goes wrong.
The Room library we use on Android provides a mechanism for migrations. Documentation here.
IMPORTANT NOTE: we are now using a version of Room that supports autogenerated migrations. You can try to do this following the official guide, but note that all the existing migrations were written before this feature was added, so your process and code will be somewhat different than existing manual migrations / the below steps. In either case, it's important to still go through steps 7-11 below to add tests.
On Android, we don't have control over the SQLite schema at a language level; it's auto-generated by Room using the entity classes that are included in UpdatesDatabase.
To add a manual migration, use the following steps (automatic migrations will likely differ in steps 4-5):
@Database version parameter in UpdatesDatabase.kt. Room will now fall back to a destructive migration by default.getInstance.@Database exportSchema to true in UpdatesDatabase.kt, and uncomment these lines in android/build.gradle. Run a build of some project that uses expo-updates, like Android Expo Go.This PR can be used as a reference for the finished product.
On iOS we use the sqlite3 library directly rather than interacting with SQLite through a wrapper. We've rolled our own migration system to roughly match the functionality Room provides on Android, which means there isn't any documentation for it besides the following.
Note that we use the filename of the database to determine the schema version number.
To add a migration, use the following steps:
EXUpdatesDatabaseLatestFilename variable.
EXUpdatesDatabase if, for instance, you added new NOT NULL columns to a table. Unit tests should catch this.EXUpdatesDatabaseMigration protocol, following the patterns in existing migrations.
NO from the runMigrationOnDatabase method anytime there are unrecoverable errors; if NO is returned then expo-updates will fall back to a destructive migration. This is better than a partially migrated or corrupt DB!filename.EXUpdatesDatabaseMigrationRegistry.EXUpdatesDatabaseInitialization class will do this for you.