plan method

SqliteMigrationPlan plan({
  1. required SchemaDocument from,
  2. required SchemaDocument to,
})

Builds a plan from two schema snapshots.

Implementation

SqliteMigrationPlan plan({
  required SchemaDocument from,
  required SchemaDocument to,
}) {
  from = from.withoutIgnored();
  to = to.withoutIgnored();
  final statements = <String>[];
  final warnings = <String>[];
  var requiresRebuild = false;

  final fromModels = {for (final model in from.models) model.name: model};

  for (final targetModel in to.models) {
    final sourceModel =
        fromModels.remove(targetModel.name) ??
        _removeModelByDatabaseName(fromModels, targetModel.databaseName);
    if (sourceModel == null) {
      statements.add(
        schemaApplier.createTableStatementForModel(targetModel, schema: to),
      );
      continue;
    }

    _planModelDiff(
      targetSchema: to,
      sourceModel: sourceModel,
      targetModel: targetModel,
      statements: statements,
      warnings: warnings,
      markRequiresRebuild: () => requiresRebuild = true,
    );
  }

  for (final removedModel in fromModels.values) {
    warnings.add(
      'Dropping model ${removedModel.name} requires manual migration.',
    );
  }

  _planImplicitManyToManyDiffs(
    from: from,
    to: to,
    statements: statements,
    warnings: warnings,
    markRequiresRebuild: () => requiresRebuild = true,
  );

  return SqliteMigrationPlan(
    statements: List<String>.unmodifiable(statements),
    warnings: List<String>.unmodifiable(warnings),
    requiresRebuild: requiresRebuild,
  );
}