Using sqflite plugin

The most common plugin to use for SQLite in flutter is sqflite. It provides callbacks to facilitate migrations, but the actual migration implementation is up to the user. If you take a look at the signature of the method openDatabase , you can spot an onCreate and an onUpgrade option.

Using onCreate and onUpgrade

In order to facilitate schema migration you should provide appropriate callbacks for onCreate (schema initialisation) and onUpgrade (schema migration) options. Much like so:

Binding the version

This is all straight forward up to now, but what about the version option? Well, this option is the one controlling when and if the migration will run. So, you have to put extra attention into ensuring it always has the correct number. A nice trick you can do is to bind this number with the number of migration scripts you have.

Let’s say you have the following transition from migrationScripts1 to migrationScripts2 .

Obviously, you would like to run all 3 new migration scripts that have been added. If you bind the version to the number of migration scripts, say n , and given that the version cannot be less than 1, you would like to get from version 1 to version 1 + n .

version: migrationScripts.length + 1

But what if you want to transition from following migrationScript1 to migrationScript2 ?

In this situation, you want to migrate from version 2 to version 4. This is where the oldVersion variable in the onUpgrade callback comes to play. So, with some index manipulation, you can end up to something like the following.

Using sqlite_migration library

I have used the above pattern way too many times so I decided to create a library out of it. The sqflite_migration library was created for my own sanity and it basically abstracts away all sqflite migration details into a migration layer, which I think is straightforward to use.

You just have to use the `openDatabaseWithMigration` method and pass the migration configuration.

As you can see using sqflite_migration library simplifies things a lot, as you do not have to deal with index manipulation and script execution yourself.