saveSchemaVersion method

Future<void> saveSchemaVersion(
  1. int version,
  2. List<SupabaseTableInfo> tables
)

Saves the current schema state to a versioned JSON file.

Implementation

Future<void> saveSchemaVersion(
  int version,
  List<SupabaseTableInfo> tables,
) async {
  final filePath = p.join(_schemaVersionsDir, 'schema_v$version.json');
  _logger.info('Saving schema state for version $version to: $filePath');
  try {
    final file = File(filePath);
    await _ensureDirectoryExists(file.parent.path); // Ensure parent exists
    final jsonList = tables.map((t) => t.toJson()).toList();
    final content = JsonEncoder.withIndent('  ').convert(jsonList);
    await file.writeAsString(content);
    // Update internal state after successful save
    previousSchemaStates[version] = tables;
    if (version > currentSchemaVersion) {
      currentSchemaVersion = version;
    }
  } catch (e) {
    _logger.severe('Failed to save schema version file $filePath: $e');
  }
}