migrateSession function

Future<MigrationResult> migrateSession(
  1. Map<String, dynamic> data
)

Run migrations on session data.

Implementation

Future<MigrationResult> migrateSession(Map<String, dynamic> data) async {
  final fromVersion = (data['sessionVersion'] as int?) ?? 1;
  if (fromVersion >= currentSessionVersion) {
    return MigrationResult(
      success: true,
      fromVersion: fromVersion,
      toVersion: fromVersion,
    );
  }

  var current = Map<String, dynamic>.from(data);
  final applied = <String>[];
  final errors = <String>[];

  for (final migration in sessionMigrations) {
    if (migration.fromVersion >= fromVersion &&
        migration.toVersion <= currentSessionVersion) {
      try {
        current = await migration.migrate(current);
        applied.add(
          'v${migration.fromVersion}→v${migration.toVersion}: ${migration.description}',
        );
      } catch (e) {
        errors.add(
          'v${migration.fromVersion}→v${migration.toVersion} failed: $e',
        );
        break;
      }
    }
  }

  return MigrationResult(
    success: errors.isEmpty,
    fromVersion: fromVersion,
    toVersion: (current['sessionVersion'] as int?) ?? currentSessionVersion,
    migrationsApplied: applied,
    errors: errors,
  );
}