Path: redb/redb.Tests.Integration/Tests/Postgres/PostgresExportRenameHistoryTests.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/PostgresExportRenameHistoryTests.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Npgsql;
using redb.Core.Extensions;
using redb.Postgres.Extensions;
using redb.Tests.Integration.Tests.Base;
namespace redb.Tests.Integration.Tests.Postgres;
/// on PostgreSQL, scratch databases dropped afterwards.
public class PostgresExportRenameHistoryTests : ExportRenameHistoryTestsBase, IAsyncLifetime
{
private string _server = null!;
private readonly List _databases = [];
private readonly List _files = [];
protected override string ProviderName => "postgres";
public Task InitializeAsync()
{
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
_server = config.GetConnectionString("Postgres")!;
return Task.CompletedTask;
}
public async Task DisposeAsync()
{
NpgsqlConnection.ClearAllPools();
foreach (var database in _databases)
await ExecuteAsync(Maintenance(), $"DROP DATABASE IF EXISTS \"{database}\" WITH (FORCE)");
foreach (var file in _files)
File.Delete(file);
}
protected override async Task NewDatabaseAsync(string suffix)
{
var name = $"redb_export_ren_{Guid.NewGuid().ToString("N")[..12]}_{suffix}";
_databases.Add(name);
await ExecuteAsync(Maintenance(), $"CREATE DATABASE \"{name}\"");
return new NpgsqlConnectionStringBuilder(_server) { Database = name }.ConnectionString;
}
protected override ServiceProvider Build(string cs)
{
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
services.AddRedb(options => options
.UsePostgres(cs)
.Configure(c => c.CacheDomain = $"export-ren-{Guid.NewGuid():N}"));
return services.BuildServiceProvider();
}
protected override string NewFile()
{
var file = Path.Combine(Path.GetTempPath(), $"redb-export-ren-{Guid.NewGuid():N}.redb");
_files.Add(file);
return file;
}
protected override async Task ScalarAsync(string cs, string sql)
{
await using var connection = new NpgsqlConnection(cs);
await connection.OpenAsync();
await using var command = new NpgsqlCommand(sql, connection);
return AsText(await command.ExecuteScalarAsync());
}
protected override async Task> PairsAsync(string cs, string sql)
{
await using var connection = new NpgsqlConnection(cs);
await connection.OpenAsync();
await using var command = new NpgsqlCommand(sql, connection);
await using var reader = await command.ExecuteReaderAsync();
var rows = new List<(string?, string?)>();
while (await reader.ReadAsync())
rows.Add((AsText(reader.GetValue(0)), AsText(reader.GetValue(1))));
return rows;
}
protected override Task ExecuteSqlAsync(string cs, string sql) => ExecuteAsync(cs, sql);
private string Maintenance() => new NpgsqlConnectionStringBuilder(_server) { Database = "postgres" }.ConnectionString;
private static async Task ExecuteAsync(string cs, string sql)
{
await using var connection = new NpgsqlConnection(cs);
await connection.OpenAsync();
await using var command = new NpgsqlCommand(sql, connection);
await command.ExecuteNonQueryAsync();
}
}