Path: redb/redb.Tests.Integration/Tests/Base/SchemaInitTestsBase.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/Base/SchemaInitTestsBase.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Export.Services;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// redb init () on PostgreSQL and SQL Server: into an empty database it creates the
/// schema with the module this build requires; a database that already holds the redb schema is refused before anything
/// runs. Run over an existing database, the SQL Server script dropped every foreign key and the id sequence before its
/// first CREATE TABLE failed, and the database went on without them. Scratch databases of their own, dropped afterwards.
///
public abstract class SchemaInitTestsBase
{
/// postgres or mssql.
protected abstract string ProviderName { get; }
/// The module version this build requires.
protected abstract string RequiredVersion { get; }
/// A new empty database of this run; the subclass drops it afterwards.
protected abstract Task NewDatabaseAsync(string suffix);
/// A redb service over with a cache domain of its own.
protected abstract ServiceProvider Build(string cs);
/// The first column of the first row as text.
protected abstract Task ScalarAsync(string cs, string sql);
/// The sequences, the foreign keys, the triggers with their state, the module version.
protected abstract Task> CatalogAsync(string cs);
[Fact]
public async Task Init_IntoAnEmptyDatabase_CreatesTheSchemaWithTheRequiredModule()
{
var cs = await NewDatabaseAsync("init");
await SchemaInitService.InitializeAsync(ProviderName, cs);
(await CatalogAsync(cs)).Should().Contain($"module {RequiredVersion}");
await using var provider = Build(cs);
await provider.GetRequiredService().InitializeAsync(typeof(ExportService).Assembly);
}
[Fact]
public async Task ASecondInit_IsRefused_AndTouchesNothing()
{
var cs = await NewDatabaseAsync("again");
await SchemaInitService.InitializeAsync(ProviderName, cs);
await using (var provider = Build(cs))
{
var redb = provider.GetRequiredService();
await redb.InitializeAsync(typeof(ExportService).Assembly);
await redb.SyncSchemeAsync();
await redb.SaveAsync(new RedbObject { name = "kept", Props = new SimpleProps { Title = "k" } });
}
var before = await StateAsync(cs);
before.Should().Contain(l => l.StartsWith("fk ", StringComparison.Ordinal), "precondition: init created the foreign keys");
before.Should().Contain(l => l.StartsWith("sequence ", StringComparison.Ordinal), "precondition: init created the id sequence");
var initAgain = async () => await SchemaInitService.InitializeAsync(ProviderName, cs);
(await initAgain.Should().ThrowAsync()).Which.Message
.Should().Contain("already holds the redb schema").And.Contain("has changed nothing")
.And.Contain($"redb schema --upgrade --provider {ProviderName}");
(await StateAsync(cs)).Should().Equal(before);
}
private async Task> StateAsync(string cs)
{
var lines = await CatalogAsync(cs);
foreach (var table in new[] { "_schemes", "_structures", "_objects", "_values", "_users", "_roles" })
lines.Add($"{table}={await ScalarAsync(cs, $"SELECT COUNT(*) FROM {table}")}");
return lines;
}
}