# Upgrade preflight (`otopcua-preflight`) A **read-only** check of an OtOpcUa ConfigDb, run from the **new** build **before** its migrations are applied. It reports every row that the upgrade — or the first deploy after it — would refuse, so the clean-up happens before the maintenance window instead of in the middle of it. It issues only `SELECT`s. Introduced for the 2026-12-02 review remediation follow-ups lmxopcua#1013, #1015, #1021 and #1058. ## What it checks | Section | What | Severity | Issue | |---|---|---|---| | `schema` | Applied vs pending migrations; tables/columns a pending migration creates (read as empty/default — see below); whether `ALLOW_SNAPSHOT_ISOLATION` is on. | Info / Warning | — | | `migration:ConfigurationGapsIndexesAndRowVersions` | Duplicate `NodeAcl` rows per (cluster, principal, scope kind, scope id) — typically several Cluster-scope rows for one principal — and duplicate `LdapGroupRoleMapping` rows per (group, cluster), incl. system-wide. The migration THROWs on either. | Blocking | #1021 | | `migration:ScriptForeignKeysAndExternalIdUniqueness` | `Script.ScriptId` NULL; a VirtualTag / ScriptedAlarm naming a script that does not exist; a non-blank ZTag or SAPID on more than one equipment (case-insensitive, as the new unique index); any `ExternalIdReservation` row (the table is dropped). The migration THROWs on any of them. | Blocking | #1040, #1049 | | `deploy-gate` | The deploy gate's own rule pass (`DeployGateRules` — the code `AdminOperationsActor` runs): every `DraftValidator` rule, incl. the #978 rules `VirtualTagNameInvalid`, `UnsEffectiveNameInvalid` (illegal VirtualTag names / display-name overrides), `VirtualTagScriptNotFound`, `ScriptedAlarmScriptNotFound` (dangling script references), the #998 rules (`BadDuplicateExternalIdentifier`, `DanglingReference`, `HistorianTagnameDuplicate`), and `SqlNamedQueryInvalid`; the per-cluster topology check; the TagConfig inspection (Warning unless `--tagconfig-mode Error`). | Blocking (rules) / Warning | #1015, #1058 | | `script-gate` | Every script the runtime will compile (enabled VirtualTags, enabled scripted-alarm predicates, Calculation tags, `{{equip}}` resolved) through the Roslyn compile + sandbox allow-list (`RoslynDeployScriptChecker`, as the deploy gate). Also every **unused** stored script, compiled as a value script — a Warning, since binding it later would be refused. | Blocking (Error mode) / Warning | #1013 | `SqlNamedQueryInvalid` (#1058) is a deploy-gate rule added with the preflight: the Sql driver checks every named query with `SqlNamedQueryRules.Describe` under its provider at construction and one refused query fails the whole driver. Since #974 that includes a T-SQL ScriptDom parse for SqlServer, Odbc and no-provider configs, so an **Odbc** query written in another dialect (`LIMIT`, `ILIKE`, `'a'::text`, `(x)::numeric`) is refused — the deploy gate now says so before the driver goes dark. (A bare `col::type` parses as T-SQL static-member syntax and is accepted.) ### Reading an older schema The preflight reads the snapshot through **this build's** EF model, but the database is still on the old schema. A table a pending migration creates is mapped to a typed zero-row query (it will be empty right after the migration); a column a pending migration adds is left out of the model (the property keeps its default). The `schema` section lists exactly which. A schema change the tolerance cannot bridge (e.g. a NULL in a column the new model requires) is reported as `deploy-gate/SnapshotUnreadable`; fix the migration pre-check findings and re-run. Only v3 ConfigDbs (with `20260715230637_V3Initial` applied) are accepted; anything else exits 2. ## Running it Build or publish it from the **new** source tree (it must carry the new rules and migrations): ```bash dotnet publish src/Tooling/ZB.MOM.WW.OtOpcUa.UpgradePreflight -c Release -o out/preflight # Windows target: add -r win-x64 --self-contained to run it on the box without an SDK ``` Pass the connection string through the environment (a command-line argument is visible in the process list and shell history). A login with `db_datareader` on the ConfigDb is enough. ```bash export OTOPCUA_CONFIG_CONNECTION='Server=,;Database=;User Id=;Password=<...>;TrustServerCertificate=True' out/preflight/otopcua-preflight # human-readable; Blocking + Warning findings out/preflight/otopcua-preflight --verbose # + Info (pending migrations, clean sections) out/preflight/otopcua-preflight --json > preflight.json ``` | Option | Default | Meaning | |---|---|---| | `--connection ""` | `OTOPCUA_CONFIG_CONNECTION` | ConfigDb connection string | | `--tagconfig-mode Warn\|Error` | `Warn` | the target's `Deployment:TagConfigValidationMode` | | `--script-gate Error\|Warn\|Off` | `Error` | the target's `Deployment:ScriptCompileGateMode` | | `--no-unused-scripts` | off | skip compiling scripts nothing uses | | `--json` / `--verbose` | off | output format | **Exit codes:** `0` nothing blocks the upgrade or the next deploy · `1` at least one Blocking finding · `2` usage error, unreachable database, or not a v3 ConfigDb. ## Upgrade procedure 1. On the running (old) build's ConfigDb, run the preflight from the new build. It is read-only and safe to run against a live database; under SNAPSHOT isolation it does not block editors. 2. Fix every Blocking finding through the AdminUI (re-point or delete the VirtualTag/ScriptedAlarm, clear the duplicate ZTag, merge the duplicate ACL rows, rewrite the Odbc query, fix the script) — or, for `ExternalIdReservation` rows, export them if their history matters and delete them. Re-run until exit 0. 3. Apply the new build (its migrations). A migration that still finds a bad row THROWs (error 50998 / 51049) and changes nothing; re-run the preflight to see why. 4. Deploy. The deploy gate runs the same rules the preflight ran. ## Results recorded | Date | Target | Schema at | Result | |---|---|---|---| | 2026-12-03 | docker-dev ConfigDb `OtOpcUa` (read from a copy of the stopped rig's SQL volume; the rig was not touched) | `EnableSnapshotIsolation` (6 pending) | exit 0 — both migration pre-checks clean, deploy gate clean, 3 in-use scripts compile; no Sql drivers | | 2026-12-03 | `10.100.0.35,14330` / `OtOpcUaCfg_gate_scratch` | `V3Initial` (14 pending) | exit 0 — empty config; the NodeAcl duplicate pre-check could not run on the pre-#520 `LdapGroup` column (Warning) | | 2026-12-03 | `10.100.0.35,14330` / `OtOpcUaConfig`, `OtOpcUaConfig_Dev` | v2 schema | exit 2 — not v3 ConfigDbs (nothing to check) | | — | **production (wonder-app-vd03 / vq03 / vq04)** | — | **not run — user-gated.** Remote commands on the wonder boxes are on hold (see `CLAUDE.md`); an operator with SQL access runs step 1 there. |