Skip to content
tezvyn:
Menu

Drift Schema Migrations: Evolving Your Database Safely

Source: drift.simonbinder.euMediumHow cards are made

Think of schema migrations as a recipe for upgrading your app's database from an old version to a new one. You'll use them whenever you add a table or column in Drift. The footgun is forgetting to bump schemaVersion; your migration won't run.

Why it exists

When you ship an app, users have a database with a specific structure, or schema. If you update the app and change that structure (e.g., add a user_bio column), the app will crash for existing users unless you provide instructions on how to migrate their old database to the new schema. Schema migrations are those instructions, ensuring updates don't break the app for your existing user base.

The mental model

A schema migration is like the instructions in a flat-pack furniture box. Your app knows how to build the database from scratch (the final picture), but for users who have an older version, it needs a step-by-step guide to transform what they have into the new design without throwing away all the contents. Drift's make-migrations tool is a service that compares your old and new database designs and pre-writes most of these instructions for you.

How it works

First, you configure your build.yaml to point to your database file. You run dart run drift_dev make-migrations to save a snapshot of your current schema. Then, you modify your Dart table definitions. After your changes, you must increment the schemaVersion integer in your database class and run make-migrations again. This prompts Drift to compare the new schema with the old one and generate a .steps.dart file. You use the helpers in this file, like stepByStep, inside your database's MigrationStrategy to define the upgrade logic, such as m.createTable(...) or m.addColumn(...).

When to use it

Use migrations for any change to your database schema after your app's initial release. This includes adding, renaming, or removing tables or columns, or changing column constraints. It's a mandatory practice for any production Flutter or Dart app using Drift to ensure smooth updates. The make-migrations command is the recommended, tool-assisted approach.

When not to use it

During very early development before any users have the app, you might find it faster to just clear app data and start fresh instead of writing a migration for every small tweak. However, as soon as your app is in the hands of testers or users, you must use migrations. Migrations are for schema changes, not for modifying application data (e.g., correcting a typo in a user's name); use regular update statements for that.

One canonical example

After adding a groups table to your Dart code, you bump schemaVersion from 1 to 2 in your MyDatabase class and run make-migrations. Drift generates a helper. You then implement the onUpgrade strategy: MigrationStrategy(onUpgrade: stepByStep(from1To2: (m, schema) async { await m.createTable(schema.groups); })). Here, m is the Migrator that executes commands, and schema is a type-safe representation of the tables as they exist in version 2, letting you create the new table correctly.

Interview question

Which action is a critical oversight that would prevent a Drift schema migration from executing the necessary upgrade steps for existing users?

  • a.Failing to increment the schemaVersion in the database class.Correct
  • b.Not including m.createTable(...) or m.addColumn(...) in the onUpgrade strategy.
  • c.Using the migration to update specific user data, such as a username.
  • d.Not running dart run drift_dev make-migrations after modifying table definitions.
Why?

The card explicitly states that 'The footgun is forgetting to bump schemaVersion; your migration won't run.' This directly prevents the database from recognizing the need for an upgrade and executing the corresponding steps. Other options describe incomplete migrations, incorrect usage, or issues with step generation, but not the fundamental prevention of the migration's execution.

Just read this? Test yourself on what you have been reading.

Read the original → drift.simonbinder.eu

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles