Skip to content

Schema Migration

You'll move an app's data into a real database, then change the tables and ship the update to a phone that already has the old version, without losing a row.

About

A key-value store carries you until the day you have two kinds of thing that point at each other, and then you want a database. Each platform ships one for free: SQLite under Room on Android, SwiftData or Core Data on iOS, sqflite or Drift in Flutter. The first version is easy. The skill is the second version, because every phone that already has your app holds the old tables, and new code opening an old file crashes on launch, which is the most common bad review on an update. So you build it, fill it with data you'd mind losing, change the schema, and update over the top.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
5
Takes
One weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Mobile Apps to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Build an app with two tables where one points at the other. Shopping lists and their items, trips and their stops, plants and their waterings. Use the platform's database, not a JSON file. Decide what happens to the children when a parent is deleted, and make it happen on purpose rather than by accident. Film adding three items to a parent and then deleting the parent. Video
  2. 2
    Install a release build and put fifty real rows in it. A release build, because the update later has to be a real update over the top of it. Type in data you'd mind losing, not test1 and test2. Take a photo of the app with the rows showing and the version number somewhere on screen. Photo
  3. 3
    Add a column and a third table, with a migration. A done flag on the child rows and a table of notes is enough. Write the migration by hand or let the framework generate it, but read what it does, and bump the schema version. The trap is deleting and recreating the database on a version change, which works on your phone and wipes everyone else's. Take a photo of the migration code. Photo
  4. 4
    Update the installed app over the top and show the old rows with the new column. Install the new build without uninstalling the old one. If it crashes on launch, that's the lesson: read the stack trace, fix the migration, reinstall the old version and try again. Film the first launch after the update, scrolling through the rows you typed in last time. Video
  5. 5
    Add a screen that answers a question across both tables. Items never ticked off, the trip with the most stops, the plant that's gone longest without water. This is what the key-value store couldn't do. Film the answer changing as you edit a row. Video