Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProArrayParentIndexTests.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/MsSqlPro/MsSqlProArrayParentIndexTests.cs ---- using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Core; using redb.Core.Pro.Extensions; using redb.MSSql.Pro.Extensions; using redb.Tests.Integration.Fixtures; namespace redb.Tests.Integration.Tests.MsSqlPro; /// /// IX__values__array_parent_id is the leading column of IX__values__array_parent_index, which serves the same lookups, /// the check of FK__values__array_parent and the cascade trigger. A new database does not create it; the module drops it /// on an existing database only where an unfiltered index led by _array_parent_id remains. 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 MsSqlProArrayParentIndexTests : 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("MSSql")!; private readonly SchemaUpgradeCleanup _cleanup; public MsSqlProArrayParentIndexTests(SchemaUpgradeCleanup cleanup) { _cleanup = cleanup; cleanup.Register(async () => { SqlConnection.ClearAllPools(); await AdminAsync("master", $"IF DB_ID(N'{DatabaseName}') IS NOT NULL BEGIN ALTER DATABASE [{DatabaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{DatabaseName}]; END"); }); } private string DatabaseName => $"redb_arrparent_pro_{_cleanup.Suffix}"; [Fact] public async Task NarrowIndex_IsLeftOutOfANewDatabase_AndDroppedOnlyWhereTheWideOneExists() { var database = DatabaseName; await AdminAsync("master", $"IF DB_ID(N'{database}') IS NOT NULL BEGIN ALTER DATABASE [{database}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{database}]; END"); await AdminAsync("master", $"CREATE DATABASE [{database}]"); await StartAsync(database, ensureCreated: true); (await IndexExistsAsync(database, Narrow)).Should().BeFalse("a new database does not keep 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 [dbo].[_values]([_array_parent_id])"); await AdminAsync(database, "DROP FUNCTION dbo.pvt_module_version"); await StartAsync(database, ensureCreated: false); (await IndexExistsAsync(database, Narrow)).Should().BeFalse("the wider index serves its lookups, the key check and the trigger"); (await IndexExistsAsync(database, Wide)).Should().BeTrue(); // A database created by an old version without the wide index: the narrow one is the only index led by the parent. await AdminAsync(database, $"CREATE INDEX [{Narrow}] ON [dbo].[_values]([_array_parent_id])"); await AdminAsync(database, $"DROP INDEX [{Wide}] ON [dbo].[_values]"); await AdminAsync(database, "DROP FUNCTION dbo.pvt_module_version"); await StartAsync(database, ensureCreated: false); (await IndexExistsAsync(database, Narrow)).Should().BeTrue("without a wider index the key check and the trigger keep this one"); } private static async Task StartAsync(string database, bool ensureCreated) { var cs = new SqlConnectionStringBuilder(AdminBase) { InitialCatalog = 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.UseMsSql(cs).Configure(c => c.EnablePropsCache = false); if (!string.IsNullOrWhiteSpace(license)) options.WithLicense(license); }); await using var provider = services.BuildServiceProvider(); await provider.GetRequiredService().InitializeAsync(ensureCreated); SqlConnection.ClearAllPools(); } private static async Task IndexExistsAsync(string database, string name) { await using var conn = new SqlConnection(new SqlConnectionStringBuilder(AdminBase) { InitialCatalog = database, Pooling = false }.ConnectionString); await conn.OpenAsync(); await using var cmd = conn.CreateCommand(); cmd.CommandText = "SELECT COUNT(*) FROM sys.indexes WHERE object_id = OBJECT_ID(N'dbo._values') AND name = @name"; cmd.Parameters.AddWithValue("@name", name); return (int)(await cmd.ExecuteScalarAsync())! > 0; } private static async Task AdminAsync(string database, string sql) { await using var conn = new SqlConnection(new SqlConnectionStringBuilder(AdminBase) { InitialCatalog = database, Pooling = false }.ConnectionString); await conn.OpenAsync(); await using var cmd = conn.CreateCommand(); cmd.CommandText = sql; cmd.CommandTimeout = 600; await cmd.ExecuteNonQueryAsync(); } }