how-to deployer

Runbook — World model deployment (Registers & Datasets)

How a deployment (sandbox or country) gets its world model, and how the model evolves safely in production.

Scope: this runbook covers the world-model catalog, compiled migrations, datasets, and the demo model. It does NOT cover SmartRules procedure authoring (see the editor runbook) or Keycloak role administration beyond the domain_steward notes below.


1. Where the model lives

  • The world model is a directory of YAML files (model.yaml, entities/, relationships/, datasets/) plus the platform’s read-only seed vocabulary (src/opengov/worldmodel/seed/).
  • The backend loads it from OPENGOV_WORLDMODEL_DIR. Default (unset): the packaged example (src/opengov/worldmodel/examples/d26-company-register/ — the Kenya company-register model used by the sandbox demo). A production deployment MUST set OPENGOV_WORLDMODEL_DIR to its own model directory, or it will serve the demo dataset.
  • The Studio edits a draft and publishes through the governance queue (propose → domain_steward approves → files are written to OPENGOV_WORLDMODEL_DIR). Never hand-edit the live model on a running instance.

2. How the model reaches the database

  1. The model validates (grammar rules 1–10, fail-closed): python -m opengov.worldmodel.compile <model_dir> --check <migration>

  2. The compiler emits a deterministic Alembic migration. The regenerate-and-diff drift guard (--check) must pass in CI — a hand-edited migration fails the gate.

  3. alembic upgrade heads applies it: typed wm_* tables, wm_history + triggers. Verify: \dt wm_* shows every entity/relationship table.

    heads, plural — do not “correct” this to head. On an instance that has approved a publish, the migration chain has two branches: the ones shipped in the image, and the publishes written to this instance’s own volume. Singular head refuses to choose between them, and the deployment fails to start. This took the sandbox down twice in two days before the plural form landed. Background: issue #1378.

  4. Additive changes (new entity, new nullable field) compile to ALTERs on the same tables — never table-per-version. Breaking changes require an explicit major version in model.yaml and a migration note reviewed by the steward.

3. The sandbox demo model

The sandbox runs the packaged example (unset OPENGOV_WORLDMODEL_DIR): the Kenya world (company, person, address, government) with two registers — the Company register (registration) and name reservations. The public dataset company_directory is served anonymously at GET /api/public/v1/datasets/company_directory. Redeploys reset demo data unless a seeding job re-runs (see the demo seeding script under scripts/).

4. Production checklist (per country)

  • OPENGOV_WORLDMODEL_DIR set to the country’s model directory (mounted read-write for the API/worker containers; owned by the steward workflow).
  • The country’s model reviewed and approved through the governance queue — not copied by hand.
  • domain_steward role assigned to the country’s authorized person(s) in Keycloak; registrars hold registrar for corrections.
  • alembic upgrade heads after each model publish (the publish preview shows the compiled migration before approval).
  • Datasets: each datasets/*.yaml validates (definition-time privacy gate); after the first publish, spot-check one public dataset for private-field absence.
  • Nightly reconciliation job visible in the worker; first week’s drift reports reviewed (they are alert-only by design).

5. Troubleshooting

SymptomLikely causeAction
GET /api/public/v1/datasets/<key> 404dataset not defined in the active model dir, or OPENGOV_WORLDMODEL_DIR unset/mispointedcheck the env var; load_and_validate the dir locally
publish-preview 422model violates grammar rules 1–10the response body lists every violation in plain language
compiled migration won’t applyhand-edited migration (drift)regenerate via the compiler; never hand-edit
correction 403caller lacks registrar/adminassign the role in Keycloak
history missing for a recordwrites bypassed materialize_filing/correct_record (raw SQL)never write wm_* outside the runtime; restore from backup if needed