Path: redb/redb.Core/Providers/Base/SchemeSyncProviderBase.Drop.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.Core/Providers/Base/SchemeSyncProviderBase.Drop.cs ---- using Microsoft.Extensions.Logging; using redb.Core.Models; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Core.Utils; namespace redb.Core.Providers.Base; public abstract partial class SchemeSyncProviderBase { /// public Task DropUnknownStructuresAsync(bool dryRun = true, CancellationToken cancellationToken = default) where TProps : class => GuardSchemeSyncAsync(() => DropUnknownStructuresCoreAsync(dryRun, cancellationToken), cancellationToken); private async Task DropUnknownStructuresCoreAsync(bool dryRun, CancellationToken cancellationToken) where TProps : class { var scheme = await GetSchemeByTypeAsync(cancellationToken) ?? throw new InvalidOperationException( $"No scheme is stored for {typeof(TProps).FullName}: synchronise it before dropping its structures."); var stored = await Context.QueryFirstOrDefaultAsync(Sql.Schemes_SelectById(), new object[] { scheme.Id }, cancellationToken); var classVersion = GetRedbSchemeAttribute()?.Version ?? 0; var storedVersion = stored?.Version ?? 0; if (classVersion < storedVersion) throw new Exceptions.RedbPropsClassOlderException(scheme.Name, typeof(TProps), classVersion, storedVersion); var structures = await Context.QueryAsync(Sql.Structures_SelectByScheme(), new object[] { scheme.Id }, cancellationToken); var known = ClassStructureIds(typeof(TProps), structures.Cast().ToList()); var byId = structures.ToDictionary(s => s.Id); string PathOf(RedbStructure s) => s.IdParent is { } p && byId.TryGetValue(p, out var parent) ? PathOf(parent) + "." + s.Name : s.Name; var unknown = structures.Where(s => !known.Contains(s.Id)).ToList(); var items = new List(); foreach (var s in unknown.OrderBy(PathOf, StringComparer.Ordinal)) { var values = await Context.ExecuteScalarAsync( $"SELECT COUNT(*) FROM _values WHERE _id_structure = {s.Id}", Array.Empty(), cancellationToken) ?? 0; items.Add(new RedbDropReportItem(PathOf(s), s.Id, values, false)); } if (!dryRun && unknown.Count > 0) { BeforeSchemeWrite(); await Context.ExecuteAsync(Sql.Structures_DeleteByIds(unknown.Select(s => s.Id).ToList()), Array.Empty(), cancellationToken); await Context.ExecuteAsync(Sql.Schemes_SyncMetadataCache(), new object[] { scheme.Id }, cancellationToken); var remaining = await Context.QueryAsync(Sql.Structures_SelectBySchemeShort(), new object[] { scheme.Id }, cancellationToken); await Context.ExecuteAsync(Sql.Schemes_UpdateHash(), new object[] { SchemeHashCalculator.ComputeSchemeStructureHash(remaining), scheme.Id }, cancellationToken); Cache.InvalidateScheme(scheme.Id); Cache.InvalidateScheme(scheme.Name); items = items.Select(i => i with { Deleted = true }).ToList(); } var report = new RedbDropReport(scheme.Name, scheme.Id, classVersion, storedVersion, dryRun, items); if (items.Count > 0) Logger?.LogWarning("REDB DropUnknownStructuresAsync: {Report}", report.ToString()); return report; } }