rollback method

Future<void> rollback({
  1. String? snapshotPath,
})

Restores the project from the latest (or a specific) snapshot.

Implementation

Future<void> rollback({String? snapshotPath}) async {
  final target = snapshotPath != null
      ? Directory(snapshotPath)
      : _latestSnapshot();

  if (target == null) {
    print('❌ No snapshots found. Run a migration first.');
    return;
  }

  final manifestFile = File('${target.path}/$_manifestName');
  if (!manifestFile.existsSync()) {
    print('❌ Snapshot manifest missing at ${target.path}');
    return;
  }

  final data =
      jsonDecode(manifestFile.readAsStringSync()) as Map<String, dynamic>;
  final files = data['files'] as Map<String, dynamic>;
  final ts = data['timestamp'] as String;

  print('⏪ Rolling back to snapshot from $ts...');

  int restored = 0;
  for (final entry in files.entries) {
    final dest = File('$projectPath/${entry.key}');
    final src = File(entry.value as String);
    if (src.existsSync()) {
      dest.parent.createSync(recursive: true);
      src.copySync(dest.path);
      restored++;
    }
  }

  print('✅ Rollback complete: $restored/${files.length} files restored.');
}