toPgSql method

String toPgSql({
  1. required List<DatabaseMigrationVersion> installedModules,
})

Implementation

String toPgSql({required List<DatabaseMigrationVersion> installedModules}) {
  String out = '';

  var tableCreation = '';
  var foreignRelations = '';
  for (var table in tables.where((table) => table.isManaged)) {
    tableCreation += '--\n';
    tableCreation += '-- Class ${table.dartName} as table ${table.name}\n';
    tableCreation += '--\n';
    tableCreation += table.tableCreationToPgsql();
    if (table.foreignKeys.isNotEmpty) {
      foreignRelations += '--\n';
      foreignRelations += '-- Foreign relations for "${table.name}" table\n';
      foreignRelations += '--\n';
      foreignRelations += table.foreignRelationToPgsql();
    }
  }

  // Start transaction
  out += 'BEGIN;\n';
  out += '\n';

  // Create tables
  out += tableCreation;

  // Create foreign relations
  out += foreignRelations;

  if (installedModules.isNotEmpty) {
    out += '\n';
  }

  for (var module in installedModules) {
    out += _sqlStoreMigrationVersion(
      module: module.module,
      version: module.version,
    );
  }

  out += '\n';
  out += 'COMMIT;\n';

  return out;
}