Class PostgresMigrator
- Namespace
- Excalibur.EventSourcing.Postgres
- Assembly
- Excalibur.EventSourcing.Postgres.dll
Postgres implementation of IMigrator.
public sealed class PostgresMigrator : IMigrator
- Inheritance
-
PostgresMigrator
- 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 Postgres advisory locks (pg_advisory_lock/pg_advisory_unlock) to prevent concurrent migrations during multi-instance startup.
Uses snake_case naming convention per for Postgres compatibility.
Constructors
PostgresMigrator(NpgsqlDataSource, Assembly, string, ILogger<PostgresMigrator>)
Initializes a new instance of the PostgresMigrator class.
public PostgresMigrator(NpgsqlDataSource dataSource, Assembly migrationAssembly, string migrationNamespace, ILogger<PostgresMigrator> logger)
Parameters
dataSourceNpgsqlDataSourceThe NpgsqlDataSource for connection pooling.
migrationAssemblyAssemblyThe assembly containing migration scripts as embedded resources.
migrationNamespacestringThe namespace prefix for migration resources (e.g., "MyApp.Migrations").
loggerILogger<PostgresMigrator>The logger instance.
PostgresMigrator(string, Assembly, string, ILogger<PostgresMigrator>)
Initializes a new instance of the PostgresMigrator class.
public PostgresMigrator(string connectionString, Assembly migrationAssembly, string migrationNamespace, ILogger<PostgresMigrator> logger)
Parameters
connectionStringstringThe Postgres connection string.
migrationAssemblyAssemblyThe assembly containing migration scripts as embedded resources.
migrationNamespacestringThe namespace prefix for migration resources (e.g., "MyApp.Migrations").
loggerILogger<PostgresMigrator>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.