rollbackMigration method
Rolls back a migration using stored or on-disk schema snapshots.
Implementation
Future<PostgresqlRollbackResult> rollbackMigration({
required pg.SessionExecutor executor,
required String migrationsDirectory,
String? migrationName,
String? rollbackName,
bool allowWarnings = false,
}) async {
final activeHistory = await runner.loadActiveHistory(executor);
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(
executor: executor,
target: targetSchema,
targetMigrationName: targetRecord.name,
rollbackName: rollbackName,
allowWarnings: allowWarnings,
);
}