Path: redb/redb.Tests.Integration/Fixtures/SqliteProFixture.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/SqliteProFixture.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Data;
using redb.Core.Models.Configuration;
using redb.Core.Pro.Extensions;
using redb.SQLite.Pro.Extensions;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Fixtures;
///
/// SQLite Pro fixture — pure C# (ProSqlBuilder + C# materialization, no DB-side functions),
/// so no native extension is needed. Uses a separate fresh DB file from the Free fixture to avoid
/// cross-collection contention on the single-writer SQLite file.
///
public sealed class SqliteProFixture : IAsyncLifetime
{
public IRedbService Redb { get; private set; } = null!;
public ServiceProvider ServiceProvider { get; private set; } = null!;
/// The fixture's own database file: a test of the collection that builds a host of its own uses this one.
public string ConnectionString { get; private set; } = null!;
public async Task InitializeAsync()
{
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
// Own DB file: the Free and Pro collections run in PARALLEL (the old comment assumed
// sequential and was wrong), and SQLite is a single-writer engine - two collections
// hammering one file starve each other on the write lock for minutes (gate hang,
// 2026-09-09: NextResult lost the non-FIFO lock race to the other collection's stream
// of short transactions over and over).
var cs = System.Text.RegularExpressions.Regex.Replace(
config.GetConnectionString("Sqlite")!,
@"Data Sources*=s*[^;]+", "Data Source=redb_tests_sqlite_pro.db");
var license = config["Redb:License"];
// The native extension path is static for the whole process, and several tests set it (every
// Free fixture, and Pro tests built on Free ones). Once it is set, InitializeAsync checks the
// extension's version on the connection it is given; a connection this fixture had opened while
// the path was still empty carried no extension, and that check failed with "no
// pvt_module_version() function" for whichever Pro test happened to run after such a test. Set
// here, before the first connection, every connection of the fixture loads the extension - harmless
// for Pro, which does not call it - and the order of the tests no longer matters.
redb.SQLite.Data.SqliteDataSource.NativeExtensionPath ??= SqliteTestSupport.ResolveNativeExtension();
SqliteTestSupport.DeleteDbFiles(cs);
ConnectionString = cs;
var services = new ServiceCollection();
services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options =>
{
options.UseSqlite(cs)
.Configure(c =>
{
c.PropsSaveStrategy = PropsSaveStrategy.ChangeTracking;
c.SkipHashValidationOnCacheCheck = false;
c.EnablePropsCache = false;
c.EnablePvtPrefilter = ProTestOptions.PvtPrefilter;
});
if (!string.IsNullOrWhiteSpace(license))
options.WithLicense(license);
});
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();
}
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();
}
}