buildMigrationRegistry function
Wires schema's version history into a SaveMigrationRegistry — one
stepMigrations entry per consecutive (fromVersion, toVersion) hop is
required (this compiler cannot safely synthesize field-transform logic
from a declarative schema alone). Throws RemoteSchemaCompilerException
if a hop's migrate function is missing; SaveMigrationRegistry's own
constructor still re-validates the resulting chain.
Implementation
SaveMigrationRegistry buildMigrationRegistry(
RemoteSchemaDef schema, {
required Map<int, Map<String, Object?> Function(Map<String, Object?> input)>
stepMigrations,
}) {
final steps = <SaveMigrationStep>[];
for (var i = 0; i < schema.versions.length - 1; i++) {
final from = schema.versions[i].version;
final to = schema.versions[i + 1].version;
final migrate = stepMigrations[from];
if (migrate == null) {
throw RemoteSchemaCompilerException(
'missing migrate function for version $from -> $to',
);
}
steps.add(
SaveMigrationStep(fromVersion: from, toVersion: to, migrate: migrate),
);
}
return SaveMigrationRegistry(
currentVersion: schema.current.version,
steps: steps,
);
}