migrateSettings function
Run migrations on settings data.
Implementation
Future<MigrationResult> migrateSettings(Map<String, dynamic> data) async {
final fromVersion = (data['configVersion'] as int?) ?? 1;
if (fromVersion >= currentConfigVersion) {
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 settingsMigrations) {
if (migration.fromVersion >= fromVersion &&
migration.toVersion <= currentConfigVersion) {
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['configVersion'] as int?) ?? currentConfigVersion,
migrationsApplied: applied,
errors: errors,
);
}