Table of Contents

Class PostgreSQLMigrationRunner

Namespace
The.PostgreSQL.Schema
Assembly
Instagile.PostgreSQL.dll

Executes schema migrations against a PostgreSQL database. Handles control table creation and migration version tracking.

public sealed class PostgreSQLMigrationRunner : MigrationRunner<IDatabase>
Inheritance
PostgreSQLMigrationRunner
Inherited Members

Constructors

PostgreSQLMigrationRunner(IDatabase, QueryConfig?, ILogger<PostgreSQLMigrationRunner>?)

public PostgreSQLMigrationRunner(IDatabase database, QueryConfig? config = null, ILogger<PostgreSQLMigrationRunner>? logger = null)

Parameters

database IDatabase
config QueryConfig
logger ILogger<PostgreSQLMigrationRunner>

Methods

CreateDbParameter(int, object)

Creates a PostgreSQL parameter which will match a positional placeholder ($1, $2, etc.).

protected override DbParameter CreateDbParameter(int number, object value)

Parameters

number int
value object

Returns

DbParameter

CreateDbParametersFor(MigrationRunnerOptions)

Creates parameters for migration runner options. PostgreSQL requires time zone conversion for DateTime → Instant transitions.

protected override DbParameter[] CreateDbParametersFor(MigrationRunnerOptions migrationParameters)

Parameters

migrationParameters MigrationRunnerOptions

Returns

DbParameter[]

ExecuteBatch(IDatabase, string, TimeSpan, DbParameter[])

Executes a migration batch, using NpgsqlBatch if the SQL contains multiple statements. This is necessary because PostgreSQL parameter binding doesn't work with semicolon-concatenated SQL.

protected override void ExecuteBatch(IDatabase db, string text, TimeSpan timeout, DbParameter[] parameters)

Parameters

db IDatabase
text string
timeout TimeSpan
parameters DbParameter[]

Remarks

DML is careful to execute only one statement at at a time; DDL, unfortunately, was designed to be written to .theSchema files and does not provide the same guarantee.

GetControlTableExists(IDatabase)

Checks if the migration control table exists in the PostgreSQL database.

protected override bool GetControlTableExists(IDatabase db)

Parameters

db IDatabase

Returns

bool

GetServerProperties(IDatabase)

Gets PostgreSQL server properties for migration decision-making.

protected override HashSet<string> GetServerProperties(IDatabase db)

Parameters

db IDatabase

Returns

HashSet<string>

SplitBatch(string)

Splits a batch of SQL statements while respecting PostgreSQL dollar-quoted strings (\(...\) and \(tag\)...\(tag\)). This prevents splitting on semicolons that appear inside PL/pgSQL function bodies. Assumes dollar quotes appear at the start or end of lines (as generated by PostgreSQLTriggers).

public static IEnumerable<string> SplitBatch(string batchSql)

Parameters

batchSql string

Returns

IEnumerable<string>