Path: redb/redb.Tests.Integration/Tests/SqlitePro/SqliteProArrayParentIndexTests.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/SqlitePro/SqliteProArrayParentIndexTests.cs
----
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Pro.Extensions;
using redb.SQLite.Pro.Extensions;
namespace redb.Tests.Integration.Tests.SqlitePro;
///
/// IX__values__array_parent_id is the leading column of IX__values__array_parent_index, which serves the same lookups and
/// the ON DELETE CASCADE of FK__values__array_parent. A new file does not create it; schema step 17 drops it from an
/// existing file only where a non-partial index led by _array_parent_id remains. The test owns its file and makes the
/// steps run again by setting the stamp back, as an older file meets them.
///
public class SqliteProArrayParentIndexTests
{
private const string Narrow = "IX__values__array_parent_id";
private const string Wide = "IX__values__array_parent_index";
private const string File = "redb_tests_array_parent_index_pro.db";
private const string Cs = "Data Source=" + File;
[Fact]
public async Task NarrowIndex_IsLeftOutOfANewFile_AndDroppedOnlyWhereTheWideOneExists()
{
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
await StartAsync(ensureCreated: true);
(await IndexExistsAsync(Narrow)).Should().BeFalse("a new file does not create it");
(await IndexExistsAsync(Wide)).Should().BeTrue();
// An existing file stamped before step 17: both indexes. The step drops the narrow one.
await ExecAsync($"CREATE INDEX \"{Narrow}\" ON _values (_array_parent_id)");
await ExecAsync("PRAGMA user_version = 16");
await StartAsync(ensureCreated: false);
(await IndexExistsAsync(Narrow)).Should().BeFalse("the wider index serves its lookups and the cascade");
(await IndexExistsAsync(Wide)).Should().BeTrue();
// A file created by an old version without the wide index: the narrow one is the cascade's only index.
await ExecAsync($"CREATE INDEX \"{Narrow}\" ON _values (_array_parent_id)");
await ExecAsync($"DROP INDEX \"{Wide}\"");
await ExecAsync("PRAGMA user_version = 16");
await StartAsync(ensureCreated: false);
(await IndexExistsAsync(Narrow)).Should().BeTrue("without a wider index the cascade keeps this one");
SqliteConnection.ClearAllPools();
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
}
private static async Task StartAsync(bool ensureCreated)
{
var services = new ServiceCollection();
services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options => options.UseSqlite(Cs).Configure(c => c.EnablePropsCache = false));
await using var provider = services.BuildServiceProvider();
await provider.GetRequiredService().InitializeAsync(ensureCreated);
SqliteConnection.ClearAllPools();
}
private static async Task IndexExistsAsync(string name)
{
await using var conn = new SqliteConnection(Cs);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = $name";
cmd.Parameters.AddWithValue("$name", name);
return (long)(await cmd.ExecuteScalarAsync())! > 0;
}
private static async Task ExecAsync(string sql)
{
await using var conn = new SqliteConnection(Cs);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
await cmd.ExecuteNonQueryAsync();
}
}