ensureMatchesLoadedSchemaSync method

void ensureMatchesLoadedSchemaSync(
  1. LoadedSchemaDocument loaded, {
  2. String? generatorName,
  3. String? outputPath,
})

Verifies that an existing generated client matches loaded.

Implementation

void ensureMatchesLoadedSchemaSync(
  LoadedSchemaDocument loaded, {
  String? generatorName,
  String? outputPath,
}) {
  final generator = workflow.resolveGenerator(
    loaded,
    generatorName: generatorName,
  );
  final effectiveOutputPath = outputPath == null || outputPath.isEmpty
      ? generator.outputPath
      : File(outputPath).absolute.path;
  final outputFile = File(effectiveOutputPath);
  if (!outputFile.existsSync()) {
    throw GeneratedClientSchemaMismatchException(
      outputPath: outputFile.path,
      diff: 'Generated client file does not exist. Run `comon_orm generate`.',
    );
  }

  final schemaSource = File(loaded.filePath).readAsStringSync();
  final existingContent = outputFile.readAsStringSync();
  final expectedHash = sha256.convert(utf8.encode(schemaSource)).toString();
  final existingHash = _extractSchemaHash(existingContent);
  if (existingHash == expectedHash) {
    return;
  }

  final existingSchemaSource = _extractEmbeddedSchemaSource(existingContent);
  final diff = _buildSchemaDiff(
    expected: schemaSource,
    actual: existingSchemaSource,
  );
  throw GeneratedClientSchemaMismatchException(
    outputPath: outputFile.path,
    diff: diff,
  );
}