Path: redb-route/tests/redb.Route.Tests.Sql/E2E/Infrastructure/PostgresE2EProvider.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/E2E/Infrastructure/PostgresE2EProvider.cs
----
using System.Data.Common;
using Npgsql;
namespace redb.Route.Tests.Sql.E2E.Infrastructure;
/// PostgreSQL through Npgsql. Container pgdb (profile pgdb of docker-compose.tests.yml).
public sealed class PostgresE2EProvider : SqlE2EProvider
{
private const string EnvironmentVariable = "ROUTE_SQL_PG_CS";
private const string DefaultConnectionString =
"Host=localhost;Port=5432;Username=postgres;Password=1;Database=postgres;Timeout=5";
public override string Name => "postgres";
public override DbProviderFactory Factory => NpgsqlFactory.Instance;
// Npgsql 10 leaves PostgresException.BatchCommand null unless the connection opts in, and the suite
// checks the exact failed item of a batch: the option is set here, also on an overriding string.
public override string ConnectionString => new NpgsqlConnectionStringBuilder(
Environment.GetEnvironmentVariable(EnvironmentVariable)?.Trim() is { Length: > 0 } cs ? cs : global::redb.Route.Tests.Shared.TestDatabases.Postgres(DefaultConnectionString))
{
IncludeFailedBatchedCommand = true,
}.ConnectionString;
public override string StartHint =>
$"Start it with 'docker start pgdb' (or 'docker compose -f redb.Route/docker-compose.tests.yml up -d pgdb'); override the connection with {EnvironmentVariable}.";
public override string ReturningInsertSql(string table) =>
$"INSERT INTO {table} (id, val) VALUES (:#id, :#val) RETURNING id";
public override string NativeReturningInsertSql(string table) =>
$"INSERT INTO {table} (id, val) VALUES (@id, @val) RETURNING id";
public override string TransactionKillingLiteral(string table) =>
$"INSERT INTO {table} (id, val) VALUES (CAST('abc' AS int), 'x')";
public override string TransactionKillingBatchSql(string table) =>
$"INSERT INTO {table} (id, val) VALUES (CAST(:#id AS int), :#val)";
public override List> TransactionKillingItems() =>
[
new() { ["id"] = "1", ["val"] = "a" },
new() { ["id"] = "abc", ["val"] = "x" },
new() { ["id"] = "3", ["val"] = "c" },
];
// Any error aborts the transaction block; COMMIT then answers ROLLBACK and Npgsql does not raise.
public override int[] LoopContinueDuplicateKeyRows => [];
public override int[] LoopContinueTransactionKillingRows => [];
public override bool DuplicateKeyDestroysTransaction => false;
public override bool TransactionKillingErrorDestroysTransaction => false;
public override bool ReportsSavepointSupport => true;
public override bool CommandAfterTransactionKillingErrorIsRejected => true;
public override bool CanCreateBatch => true;
public override bool PrepareRejectsUntypedParameters => false;
}