rollbackMigration method

SqliteRollbackResult rollbackMigration({
  1. required Database database,
  2. required String migrationsDirectory,
  3. String? migrationName,
  4. String? rollbackName,
  5. bool allowWarnings = false,
})

Rolls back a migration using stored or on-disk schema snapshots.

Implementation

SqliteRollbackResult rollbackMigration({
  required sqlite.Database database,
  required String migrationsDirectory,
  String? migrationName,
  String? rollbackName,
  bool allowWarnings = false,
}) {
  final activeHistory = runner.loadActiveHistory(database);
  final targetRecord = resolveRollbackTarget(
    activeHistory: activeHistory,
    migrationName: migrationName,
    migrationNameOf: (record) => record.name,
  );

  final beforeSchemaFile = File(
    '$migrationsDirectory${Platform.pathSeparator}${targetRecord.name}'
    '${Platform.pathSeparator}before.prisma',
  );
  final snapshotSource = beforeSchemaFile.existsSync()
      ? beforeSchemaFile.readAsStringSync()
      : targetRecord.beforeSchema;
  if (snapshotSource == null || snapshotSource.trim().isEmpty) {
    throw StateError(
      'Rollback snapshot not found on disk and not stored in database history for ${targetRecord.name}.',
    );
  }

  final targetSchema = const SchemaParser().parse(snapshotSource);

  return runner.rollbackToSchema(
    database: database,
    target: targetSchema,
    targetMigrationName: targetRecord.name,
    rollbackName: rollbackName,
    allowWarnings: allowWarnings,
  );
}