verifyDatabaseIntegrity static method

Future<bool> verifyDatabaseIntegrity(
  1. DatabaseSession session
)

Returns true if the database structure is up to date. If not, it logs a warning via the global log.

Implementation

static Future<bool> verifyDatabaseIntegrity(DatabaseSession session) async {
  var warnings = <String>[];

  var liveDatabase = await session.db.analyzer.analyze();
  var targetTables = session.db.analyzer.getTargetTableDefinitions();

  for (var table in targetTables) {
    var liveTable = liveDatabase.findTableNamed(table.name);
    if (liveTable == null) {
      warnings.add('Table "${table.name}" is missing.');
      continue;
    }
    var mismatches = liveTable.like(table).asStringList();

    if (mismatches.isNotEmpty) {
      warnings.add(
        'Table "${table.name}" is not like the target database:\n'
        ' - ${mismatches.join('\n - ')}',
      );
      continue;
    }
  }
  if (warnings.isNotEmpty) {
    log.warning(
      'The database does not match the target database:\n'
      '${warnings.map((w) => ' - $w').join('\n')}\n'
      'Hint: Did you forget to run `serverpod generate`, apply the migrations '
      '(--apply-migrations), or run a repair migration (--apply-repair-migration)?',
    );
  }

  return warnings.isEmpty;
}