// Copyright Epic Games, Inc. All Rights Reserved.
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace HordeServer.Server;
///
/// Attribute to decorate MongoDB migration classes with metadata
///
[AttributeUsage(AttributeTargets.Class)]
public sealed class MongoMigrationAttribute(string plugin, int version, string name, bool autoLoad = true) : Attribute
{
///
/// Plugin identifier for this migration.
///
public string Plugin { get; } = plugin;
///
/// Gets the version number for this migration and plugin
/// Must be monotonically increasing and unique within in plugin namespace
///
public int Version { get; } = version;
///
/// Descriptive name of the change this migration performs
///
public string Name { get; } = name;
///
/// Whether migration should be detected and loaded during full reflection scans of available migrations
/// If set to false, the migration must manually be registered with a migrator
///
public bool AutoLoad { get; } = autoLoad;
}
///
/// Provides context and services needed for executing migrations
///
public record MigrationContext(IMongoService MongoService, ILogger Logger)
{
///
/// Shorthand for accessing the MongoDB database object
///
public IMongoDatabase Database => MongoService.Database;
}
///
/// Defines operations for upgrading and downgrading database schemas
/// Implemented by classes providing a migration
///
public interface IMongoMigration
{
///
/// Upgrade the database schema version
///
public Task UpAsync(MigrationContext context, CancellationToken token);
///
/// Downgrade the database schema version
///
public Task DownAsync(MigrationContext context, CancellationToken token);
}