Path: redb/redb.Tests.Integration/Tests/SqlitePro/SqliteProMigrationPropsCacheTests.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/SqliteProMigrationPropsCacheTests.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Pro.Extensions;
using redb.Core.Pro.Migration;
using redb.Core.Pro.Query;
using redb.SQLite.Pro.Extensions;
namespace redb.Tests.Integration.Tests.SqlitePro;
///
/// A Pro migration changes values in the database and leaves the object row as it was. The props cache serves an
/// entry while the stored object hash equals the cached one, so a process that cached the object before the migration
/// must not keep serving the values from before it.
///
public class SqliteProMigrationPropsCacheTests
{
private const string Cs = "Data Source=redb_tests_migration_props_cache_pro.db";
private static ServiceProvider Build(string node = "a")
{
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options =>
{
options.UseSqlite(Cs);
options.Configure(c =>
{
c.EnablePropsCache = true;
c.SkipHashValidationOnCacheCheck = false;
c.CacheDomain = $"migration-props-cache-{node}";
});
});
return services.BuildServiceProvider();
}
[Fact]
public async Task ObjectCachedBeforeAMigration_IsLoadedWithTheMigratedValues()
{
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
await using var provider = Build();
var redb = provider.GetRequiredService();
var dialect = provider.GetRequiredService();
await redb.InitializeAsync(ensureCreated: true);
await redb.SyncSchemeAsync();
var id = await redb.SaveAsync(new RedbObject
{ name = "cached-before-migration", Props = new MigrationAtomicityProbeProps { Amount = 5 } });
(await redb.LoadAsync(id))!.Props.Amount
.Should().Be(5, "precondition: the object is loaded, so it is in the props cache");
var results = await redb.MigrateAsync(dialect);
results.Should().OnlyContain(r => r.Success, "precondition: the migration applied");
var stored = await redb.Context.ExecuteScalarAsync(
$"SELECT v._Long FROM _values v JOIN _structures s ON s._id = v._id_structure " +
$"WHERE v._id_object = {id} AND s._name = 'Amount'");
stored.Should().Be(10, "precondition: the database holds the migrated value");
(await redb.LoadAsync(id))!.Props.Amount
.Should().Be(10, "the database holds 10 after the migration; the cache must not serve the value from before it");
}
[Fact]
public async Task ObjectCachedByAnotherProcessBeforeAMigration_IsLoadedThereWithTheMigratedValues()
{
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
await using var migrating = Build("migrating");
await using var other = Build("other");
var redb = migrating.GetRequiredService();
var otherRedb = other.GetRequiredService();
var dialect = migrating.GetRequiredService();
await redb.InitializeAsync(ensureCreated: true);
await otherRedb.InitializeAsync();
await redb.SyncSchemeAsync();
await otherRedb.SyncSchemeAsync();
var id = await redb.SaveAsync(new RedbObject
{ name = "cached-elsewhere", Props = new MigrationAtomicityProbeProps { Amount = 5 } });
(await otherRedb.LoadAsync(id))!.Props.Amount
.Should().Be(5, "precondition: the other process has the object in its props cache");
var results = await redb.MigrateAsync(dialect);
results.Should().OnlyContain(r => r.Success, "precondition: the migration applied");
(await otherRedb.LoadAsync(id))!.Props.Amount
.Should().Be(10, "the other process validates its cache by the stored object hash, which the migration cleared");
}
}