Path: redb/redb.Tests.Integration/Tests/Postgres/PostgresIndexCatalogTests.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/PostgresIndexCatalogTests.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models;
using redb.Core.Models.Configuration;
using redb.Postgres.Extensions;
using redb.Postgres.Sql;
using redb.Tests.Integration.Tests.Base;
namespace redb.Tests.Integration.Tests.Postgres;
/// PostgresIndexCatalog.Base against a fresh PostgreSQL database of this build (see ).
public class PostgresIndexCatalogTests : IndexCatalogTestsBase
{
internal static readonly string AdminBase =
new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetConnectionString("Postgres")!;
protected override async Task CreateDatabaseAsync(string name)
{
await ExecAdminAsync($"CREATE DATABASE \"{name}\"");
return new NpgsqlConnectionStringBuilder(AdminBase) { Database = name }.ConnectionString;
}
protected override async Task DropDatabaseAsync(string name)
{
NpgsqlConnection.ClearAllPools();
await ExecAdminAsync($"DROP DATABASE IF EXISTS \"{name}\" WITH (FORCE)");
}
internal static async Task ExecAdminAsync(string sql)
{
var cs = new NpgsqlConnectionStringBuilder(AdminBase) { Database = "postgres", Pooling = false }.ConnectionString;
await using var connection = new NpgsqlConnection(cs);
await connection.OpenAsync();
await using var command = new NpgsqlCommand(sql, connection);
await command.ExecuteNonQueryAsync();
}
protected override void AddRedb(IServiceCollection services, string connectionString, Action configure)
=> services.AddRedb(o => o.UsePostgres(connectionString).Configure(configure));
// pg_indexes.indexdef names the table with its schema; the table is kept schema-free.
protected override async Task> ReadCreateStatementsAsync(IRedbContext context)
{
var rows = await context.QueryAsync(
"SELECT indexname AS \"Name\", replace(indexdef, ' ON ' || schemaname || '.', ' ON ') AS \"Sql\" " +
"FROM pg_indexes WHERE schemaname = current_schema()");
return rows.ToDictionary(r => r.Name!, r => r.Sql!, StringComparer.OrdinalIgnoreCase);
}
protected override IReadOnlyList Catalog => PostgresIndexCatalog.Base;
protected override (string File, string Namespace, string Class, string Member, RedbIndexOrigin Origin) Target =>
("redb.Postgres/sql/PostgresIndexCatalog.g.cs", "redb.Postgres.Sql", "PostgresIndexCatalog", "Base", RedbIndexOrigin.Base);
protected sealed class SqlRow
{
public string? Name { get; set; }
public string? Sql { get; set; }
}
}
/// The index check and its repair on PostgreSQL (CREATE INDEX CONCURRENTLY), see .
public class PostgresIndexRepairTests : IndexRepairTestsBase
{
protected override async Task CreateDatabaseAsync(string name)
{
await PostgresIndexCatalogTests.ExecAdminAsync($"CREATE DATABASE \"{name}\"");
return new NpgsqlConnectionStringBuilder(PostgresIndexCatalogTests.AdminBase) { Database = name }.ConnectionString;
}
protected override async Task DropDatabaseAsync(string name)
{
NpgsqlConnection.ClearAllPools();
await PostgresIndexCatalogTests.ExecAdminAsync($"DROP DATABASE IF EXISTS \"{name}\" WITH (FORCE)");
}
protected override void AddRedb(IServiceCollection services, string connectionString, Action configure)
=> services.AddRedb(o => o.UsePostgres(connectionString).Configure(configure));
protected override string DropIndexSql(string table, string index) => $"DROP INDEX \"{index}\"";
protected override string CreateOtherIndexSql(string index) => $"CREATE INDEX \"{index}\" ON _objects (_note)";
}