apply static method

void apply(
  1. Database db
)

Applies PRAGMAs, creates every table/index (idempotent), seeds the counters, and reconciles user_version. Safe to call on an existing database (nothing is dropped).

Throws StateError if the database's stored user_version is newer than contractVersion — this reader cannot safely touch a forward-versioned file.

Implementation

static void apply(Database db) {
  for (final p in pragmas) {
    db.execute(p);
  }

  final storedVersion = _userVersion(db);
  if (storedVersion > contractVersion) {
    throw StateError(
        'SQLite database is at contract version $storedVersion, newer than '
        'this server understands ($contractVersion). Refusing to open.');
  }

  db.execute(atData);
  for (final ix in atDataIndexes) {
    db.execute(ix);
  }
  db.execute(commitLog);
  for (final ix in commitLogIndexes) {
    db.execute(ix);
  }
  db.execute(counters);
  db.execute(seedCounters);
  db.execute(notifications);
  for (final ix in notificationIndexes) {
    db.execute(ix);
  }
  db.execute(accessLog);
  for (final ix in accessLogIndexes) {
    db.execute(ix);
  }

  if (storedVersion < contractVersion) {
    // Forward-migrate marker. (v1 is the initial contract, so there is
    // no data reshaping to do; future bumps add migration steps here.)
    db.execute('PRAGMA user_version = $contractVersion;');
  }
}