Path: redb-route/tests/redb.Route.Tests.Llm/Storage/NamedRedbFixture.cs Snapshot: redb-route, 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-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Llm/Storage/NamedRedbFixture.cs ---- using System.Data.Common; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Npgsql; using redb.Core; using redb.Core.Models.Configuration; using redb.Core.Pro.Extensions; using redb.MSSql.Pro.Extensions; using redb.Postgres.Pro.Extensions; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.RedbCore.Extensions; using redb.Route.Llm.Storage.Redb.Schemas; using redb.SQLite.Pro.Extensions; namespace redb.Route.Tests.Llm.Storage; /// /// A second redb Pro instance in a database of its own, next to the default one of : the /// "agent memory" database of a host that keeps the agent's data apart from its business data. Registered on a route /// context under with , so ?redb= and a registration name can pick it. /// The same provider as the default instance (REDB_PROVIDER); the database is created when it is missing. On a /// server its name is the default database's with (redb gives redb_agent_memory), so a /// run that points the default connection string at a database of its own gets a named database of its own too. /// public sealed class NamedRedbFixture : IAsyncLifetime { /// The name the instance is registered under. public const string Name = "agent-memory"; /// Appended to the default database's name for the named instance's database on a server. public const string Suffix = "_agent_memory"; private const string SqliteFile = "redb_llm_agent_memory.db"; private ServiceProvider? _services; /// The named instance. public IRedbService Redb { get; private set; } = null!; private static string Provider => Environment.GetEnvironmentVariable("REDB_PROVIDER")?.ToLowerInvariant() is { Length: > 0 } p ? p : "sqlite"; /// public async Task InitializeAsync() { var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); var services = new ServiceCollection(); services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning)); switch (Provider) { case "postgres": { var cs = new NpgsqlConnectionStringBuilder( global::redb.Route.Tests.Shared.TestDatabases.Postgres(config.GetConnectionString("Postgres")!)); cs.Database += Suffix; await EnsurePostgresDatabaseAsync(cs); services.AddRedbPro(o => o.UsePostgres(cs.ConnectionString).Configure(Settings)); break; } case "mssql": { var cs = new SqlConnectionStringBuilder( global::redb.Route.Tests.Shared.TestDatabases.MsSql(config.GetConnectionString("MsSql")!)); cs.InitialCatalog += Suffix; await EnsureMsSqlDatabaseAsync(cs); services.AddRedbPro(o => o.UseMsSql(cs.ConnectionString).Configure(Settings)); break; } default: services.AddRedbPro(o => o.UseSqlite($"Data Source={SqliteFile}").Configure(Settings)); break; } _services = services.BuildServiceProvider(); Redb = _services.GetRequiredService(); try { await Redb.InitializeAsync(ensureCreated: true); } catch { await Redb.InitializeAsync(); } await Redb.SyncSchemeAsync(); await Redb.SyncSchemeAsync(); await Redb.SyncSchemeAsync(); await Redb.SyncSchemeAsync(); await Redb.SyncSchemeAsync(); } private static void Settings(RedbServiceConfiguration c) { c.PropsSaveStrategy = PropsSaveStrategy.ChangeTracking; c.EnablePropsCache = false; } /// Registers the instance on under . public void AttachTo(IRouteContext context) => context.RegisterRedbService(Name, Redb); private static async Task EnsurePostgresDatabaseAsync(NpgsqlConnectionStringBuilder target) { var database = target.Database!; var admin = new NpgsqlConnectionStringBuilder(target.ConnectionString) { Database = "postgres" }; await using var connection = new NpgsqlConnection(admin.ConnectionString); await connection.OpenAsync(); await using var exists = new NpgsqlCommand("SELECT 1 FROM pg_database WHERE datname = @n", connection); exists.Parameters.AddWithValue("n", database); if (await exists.ExecuteScalarAsync() is null) { await using var create = new NpgsqlCommand($"CREATE DATABASE \"{database}\"", connection); await create.ExecuteNonQueryAsync(); } } private static async Task EnsureMsSqlDatabaseAsync(SqlConnectionStringBuilder target) { var database = target.InitialCatalog; var admin = new SqlConnectionStringBuilder(target.ConnectionString) { InitialCatalog = "master" }; await using var connection = new SqlConnection(admin.ConnectionString); await connection.OpenAsync(); await using var create = new SqlCommand($"IF DB_ID(N'{database}') IS NULL CREATE DATABASE [{database}]", connection); await create.ExecuteNonQueryAsync(); } /// public async Task DisposeAsync() { if (_services is not null) await _services.DisposeAsync(); } }