buildSnapshot function

SchemaSnapshot buildSnapshot(
  1. List<Schema> schemas, {
  2. required SqlDialect dialect,
  3. String? dialectName,
})

Describes every table and index reachable from schemas.

schemas must be listed explicitly: a table registers itself when its top-level final is first read, and Dart initialises those lazily, so importing a schema library constructs nothing.

dialectName selects which tables count and defaults to the dialect's own SqlDialect.name, pass it explicitly only when the tables carry a different tag than the rendering dialect.

Implementation

SchemaSnapshot buildSnapshot(
  List<Schema<dynamic>> schemas, {
  required SqlDialect dialect,
  String? dialectName,
}) {
  dialectName ??= dialect.name;
  final tables = <String, TableSnapshot>{};
  final indexes = <String, IndexSnapshot>{};

  for (final schema in schemas) {
    final table = schema.$;
    // An alias is a view onto another table, never its own definition.
    if (table.alias != null) continue;
    if (table.dialect?.name != dialectName) continue;

    if (tables.containsKey(table.name)) {
      throw StateError('two tables are both named "${table.name}"');
    }
    tables[table.name] = _table(table, dialect);

    for (final index in table.indexes) {
      if (indexes.containsKey(index.name)) {
        throw StateError('two indexes are both named "${index.name}"');
      }
      indexes[index.name] = _index(index, dialect);
    }
  }

  return SchemaSnapshot(
    dialect: dialectName,
    tables: tables,
    indexes: indexes,
  );
}