Path: redb/redb.Tests.Integration/Tests/SqlitePro/SqliteProCreateFailureTests.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/SqliteProCreateFailureTests.cs
----
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Pro.Extensions;
using redb.SQLite.Pro.Extensions;
namespace redb.Tests.Integration.Tests.SqlitePro;
///
/// A creation script that fails in the middle (here on an object the file already holds under a name the script creates
/// after the tables) must surface the error and leave no part of the schema behind: a file stamped as created with the
/// tables before the failure and nothing after it reads as a valid redb file, and a second start took the upgrade path
/// over it and reported success. Both attempts fail, and the file never holds _schemes or a schema version.
///
public class SqliteProCreateFailureTests
{
private const string Cs = "Data Source=redb_tests_create_failure_pro.db";
// An index the creation script builds after the tables and before the seed rows.
private const string NameTakenLater = "idx_metadata_cache_lookup";
[Fact]
public async Task CreationFailingInTheMiddle_ThrowsAndLeavesNoSchema_OnEveryAttempt()
{
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
await using (var blocker = new SqliteConnection(Cs + ";Pooling=False"))
{
await blocker.OpenAsync();
await using var cmd = blocker.CreateCommand();
cmd.CommandText = $"CREATE TABLE {NameTakenLater}(x INTEGER)";
await cmd.ExecuteNonQueryAsync();
}
for (var attempt = 1; attempt <= 2; attempt++)
{
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options => options.UseSqlite(Cs));
await using (var provider = services.BuildServiceProvider())
{
var redb = provider.GetRequiredService();
var act = () => redb.InitializeAsync(ensureCreated: true);
await act.Should().ThrowAsync($"attempt {attempt}: the failing statement must surface");
}
SqliteConnection.ClearAllPools();
await using var check = new SqliteConnection(Cs + ";Pooling=False");
await check.OpenAsync();
await using var q = check.CreateCommand();
q.CommandText = "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = '_schemes'";
((long)(await q.ExecuteScalarAsync())!).Should().Be(0, $"attempt {attempt}: no part of the schema stays");
q.CommandText = "PRAGMA user_version";
((long)(await q.ExecuteScalarAsync())!).Should().Be(0, $"attempt {attempt}: the file is not stamped");
}
}
}