createTableIfNotExists method

Future<void> createTableIfNotExists(
  1. Object entity
)

Creates the entity table for entity if it does not exist yet, with the indexes and uniqueIndexes it declares (also created on an existing table when missing). With autoAddColumns, adds the columns the table lacks. Concurrent calls are serialized; a table already seen is not looked up again.

Implementation

Future<void> createTableIfNotExists(Object entity) async {
  final String table = _tableOf(entity);
  if (_knownTables.contains(table)) return;
  final _TxScope? scope = _scope;
  if (scope != null) {
    // A transaction already serializes its work; remember the table only
    // once the transaction commits.
    if (scope.createdTables.contains(table)) return;
    await _ensureTable(entity, table);
    scope.createdTables.add(table);
    return;
  }
  final Completer<void> done = Completer<void>();
  final Future<void> previous = _tableLock;
  _tableLock = done.future;
  try {
    await previous;
    if (_knownTables.contains(table)) return;
    await _ensureTable(entity, table);
    _knownTables.add(table);
  } finally {
    done.complete();
  }
}