fix(localdb): capture triggers survive consumer upserts (explicit ON CONFLICT upsert form, outer-statement override)

This commit is contained in:
Joseph Doherty
2026-07-18 00:57:27 -04:00
parent 801b042208
commit 154d584190
4 changed files with 88 additions and 31 deletions
@@ -245,6 +245,36 @@ public sealed class CaptureTriggerTests : IDisposable
Assert.Equal(o.Hlc, rv.Single(v => v.PkJson == o.PkJson).Hlc);
}
// Pins the explicit ON CONFLICT upsert form in the row_version capture statements: SQLite
// replaces a trigger-body statement's OR REPLACE with the OUTER statement's conflict handling,
// so a consumer `INSERT ... ON CONFLICT DO UPDATE` used to turn the au trigger's OR REPLACE
// into a plain INSERT -> SQLITE_CONSTRAINT_PRIMARYKEY (1555) on the existing row_version row.
[Fact]
public async Task Upsert_OnConflictDoUpdate_BothBranches_Capture()
{
using var db = await NewOrdersDb();
const string upsert =
"INSERT INTO orders (id, sku, qty) VALUES (7, @sku, @qty) " +
"ON CONFLICT(id) DO UPDATE SET sku = excluded.sku, qty = excluded.qty";
await db.ExecuteAsync(upsert, new { sku = "FIRST", qty = 1 }); // insert branch -> ai
await db.ExecuteAsync(upsert, new { sku = "SECOND", qty = 2 }); // update branch -> au
var oplog = await Oplog(db);
Assert.Equal(2, oplog.Count);
Assert.Equal("{\"id\":7,\"sku\":\"FIRST\",\"qty\":1}", oplog[0].RowJson);
Assert.Equal("{\"id\":7,\"sku\":\"SECOND\",\"qty\":2}", oplog[1].RowJson);
Assert.All(oplog, o => Assert.Equal(0, o.IsTombstone));
var rv = await db.QueryAsync(
"SELECT pk_json, hlc, is_tombstone FROM __localdb_row_version WHERE table_name='orders'",
r => (PkJson: r.GetString(0), Hlc: r.GetInt64(1), Tomb: r.GetInt64(2)));
var v = Assert.Single(rv);
Assert.Equal("{\"id\":7}", v.PkJson);
Assert.Equal(0, v.Tomb);
Assert.Equal(oplog[1].Hlc, v.Hlc);
}
[Fact]
public async Task Capture_CoexistsWithConsumerTrigger()
{