Files
lmxopcua/docs/Reservations.md
T
Joseph Doherty 90de55535d fix(config): #1040 #1049 script FKs, unique ZTag/SAPID indexes, reservation ledger retired
One migration, ScriptForeignKeysAndExternalIdUniqueness:
- FK_VirtualTag_Script / FK_ScriptedAlarm_Script (Restrict) onto Script.ScriptId, which becomes
  NOT NULL and an alternate key (unique constraint UX_Script_LogicalId replaces the index).
- UX_Equipment_ZTag / UX_Equipment_SAPID filtered UNIQUE indexes (blank = no identifier, the
  deploy gate's reading) replace the non-unique IX_ ones.
- ExternalIdReservation + sp_ReleaseExternalIdReservation dropped (decision: retire — no writer
  since the generation model; uniqueness is gate + indexes). The /reservations page, query,
  ReservationKind, DraftSnapshot.ActiveReservations and the reservation pre-flight rule go too.
- A read-only pre-check THROWs 51049 naming every offending row class (NULL ScriptId, dangling
  VirtualTag/ScriptedAlarm script, duplicated ZTag/SAPID, any reservation row) and changes
  nothing; Down restores the previous shape.

Authoring mirrors so the constraints are never the first to say no: UnsTreeService refuses a
missing script for VirtualTag/ScriptedAlarm create/update, and a ZTag/SAPID another equipment
holds on create/update/import (owner named; import keeps the clean rows); values stored trimmed.
ScriptEdit maps a concurrent-bind FK refusal to a message. Migration pre-check, Down round-trip,
FK and index behaviour pinned against real SQL with positive controls.
2026-12-03 15:14:26 -05:00

32 lines
2.4 KiB
Markdown

# External-ID (ZTag / SAPID) uniqueness
> **The reservation ledger is retired (lmxopcua#1049, 2026-12).** `ExternalIdReservation`, the `/reservations`
> page, `sp_ReleaseExternalIdReservation` and the deploy gate's reservation pre-flight are gone. The ledger's only
> writer, `sp_PublishGeneration`, was retired with the generation model, so the table stayed empty and the
> pre-flight could never fire (review 2026-12-02, C-3). The permanent "a retired asset's id stays claimed" semantics
> of decision #124 were therefore never in force in v3, and are not provided now.
An equipment's **external identifiers** — its `ZTag` (ERP id) and `SAPID` (SAP PM id) — are unique **fleet-wide**:
no two `Equipment` rows, in the same cluster or different clusters, may carry the same value. Both are optional; a
blank value means "no identifier".
## Where it is enforced
| Layer | What | Comparison |
|---|---|---|
| Authoring (`UnsTreeService`) | Equipment create / update / CSV import refuse a value another equipment holds and name that equipment; an import names each clashing row (against the DB or an earlier row of the same file) and still imports the clean rows. Values are stored trimmed; blank is stored as `NULL`. | Trimmed; the DB's case-insensitive collation |
| Deploy gate (`DraftValidator.ValidateEquipmentExternalIdUniqueness`) | The authority: every duplicate in the whole-DB draft snapshot is reported as `BadDuplicateExternalIdentifier`, naming the other equipment. | Trimmed, case-insensitive, blank = none |
| Storage (`UX_Equipment_ZTag`, `UX_Equipment_SAPID`) | Filtered UNIQUE indexes, `WHERE [ZTag] IS NOT NULL AND [ZTag] <> N''` (same for `SAPID`) — the backstop for a concurrent save or a hand edit. Never stricter than the gate. | Column collation (case-insensitive) |
## Upgrading a populated ConfigDb
Migration `ScriptForeignKeysAndExternalIdUniqueness` creates the unique indexes and drops the ledger. It refuses
(error 51049, nothing changed) while any non-blank ZTag or SAPID is duplicated, or while `ExternalIdReservation`
still holds rows (export them if their history matters, then delete them). Run the upgrade preflight first — it lists
exactly those rows: [docs/v2/upgrade-preflight.md](v2/upgrade-preflight.md).
## Related
- `docs/v2/plan.md` — decision #124 (the original reservation design, historical).
- `docs/Raw.md` — the other deploy-gate uniqueness rules (#998).