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
+10
View File
@@ -174,6 +174,8 @@ builder.Services.AddZbLocalDb(builder.Configuration, db =>
"CREATE TABLE IF NOT EXISTS widget (id INTEGER PRIMARY KEY, name TEXT, qty INTEGER)")
.GetAwaiter().GetResult();
db.RegisterReplicated("widget"); // PK required; installs capture triggers
// The application assigns widget ids. A key SQLite assigns per node (AUTOINCREMENT, or a NULL
// rowid) collides across a pair: AUTOINCREMENT is refused, prefer TEXT GUIDs. See Limitations.
});
// Consume ILocalDb anywhere:
@@ -295,6 +297,14 @@ All options are validated at startup (`ZB.MOM.WW.Configuration` validators).
- **Exactly two nodes.** No mesh / N-node topologies.
- **LWW only.** No per-table pluggable conflict resolvers.
- **Replicated primary keys must be unique across both nodes.** LWW keys on the primary key,
so two different rows inserted on the two nodes under the same key are read as two versions
of one row, and one is lost on **both** nodes with no dead-letter and no metric. A key each
node assigns on its own cannot replicate: `RegisterReplicated` **refuses an `AUTOINCREMENT`
key** (`0.3.6`), and a plain `INTEGER PRIMARY KEY` is accepted only because the application
may assign it from a node-scoped range — left `NULL` on insert it loses rows the same way.
Use a TEXT GUID minted by the writer (what ScadaBridge's `site_events` and OtOpcUa's alarm
store-and-forward did when they moved onto LocalDb).
- **BLOB columns are rejected at registration** (a column declared `BLOB` fails
`RegisterReplicated`, because `json_object` cannot capture it). A *typeless* column can
still hold a BLOB value at runtime, which then surfaces as a write-time error, not a