checkForeignKeys static method

Future<void> checkForeignKeys(
  1. DatabaseExecutor database, [
  2. Map<String, int> previousViolations = const {}
])

Checks that the migrations, which ran with foreign keys disabled, didn't violate them, as recommended by SQLite for schema changes. Violations which existed before the migrations, previousViolations, are ignored.

Throws within the transaction of the migrations, so that they get rolled back together with the database version.

Implementation

static Future<void> checkForeignKeys(
  final DatabaseExecutor database, [
  final Map<String, int> previousViolations = const {},
]) async {
  final violations = await getForeignKeyViolations(database);
  final tables = violations.keys
      .where((table) => violations[table]! > (previousViolations[table] ?? 0));
  if (tables.isNotEmpty) {
    throw StateError(
      'The migrations violated foreign keys of the tables ${tables.join(', ')}.',
    );
  }
}