executeUpgrades method
Wraps the upgrade execution
Provide a sorted (oldest to newest version) map of version strings and executable methods in upgrades
(e.g. <String, Function>{'0.0.9': yourFunction}
).
All string / method pairs fitting into the upgrade range will be executed (minimal version exclusive, maximal version inclusive).
This method is async
to allow e.g. database operations. All upgrade methods will be executed async
in the given order.
Returns a list of executed version strings. An empty list means no upgrades were performed. A null
value is returned if the UpgradeWrapper wasn't
in a valid state during execution.
Implementation
Future<List<String>?> executeUpgrades(Map<String, Function> upgrades) async {
if (state == UpgradeState.upgrade) {
final upgradesCopy = {...upgrades};
final lastVersion = Version.parse(this.lastVersion!);
final currentVersion = Version.parse(this.currentVersion!);
final range =
VersionRange(min: lastVersion, max: currentVersion, includeMax: true);
upgradesCopy
.removeWhere((key, value) => !range.allows(Version.parse(key)));
await Future.forEach(
upgradesCopy.values, (Function upgrade) async => await upgrade());
return upgradesCopy.keys.toList();
} else if (state == UpgradeState.noUpgrade) {
return <String>[];
} else {
return null;
}
}