Path: redb/redb.Tests.Integration/Tests/SqlitePro/SqliteProMigrationAtomicityTests.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/SqliteProMigrationAtomicityTests.cs ---- using AwesomeAssertions.Execution; using Microsoft.Extensions.DependencyInjection; using redb.Core; using redb.Core.Attributes; using redb.Core.Models.Entities; using redb.Core.Pro.Migration; using redb.Core.Pro.Query; using redb.Tests.Integration.Fixtures; namespace redb.Tests.Integration.Tests.SqlitePro; [RedbScheme("Migrations: atomicity probe", Name = "migration_atomicity_probe")] public class MigrationAtomicityProbeProps { public long Amount { get; set; } } /// A migration that is not idempotent: run twice, it doubles twice. public class MigrationDoubleAmountMigration : IRedbMigration { public void Configure(IMigrationBuilder builder) { builder.Property(p => p.Amount).ComputedFrom(p => p.Amount * 2); } } /// /// A migration's UPDATE of the values and its row in _migrations are one unit, and a failure comes back as /// Success = false. When the history insert fails after the UPDATE, the values must stay as they were, or the /// next run applies a non-idempotent computation a second time. The failure between the two is a trigger on /// _migrations for the probe scheme only. /// [Collection("SqlitePro")] public class SqliteProMigrationAtomicityTests { private readonly IRedbService _redb; private readonly ISqlDialectPro _dialect; public SqliteProMigrationAtomicityTests(SqliteProFixture fixture) { _redb = fixture.Redb; _dialect = fixture.ServiceProvider.GetRequiredService(); } private const string Trigger = "tr_test_migration_history_fails"; [Fact] public async Task HistoryInsertFails_AfterTheUpdate_TheRerunDoesNotApplyTheTransformTwice() { var (schemeId, id) = await SeedAsync("migration-atomicity"); await CreateFailingTriggerAsync(schemeId); List first; try { first = await _redb.MigrateAsync(_dialect); } finally { await _redb.Context.ExecuteAsync($"DROP TRIGGER IF EXISTS {Trigger}"); } first.Should().ContainSingle(r => !r.Success && r.Error!.Contains("injected"), "precondition: the run failed at the history insert"); var afterFailure = (await _redb.LoadAsync(id))!.Props.Amount; var second = await _redb.MigrateAsync(_dialect); second.Should().OnlyContain(r => r.Success); var afterRerun = (await _redb.LoadAsync(id))!.Props.Amount; using (new AssertionScope()) { afterFailure.Should().Be(5, "a migration that failed changed nothing"); afterRerun.Should().Be(10, "the transform is applied once, by the run that succeeded"); } } [Fact] public async Task HistoryInsertFails_InACallerTransaction_TheTransactionStaysUsableAndNothingIsApplied() { var (schemeId, id) = await SeedAsync("migration-atomicity-caller"); await CreateFailingTriggerAsync(schemeId); List first; try { await using var tx = await _redb.Context.BeginTransactionAsync(); first = await _redb.MigrateAsync(_dialect); await _redb.Context.ExecuteAsync($"UPDATE _objects SET _name = 'written-after-the-failure' WHERE _id = {id}"); await tx.CommitAsync(); } finally { await _redb.Context.ExecuteAsync($"DROP TRIGGER IF EXISTS {Trigger}"); } first.Should().ContainSingle(r => !r.Success && r.Error!.Contains("injected"), "precondition: the run failed at the history insert"); var loaded = (await _redb.LoadAsync(id))!; var history = await _redb.Context.ExecuteScalarAsync($"SELECT COUNT(*) FROM _migrations WHERE _scheme_id = {schemeId}"); using (new AssertionScope()) { loaded.name.Should().Be("written-after-the-failure", "the caller's transaction stayed usable and committed"); loaded.Props.Amount.Should().Be(5, "the failed migration changed nothing inside the caller's transaction"); history.Should().Be(0, "the failed migration left no history row"); } } private async Task<(long SchemeId, long Id)> SeedAsync(string name) { var scheme = await _redb.SyncSchemeAsync(); await _redb.Context.ExecuteAsync($"DELETE FROM _migrations WHERE _scheme_id = {scheme.Id}"); await _redb.Context.ExecuteAsync($"DELETE FROM _objects WHERE _id_scheme = {scheme.Id}"); var id = await _redb.SaveAsync(new RedbObject { name = name, Props = new MigrationAtomicityProbeProps { Amount = 5 } }); return (scheme.Id, id); } private Task CreateFailingTriggerAsync(long schemeId) => _redb.Context.ExecuteAsync( $"CREATE TRIGGER {Trigger} BEFORE INSERT ON _migrations WHEN NEW._scheme_id = {schemeId} " + "BEGIN SELECT RAISE(ABORT, 'injected: history insert fails'); END"); }