Path: redb/redb.Tests.Integration/Tests/Postgres/PostgresSchemaUpgradeTests.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Postgres/PostgresSchemaUpgradeTests.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Postgres.Extensions;
using redb.Tests.Integration.Fixtures;
using redb.Tests.Integration.Tests.Base;
namespace redb.Tests.Integration.Tests.Postgres;
///
/// Owns database redb_upgrade_<run> and role redb_noddl_<run> on the test server. Not in any
/// collection on purpose: nothing else touches that database, so it can run in parallel with the
/// shared-database suites.
///
public class PostgresSchemaUpgradeTests : SchemaUpgradeTestsBase
{
public PostgresSchemaUpgradeTests(SchemaUpgradeCleanup cleanup) : base(cleanup) { }
// Virtual so the Pro subclass owns its own database and role: the two classes run in
// parallel and must not share the database they deliberately damage.
protected virtual string DatabaseName => $"redb_upgrade_{RunSuffix}";
protected virtual string FreshDatabaseName => $"redb_upgrade_fresh_{RunSuffix}";
protected virtual string RestrictedRole => $"redb_noddl_{RunSuffix}";
private const string RestrictedPassword = "noddl";
private static readonly string AdminBase =
new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetConnectionString("Postgres")!;
protected override string OwnerConnectionString =>
new NpgsqlConnectionStringBuilder(AdminBase) { Database = DatabaseName }.ConnectionString;
protected override string FreshDatabaseConnectionString =>
new NpgsqlConnectionStringBuilder(AdminBase) { Database = FreshDatabaseName }.ConnectionString;
protected override async Task RecreateFreshDatabaseAsync()
{
await ExecAdminAsync("postgres", $"DROP DATABASE IF EXISTS \"{FreshDatabaseName}\" WITH (FORCE)");
await ExecAdminAsync("postgres", $"CREATE DATABASE \"{FreshDatabaseName}\"");
}
protected override string RestrictedConnectionString =>
new NpgsqlConnectionStringBuilder(AdminBase)
{
Database = DatabaseName, Username = RestrictedRole, Password = RestrictedPassword
}.ConnectionString;
protected override void UseProvider(RedbOptionsBuilder options, string connectionString)
=> options.UsePostgres(connectionString);
protected static async Task ExecAdminAsync(string database, string sql)
{
var cs = new NpgsqlConnectionStringBuilder(AdminBase) { Database = database, Pooling = false }.ConnectionString;
await using var conn = new NpgsqlConnection(cs);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
await cmd.ExecuteNonQueryAsync();
}
protected static async Task ScalarAdminAsync(string database, string sql)
{
var cs = new NpgsqlConnectionStringBuilder(AdminBase) { Database = database, Pooling = false }.ConnectionString;
await using var conn = new NpgsqlConnection(cs);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
var v = await cmd.ExecuteScalarAsync();
return v is null or DBNull ? default : (T)v;
}
protected override async Task PrepareDatabaseAndLoginAsync()
{
if (await ScalarAdminAsync