Path: redb/redb.Tests.Integration/Fixtures/SqliteFixture.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/Fixtures/SqliteFixture.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models.Configuration;
using redb.SQLite.Data;
using redb.SQLite.Extensions;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Fixtures;
///
/// SQLite Free fixture — hosts the in-DB machinery as the native loadable extension
/// (get_object_json / pvt_build_*_sql / ...). The native binary is located by walking up to
/// redb.SQLite/native/build/redbsqlite.{dll,so,dylib}. A fresh DB file is used every run (the datetime
/// columns are REAL Julian and an old TEXT-schema file is not auto-migrated).
///
public sealed class SqliteFixture : IAsyncLifetime
{
public IRedbService Redb { get; private set; } = null!;
public ServiceProvider ServiceProvider { get; private set; } = null!;
public async Task InitializeAsync()
{
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var cs = config.GetConnectionString("Sqlite")!;
SqliteDataSource.NativeExtensionPath ??= SqliteTestSupport.ResolveNativeExtension();
SqliteTestSupport.DeleteDbFiles(cs);
var services = new ServiceCollection();
services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning));
services.AddRedb(options => options
.UseSqlite(cs)
.Configure(c =>
{
c.PropsSaveStrategy = PropsSaveStrategy.DeleteInsert;
c.SkipHashValidationOnCacheCheck = false;
c.EnablePropsCache = false;
}));
ServiceProvider = services.BuildServiceProvider();
Redb = ServiceProvider.GetRequiredService();
try { await Redb.InitializeAsync(ensureCreated: true); }
catch { await Redb.InitializeAsync(); }
await SyncSchemes();
await Cleanup();
}
private async Task SyncSchemes()
{
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.InitializeTypeRegistryAsync();
}
private async Task Cleanup()
{
var ctx = ServiceProvider.GetRequiredService();
try { await ctx.ExecuteAsync("DELETE FROM _tree"); } catch { }
await ctx.ExecuteAsync("DELETE FROM _values");
await ctx.ExecuteAsync("DELETE FROM _objects");
}
public async Task DisposeAsync()
{
if (ServiceProvider is IAsyncDisposable ad)
await ad.DisposeAsync();
else
ServiceProvider?.Dispose();
}
}