verifyDatabaseIntegrity static method

Future<bool> verifyDatabaseIntegrity(
  1. DatabaseSession session
)

Returns true if the database structure matches the model requirements. If not, it logs a warning via the global log.

For tables with TableDefinition.managed set to false, only the presence of the table and its declared columns, compatible column types, nullability, and vector dimensions are verified. Defaults, additional columns, indexes, foreign keys, and other migration-owned properties are not compared.

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,
      schema: table.schema,
    );
    if (liveTable == null) {
      warnings.add('Table "${table.name}" is missing.');
      continue;
    }
    var mismatches =
        (table.managed == false
                ? _compareUnmanagedColumns(table, liveTable)
                : 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;
}