write method

Future<void> write({
  1. required List<DatabaseMigrationVersion> installedModules,
  2. required List<DatabaseMigrationVersion> removedModules,
})

Implementation

Future<void> write({
  required List<DatabaseMigrationVersion> installedModules,
  required List<DatabaseMigrationVersion> removedModules,
}) async {
  var migrationDirectory = MigrationConstants.migrationVersionDirectory(
    projectDirectory,
    versionName,
  );

  if (migrationDirectory.existsSync()) {
    throw MigrationVersionAlreadyExistsException(
      directoryPath: migrationDirectory.path,
    );
  }
  await migrationDirectory.create(recursive: true);

  // Create sql for definition and migration
  var definitionSql = databaseDefinitionFull.toPgSql(
    installedModules: installedModules,
  );

  var migrationSql = migration.toPgSql(
    installedModules: installedModules,
    removedModules: removedModules,
  );

  // Write the database definition JSON file
  var definitionFile = MigrationConstants.databaseDefinitionProjectJSONPath(
    projectDirectory,
    versionName,
  );
  var definitionData = SerializationManager.encode(
    databaseDefinitionProject,
    formatted: true,
  );
  await definitionFile.writeAsString(definitionData);

  // Write the database full definition JSON file
  var definitionFullFile = MigrationConstants.databaseDefinitionJSONPath(
    projectDirectory,
    versionName,
  );
  var definitionFullData = SerializationManager.encode(
    databaseDefinitionFull,
    formatted: true,
  );
  await definitionFullFile.writeAsString(definitionFullData);

  // Write the database definition SQL file
  var definitionSqlFile = MigrationConstants.databaseDefinitionSQLPath(
    projectDirectory,
    versionName,
  );
  await definitionSqlFile.writeAsString(definitionSql);

  // Write the migration definition JSON file
  var migrationFile = MigrationConstants.databaseMigrationJSONPath(
    projectDirectory,
    versionName,
  );
  var migrationData = SerializationManager.encode(
    migration,
    formatted: true,
  );
  await migrationFile.writeAsString(migrationData);

  // Write the migration definition SQL file
  var migrationSqlFile = MigrationConstants.databaseMigrationSQLPath(
    projectDirectory,
    versionName,
  );
  await migrationSqlFile.writeAsString(migrationSql);
}