fix(localdb): refuse AUTOINCREMENT keys at RegisterReplicated (LocalDb-005)

Each node assigns its own AUTOINCREMENT keys, so two different rows inserted
on the two nodes share a pk_json and LWW silently keeps one on both nodes.
RegisterReplicated now refuses such a table (detected from SQLite column
metadata, before any trigger is installed). A plain INTEGER PRIMARY KEY is
still accepted (application-assigned ids are legitimate); the requirement
that replicated keys be unique across both nodes is documented in the
RegisterReplicated remarks and README Limitations. No ScadaBridge or OtOpcUa
replicated table is AUTOINCREMENT.

(scadaproj#46)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:09:45 -05:00
parent 76e9c355b4
commit fd46999946
5 changed files with 142 additions and 6 deletions
@@ -70,6 +70,58 @@ public sealed class RegistrationTests : IDisposable
Assert.Contains("BLOB", ex.Message);
}
// ---- LocalDb-005 (scadaproj#46): AUTOINCREMENT keys are assigned independently on each node ----
[Fact]
public async Task Register_AutoincrementKey_Throws()
{
// Each node hands out 1, 2, 3 … on its own, so two DIFFERENT rows inserted on the two nodes
// share a pk_json; LWW reads them as two versions of one row and one is lost on both nodes.
using var db = Create();
await db.ExecuteAsync("CREATE TABLE ev (id INTEGER PRIMARY KEY AUTOINCREMENT, msg TEXT)");
var ex = Assert.Throws<LocalDbRegistrationException>(() => db.RegisterReplicated("ev"));
Assert.Contains("ev", ex.Message);
Assert.Contains("AUTOINCREMENT", ex.Message);
// Refused before anything was installed.
Assert.Empty(await db.QueryAsync(
"SELECT name FROM sqlite_master WHERE type='trigger' AND tbl_name='ev'", r => r.GetString(0)));
}
[Fact]
public async Task Register_AutoincrementKey_Throws_WithBaselineToo()
{
using var db = Create();
await db.ExecuteAsync("CREATE TABLE ev (id INTEGER PRIMARY KEY AUTOINCREMENT, msg TEXT)");
Assert.Throws<LocalDbRegistrationException>(() => db.RegisterReplicated("ev", baselineExistingRows: true));
}
[Fact]
public async Task Register_PlainIntegerPrimaryKey_IsStillAllowed()
{
// Compatibility guard: an INTEGER PRIMARY KEY the application assigns itself (node-scoped or
// otherwise globally unique ids) is legitimate, and refusing it would break existing schemas.
using var db = Create();
await CreateOrders(db);
db.RegisterReplicated("orders");
Assert.True(db.ReplicatedTables.ContainsKey("orders"));
}
[Fact]
public async Task Register_TheWordAutoincrementOutsideTheKey_IsNotMistakenForIt()
{
// Detection reads SQLite's own column metadata, not the CREATE text, so a column NAMED
// autoincrement or a default value spelling it does not trip the check.
using var db = Create();
await db.ExecuteAsync(
"CREATE TABLE notes (id TEXT PRIMARY KEY, \"autoincrement\" INTEGER, note TEXT DEFAULT 'AUTOINCREMENT')");
db.RegisterReplicated("notes");
Assert.True(db.ReplicatedTables.ContainsKey("notes"));
}
[Fact]
public async Task Register_CascadeDeleteParent_Throws()
{