runMigrationSteps method

Future<void> runMigrationSteps({
  1. required int from,
  2. required int to,
  3. required MigrationStepWithVersion steps,
})

Allows customizing step-by-step migrations (like the OnUpgrade callback returned by the generated stepByStep function) by invoking the steps function for each intermediate schema version from from until to is reached.

This can be used to implement a custom OnUpgrade callback that runs additional checks before and after the migrations:

onUpgrade: (m, from, to) async {
 await customStatement('PRAGMA foreign_keys = OFF');

 await transaction(
   () => m.runMigrationSteps(
     from: from,
     to: to,
     steps: migrationSteps(
       from1To2: ...,
       ...
     ),
   ),
 );

 if (kDebugMode) {
   final wrongForeignKeys = await customSelect('PRAGMA foreign_key_check').get();
   assert(wrongForeignKeys.isEmpty, '${wrongForeignKeys.map((e) => e.data)}');
 }

 await customStatement('PRAGMA foreign_keys = ON;');
},

Here, the migrationSteps method is generated by drift with the drift_dev schema steps command. For details, see the documentation.

Implementation

Future<void> runMigrationSteps({
  required int from,
  required int to,
  required MigrationStepWithVersion steps,
}) {
  return VersionedSchema.runMigrationSteps(
      migrator: this, from: from, to: to, steps: steps);
}