Table of Contents

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

dataSource NpgsqlDataSource

The NpgsqlDataSource for connection pooling.

migrationAssembly Assembly

The assembly containing migration scripts as embedded resources.

migrationNamespace string

The namespace prefix for migration resources (e.g., "MyApp.Migrations").

logger ILogger<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

connectionString string

The Postgres connection string.

migrationAssembly Assembly

The assembly containing migration scripts as embedded resources.

migrationNamespace string

The namespace prefix for migration resources (e.g., "MyApp.Migrations").

logger ILogger<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

cancellationToken CancellationToken

Cancellation 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

cancellationToken CancellationToken

Cancellation 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

cancellationToken CancellationToken

Cancellation 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

targetMigrationId string

The migration ID to roll back to (this migration will remain applied).

cancellationToken CancellationToken

Cancellation 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.