Path: redb/redb.Tests.Integration/Tests/PostgresPro/PostgresProArrayParentIndexTests.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/PostgresPro/PostgresProArrayParentIndexTests.cs ---- using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Npgsql; using redb.Core; using redb.Core.Pro.Extensions; using redb.Postgres.Pro.Extensions; using redb.Tests.Integration.Fixtures; namespace redb.Tests.Integration.Tests.PostgresPro; /// /// IX__values__array_parent_id is the leading column of IX__values__array_parent_index, which serves the same lookups /// and the cascade of FK__values__array_parent. A new database does not create it; the module drops it on an existing /// database only where a wider index led by _array_parent_id exists, because a database created by an old version may /// lack array_parent_index and the cascade must keep an index. The test owns its database and makes the module apply /// again by removing its version function, as a database with an older module meets it. /// public class PostgresProArrayParentIndexTests : IClassFixture { private const string Narrow = "IX__values__array_parent_id"; private const string Wide = "IX__values__array_parent_index"; private static readonly string AdminBase = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetConnectionString("Postgres")!; private readonly SchemaUpgradeCleanup _cleanup; public PostgresProArrayParentIndexTests(SchemaUpgradeCleanup cleanup) { _cleanup = cleanup; cleanup.Register(async () => { NpgsqlConnection.ClearAllPools(); await AdminAsync("postgres", $"DROP DATABASE IF EXISTS \"{DatabaseName}\" WITH (FORCE)"); }); } private string DatabaseName => $"redb_arrparent_{_cleanup.Suffix}"; [Fact] public async Task NarrowIndex_IsLeftOutOfANewDatabase_AndDroppedOnlyWhereTheWideOneExists() { var database = DatabaseName; await AdminAsync("postgres", $"DROP DATABASE IF EXISTS \"{database}\" WITH (FORCE)"); await AdminAsync("postgres", $"CREATE DATABASE \"{database}\""); await StartAsync(database, ensureCreated: true); (await IndexExistsAsync(database, Narrow)).Should().BeFalse("a new database does not create it"); (await IndexExistsAsync(database, Wide)).Should().BeTrue(); // An existing database of an older module: both indexes. The module drops the narrow one. await AdminAsync(database, $"CREATE INDEX \"{Narrow}\" ON _values (_array_parent_id)"); await AdminAsync(database, "DROP FUNCTION pvt_module_version()"); await StartAsync(database, ensureCreated: false); (await IndexExistsAsync(database, Narrow)).Should().BeFalse("the wider index serves its lookups and the cascade"); (await IndexExistsAsync(database, Wide)).Should().BeTrue(); // A database created by an old version without the wide index: the narrow one is the cascade's only index. await AdminAsync(database, $"CREATE INDEX \"{Narrow}\" ON _values (_array_parent_id)"); await AdminAsync(database, $"DROP INDEX \"{Wide}\""); await AdminAsync(database, "DROP FUNCTION pvt_module_version()"); await StartAsync(database, ensureCreated: false); (await IndexExistsAsync(database, Narrow)).Should().BeTrue("without a wider index the cascade keeps this one"); } private static async Task StartAsync(string database, bool ensureCreated) { var cs = new NpgsqlConnectionStringBuilder(AdminBase) { Database = database }.ConnectionString; var license = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build()["Redb:License"]; var services = new ServiceCollection(); services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning)); services.AddRedbPro(options => { options.UsePostgres(cs).Configure(c => c.EnablePropsCache = false); if (!string.IsNullOrWhiteSpace(license)) options.WithLicense(license); }); await using var provider = services.BuildServiceProvider(); await provider.GetRequiredService().InitializeAsync(ensureCreated); NpgsqlConnection.ClearAllPools(); } private static async Task IndexExistsAsync(string database, string name) => (long)(await AdminScalarAsync(database, $"SELECT count(*) FROM pg_indexes WHERE indexname = '{name}'"))! > 0; private static async Task AdminAsync(string database, string sql) { await using var conn = new NpgsqlConnection(new NpgsqlConnectionStringBuilder(AdminBase) { Database = database, Pooling = false }.ConnectionString); await conn.OpenAsync(); await using var cmd = conn.CreateCommand(); cmd.CommandText = sql; await cmd.ExecuteNonQueryAsync(); } private static async Task AdminScalarAsync(string database, string sql) { await using var conn = new NpgsqlConnection(new NpgsqlConnectionStringBuilder(AdminBase) { Database = database, Pooling = false }.ConnectionString); await conn.OpenAsync(); await using var cmd = conn.CreateCommand(); cmd.CommandText = sql; return await cmd.ExecuteScalarAsync(); } }