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