plan method

Future<DVSchemaPlan> plan(
  1. Iterable<DVSchemaChange> changes,
  2. DVSchemaClassifier? classifier, {
  3. Map<String, int> rows = const <String, int>{},
})

Plans changes with the classification classifier gives.

A null classifier is a server nothing here knows the rules for, and every change is then blocking. rows is the row count per table, from a snapshot, carried onto each step.

Implementation

Future<DVSchemaPlan> plan(
  Iterable<DVSchemaChange> changes,
  DVSchemaClassifier? classifier, {
  Map<String, int> rows = const <String, int>{},
}) async {
  final List<DVSchemaPlanStep> steps = <DVSchemaPlanStep>[];
  final List<DVSchemaFinding> findings = <DVSchemaFinding>[];

  Future<DVSchemaChangeClass?> ask(DVSchemaChange change) async =>
      classifier == null ? null : await classifier.classify(change);

  for (final DVSchemaChange change in changes) {
    final DVSchemaChangeClass? given = await ask(change);
    if (given == null) {
      findings.add(
        DVSchemaFinding(
          'DV-SCHEMA-006',
          'The adapter cannot classify "$change", so it is treated as '
              'blocking.',
          change: change,
        ),
      );
    }
    final DVSchemaChangeClass changeClass =
        given ?? DVSchemaChangeClass.blocking;

    DVExpandContractPlan? safe;
    if (changeClass == DVSchemaChangeClass.blocking) {
      safe = await _expandContract(change, ask);
      if (safe != null) {
        findings.add(
          DVSchemaFinding(
            'DV-SCHEMA-001',
            '"$change" blocks readers or writers, and an expand/contract '
                'plan exists for it; the plan runs instead.',
            change: change,
          ),
        );
      }
    }

    steps.add(
      DVSchemaPlanStep(
        change: change,
        changeClass: changeClass,
        classifiedByAdapter: given != null,
        expandContract: safe,
        rows: rows[change.table],
      ),
    );
  }
  return DVSchemaPlan(
    steps: List<DVSchemaPlanStep>.unmodifiable(steps),
    findings: List<DVSchemaFinding>.unmodifiable(findings),
  );
}