revertLast method

Future<String?> revertLast()

Reverts the most recently applied migration (runs its down SQL) and returns its version, or null if nothing was applied.

Implementation

Future<String?> revertLast() async {
  await ensureTrackerTable();
  final applied = await appliedVersions();
  if (applied.isEmpty) return null;
  final version = applied.last;

  String? down;
  for (final migration in await source.discover()) {
    if (migration.version == version) {
      down = migration.down;
      break;
    }
  }

  await connection.transaction((tx) async {
    if (down case final sql? when sql.trim().isNotEmpty) {
      await tx.executeSql(sql);
    }
    await tx.execute(
      deleteFrom(SchemaMigrationsTable.table)
          .where(SchemaMigrationsTable.version.eq(version)),
    );
  });
  return version;
}