diffSchemas function

List<MssqlSchemaDifference> diffSchemas({
  1. required List<MssqlTableSchema> expected,
  2. required List<MssqlTableSchema> actual,
})

Compares a schema snapshot against the live database.

The counterpart of generating offline: generation from a snapshot needs no server, and this is the step that says whether the snapshot is still true. Kept apart so a build that generates offline can still have a deploy step that checks.

Unlike the binding check, this compares the whole schema, so it can say which column changed, from what to what, and why it matters.

Implementation

List<MssqlSchemaDifference> diffSchemas({
  required List<MssqlTableSchema> expected,
  required List<MssqlTableSchema> actual,
}) {
  final live = <String, MssqlTableSchema>{
    for (final table in actual) table.qualifiedName.toLowerCase(): table,
  };
  final out = <MssqlSchemaDifference>[];
  for (final table in expected) {
    final current = live.remove(table.qualifiedName.toLowerCase());
    if (current == null) {
      out.add(
        MssqlSchemaDifference(
          table: table.qualifiedName,
          kind: MssqlDifferenceKind.tableMissing,
          severity: MssqlDifferenceSeverity.breaking,
          expected: 'a ${table.isView ? 'view' : 'table'}',
          actual: 'nothing',
          remedy:
              'It was dropped or renamed since the snapshot was taken; '
              'take a new snapshot and regenerate.',
        ),
      );
      continue;
    }
    out.addAll(_diffTable(table, current));
  }
  for (final table in live.values) {
    out.add(
      MssqlSchemaDifference(
        table: table.qualifiedName,
        kind: MssqlDifferenceKind.tableUngenerated,
        severity: MssqlDifferenceSeverity.benign,
        expected: 'nothing',
        actual: 'a ${table.isView ? 'view' : 'table'}',
        remedy:
            'The snapshot does not describe it; take a new snapshot to '
            'generate code for it.',
      ),
    );
  }
  return out;
}