call method

Future<void> call(
  1. Database db,
  2. int version, [
  3. int toVersion = -1
])

Execute this migration plan.

Migrates the database db to the specified version. The signature of this method is designed to be compatible with the sqflite openDatabase() onCreate, onUpgrade, and onDowngrade parameters, and is thus somewhat oddly arranged.

If toVersion is omitted, then the database is assumed to migrate from version 0 to version. Otherwise, the database is assumed to migrate from version to toVersion.

When an error is thrown during execution of an Operation, it will be handled as specified by that Operation's MigrationErrorStrategy.

If for some reason the database should be migrated outside of the context of openDatabase, it may be done so "manually", e.g.

MigrationPlan myMigrationPlan = MigrationPlan({
  // your migrations
});
myMigrationPlan(db, 2, 5);  // manually run a migration from version 2 to 5

Implementation

Future<void> call(Database db, int version, [int toVersion = -1]) async {
  // If called from onCreate, toVersion is missing; fromVersion is actually toVersion
  final int trueFromVer = toVersion >= 0 ? version : -1;
  final int trueToVer = toVersion >= 0 ? toVersion : version;
  MigrationCourse course = _courseForVersions(trueFromVer, trueToVer);
  _log.fine(
      "Established db migration course from $trueFromVer to $trueToVer");
  try {
    return await course.execute(db);
  } catch (err) {
    int eVer = (err is MigrationOperationException) ? err.problemVersion : -1;
    dynamic eCause = (err is MigrationOperationException) ? err.cause : err;
    _log.info("Caught exception at version $eVer during migration: $eCause");

    // if exception, the sqlflite onCreate, onUpgrade, onDowngrade handlers
    // will do the equivalent of a rollback of entire the migration course
    throw DatabaseMigrationException(trueFromVer, trueToVer, eVer, eCause);
  }
}