Class SqlServerMigrator
- Namespace
- Excalibur.EventSourcing.SqlServer
- Assembly
- Excalibur.EventSourcing.SqlServer.dll
SQL Server implementation of IMigrator.
public sealed class SqlServerMigrator : IMigrator
- Inheritance
-
SqlServerMigrator
- Implements
- Inherited Members
Remarks
This migrator executes SQL scripts embedded as resources in the migration assembly. Scripts should be named following the pattern: YYYYMMDDHHMMSS_MigrationName.sql
Thread safety is ensured using SQL Server application locks (sp_getapplock/sp_releaseapplock) to prevent concurrent migrations during multi-instance startup.
Constructors
SqlServerMigrator(Func<SqlConnection>, Assembly, string, ILogger<SqlServerMigrator>)
Initializes a new instance of the SqlServerMigrator class.
public SqlServerMigrator(Func<SqlConnection> connectionFactory, Assembly migrationAssembly, string migrationNamespace, ILogger<SqlServerMigrator> logger)
Parameters
connectionFactoryFunc<SqlConnection>Factory function to create SQL connections.
migrationAssemblyAssemblyThe assembly containing migration scripts as embedded resources.
migrationNamespacestringThe namespace prefix for migration resources (e.g., "MyApp.Migrations").
loggerILogger<SqlServerMigrator>The logger instance.
SqlServerMigrator(string, Assembly, string, ILogger<SqlServerMigrator>)
Initializes a new instance of the SqlServerMigrator class.
public SqlServerMigrator(string connectionString, Assembly migrationAssembly, string migrationNamespace, ILogger<SqlServerMigrator> logger)
Parameters
connectionStringstringThe SQL Server connection string.
migrationAssemblyAssemblyThe assembly containing migration scripts as embedded resources.
migrationNamespacestringThe namespace prefix for migration resources (e.g., "MyApp.Migrations").
loggerILogger<SqlServerMigrator>The logger instance.
Methods
GetAppliedMigrationsAsync(CancellationToken)
Gets all migrations that have been applied to the database.
public Task<IReadOnlyList<AppliedMigration>> GetAppliedMigrationsAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<IReadOnlyList<AppliedMigration>>
A list of applied migrations in the order they were applied.
GetPendingMigrationsAsync(CancellationToken)
Gets all pending migrations that have not yet been applied.
public Task<IReadOnlyList<string>> GetPendingMigrationsAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<IReadOnlyList<string>>
A list of pending migration IDs in the order they should be applied.
MigrateAsync(CancellationToken)
Applies all pending migrations to bring the database schema up to date.
public Task<MigrationResult> MigrateAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<MigrationResult>
The result of the migration operation including applied migrations.
Remarks
This method is idempotent - calling it when no migrations are pending will succeed with an empty list of applied migrations.
RollbackAsync(string, CancellationToken)
Rolls back migrations to the specified target version.
public Task<MigrationResult> RollbackAsync(string targetMigrationId, CancellationToken cancellationToken)
Parameters
targetMigrationIdstringThe migration ID to roll back to (this migration will remain applied).
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<MigrationResult>
The result of the rollback operation.
Remarks
The target migration ID specifies the migration to roll back TO, not the migration to remove. All migrations applied after the target migration will be removed.
If the target migration ID is not found in the applied migrations, the operation will fail.