Path: redb/redb.Tests.Integration/Tests/Base/IndexRepairTestsBase.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/Base/IndexRepairTestsBase.cs ---- using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Core; using redb.Core.Data; using redb.Core.Models; using redb.Core.Models.Configuration; using redb.Core.Models.Entities; using redb.Tests.Integration.Fixtures; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// The index check and its repair on a database of its own: a dropped base index is reported Missing with what it serves, /// the start-up logs one Warning and builds nothing, the repair builds it again; an index of a base name defined otherwise /// is reported Differs and left as it is. /// public abstract class IndexRepairTestsBase { protected abstract Task CreateDatabaseAsync(string name); protected abstract Task DropDatabaseAsync(string name); protected abstract void AddRedb(IServiceCollection services, string connectionString, Action configure); /// Drops the named index of the table. protected abstract string DropIndexSql(string table, string index); /// Creates an index of the given name over another column of _objects. protected abstract string CreateOtherIndexSql(string index); private const string Dropped = "IX__values__array_key"; private const string Redefined = "IX__objects__name"; [Fact] public async Task DroppedIndex_IsReported_NotBuiltAtStart_AndRepaired() { var name = $"redb_idxfix_{Guid.NewGuid():N}"[..24]; var connectionString = await CreateDatabaseAsync(name); try { await using (var sp = Build(connectionString, null)) { var redb = sp.GetRequiredService(); await redb.InitializeAsync(ensureCreated: true); var context = sp.GetRequiredService(); await context.ExecuteAsync(DropIndexSql("_values", Dropped)); await context.ExecuteAsync(DropIndexSql("_objects", Redefined)); await context.ExecuteAsync(CreateOtherIndexSql(Redefined)); } var logs = new CapturingLoggerProvider(); await using (var sp = Build(connectionString, logs)) { var redb = sp.GetRequiredService(); await redb.InitializeAsync(); logs.Warnings.Should().ContainSingle(w => w.Contains(Dropped) && w.Contains(Redefined), "the start names every index that differs in one Warning"); var report = await redb.CheckBaseIndexesAsync(); var missing = report.Findings.Single(f => f.Name == Dropped); missing.State.Should().Be(RedbIndexState.Missing, "the start builds nothing"); missing.Expected.Origin.Should().Be(RedbIndexOrigin.Base); missing.Serves.Should().Contain("dictionary key", "the finding says what the index serves"); report.Findings.Single(f => f.Name == Redefined).State.Should().Be(RedbIndexState.Differs); var result = await redb.RepairBaseIndexesAsync(allowOffline: true); result.Built.Should().Equal([Dropped], "the repair builds the missing index and nothing else"); result.NotBuilt.Select(f => f.Name).Should().Equal([Redefined], "an index defined otherwise is never touched"); var after = await redb.CheckBaseIndexesAsync(); after.Findings.Select(f => f.Name).Should().Equal([Redefined]); } } finally { await DropDatabaseAsync(name); } } private const string PlaceIndex = "UIX__values__structure_object"; [Fact] public async Task DuplicateRows_KeepTheIndexUnbuilt_AreListed_AndNothingIsDeleted() { var name = $"redb_idxdup_{Guid.NewGuid():N}"[..24]; var connectionString = await CreateDatabaseAsync(name); try { await using var sp = Build(connectionString, null); var redb = sp.GetRequiredService(); await redb.InitializeAsync(ensureCreated: true); await redb.SyncSchemeAsync(); var id = await redb.SaveAsync(new RedbObject { name = "duplicate probe", Props = new IndexDuplicateProbeProps { Name = "dup-probe" }, }); var context = sp.GetRequiredService(); await context.ExecuteAsync(DropIndexSql("_values", PlaceIndex)); await context.ExecuteAsync( "INSERT INTO _values (_id, _id_structure, _id_object, _String) " + "SELECT (SELECT MAX(_id) + 1 FROM _values), _id_structure, _id_object, _String FROM _values " + $"WHERE _id_object = {id} AND _String = 'dup-probe' AND _array_index IS NULL AND _array_parent_id IS NULL"); var rowsBefore = await context.ExecuteScalarAsync($"SELECT COUNT(*) FROM _values WHERE _id_object = {id}"); var result = await redb.RepairBaseIndexesAsync(allowOffline: true); result.Built.Should().NotContain(PlaceIndex, "a unique index over duplicate rows is not built"); result.NotBuilt.Select(f => f.Name).Should().Contain(PlaceIndex); result.Reason.Should().Contain(PlaceIndex); var duplicate = result.Duplicates.Should().ContainSingle().Subject; duplicate.Index.Should().Be(PlaceIndex); duplicate.ObjectId.Should().Be(id); duplicate.Rows.Should().Be(2); (await context.ExecuteScalarAsync($"SELECT COUNT(*) FROM _values WHERE _id_object = {id}")) .Should().Be(rowsBefore, "the repair deletes nothing"); (await redb.CheckBaseIndexesAsync()).Findings.Single(f => f.Name == PlaceIndex).State .Should().Be(RedbIndexState.Missing); await context.ExecuteAsync("DELETE FROM _values WHERE _id = (SELECT MAX(_id) FROM _values)"); var again = await redb.RepairBaseIndexesAsync(allowOffline: true); again.Built.Should().Equal([PlaceIndex], "with the copy gone the index is built"); again.Duplicates.Should().BeEmpty(); (await redb.CheckBaseIndexesAsync()).IsClean.Should().BeTrue(); } finally { await DropDatabaseAsync(name); } } private ServiceProvider Build(string connectionString, CapturingLoggerProvider? logs) { var services = new ServiceCollection(); services.AddLogging(b => { b.SetMinimumLevel(LogLevel.Warning); if (logs != null) b.AddProvider(logs); }); AddRedb(services, connectionString, c => c.EnablePropsCache = false); return services.BuildServiceProvider(); } }