Files
ukmesh/docs/contributing.md
T
hermes-gadget 9c8c3eda40 fix(db): let a fresh database replay the migration band past the legacy moves (#214)
* fix(db): let a fresh database replay the migration band past the legacy moves

A database rebuilt from the shipped base schema could never reach head. The
band stopped at 078 because the P1-4 moves re-home relations that no migration
in this tree creates: `message_tags_local`, the `path_sim_*` simulation tables,
the derived analytics tables and the privacy cluster exist only on databases
provisioned before the move. `ALTER TABLE public.x SET SCHEMA` therefore died
with `relation "public.x" does not exist`, and 087 failed separately because
nothing in the repo ever created the `planner_export` schema its view lives in.

Each legacy-dependent move is now guarded with to_regclass() and emits a NOTICE
when it skips, so a base-schema database continues while a legacy-provisioned
database still executes the identical statement. 087 creates `planner_export`
with IF NOT EXISTS, which leaves an operator-provisioned schema, its owner and
its grants untouched.

Behaviour on a database that has the legacy relations is unchanged: replaying
the band over an identical legacy-provisioned fixture with the origin files and
with these files produces a byte-for-byte identical `pg_dump --schema-only`
(6211 lines), and the six trigger functions plus the export view definition are
textually identical.

Tests: migrationBootstrap.integration.test.ts already ran a two-migration
subset because of this assumption. legacyMoveReplay.integration.test.ts now
replays the whole band on a fresh seeded database (head
135_packet_identity_backfill_chunks.sql, 131 ledger rows) and separately proves
the guarded moves still move the relations, carrying their index, comment and
grants.

* ci: gate the fresh-volume replay before the ingest group

test:migration-integration was the tail of test:ingest-integration, so a fresh
volume that could not reach migration head only showed up after the whole ingest
group passed - and today that group is red on the branch for unrelated reasons,
which hides the gate entirely. Run it as its own named step ahead of the ingest
group instead, and drop the duplicated tail invocation.

* fix(db): accept the pre-guard checksums for the four legacy moves

Production already recorded 078/080/081/087 with their pre-guard contents.
Without this, the next migrate run would report checksum drift on all four
and refuse. The guarded files execute the same statements wherever the
relations exist - the pre/post databases were diffed with pg_dump
--schema-only and matched byte for byte.
2026-11-05 17:57:21 +00:00

1.9 KiB

Contributing

Structural expectations

  • routes should stay thin
  • services own orchestration
  • repositories own SQL
  • worker RF math should stay separate from queue orchestration
  • frontend map builders should stay separate from MapLibreMap.tsx

Repository hygiene

Do not commit:

  • secrets
  • credentials
  • private database dumps
  • personal or personally identifiable information
  • operational traces containing sensitive infrastructure details
  • artifacts that could be used for abuse or malicious access

If in doubt, keep it out of Git and add it to .gitignore.

DB changes

  • backend/src/db/schema/base.sql represents a fresh database. Any schema change required by an existing deployment must be added as a new, ordered migration as well; do not edit a migration that may already be recorded.
  • no migration may assume a relation that no migration creates. A fresh database replays the whole band from base.sql, so an object that only exists on older deployments must be guarded (IF to_regclass('public.x') IS NOT NULL) with a NOTICE when the guard skips, and both paths need a test: the band must reach head on a base-schema-only database, and the guarded statement must still run unchanged where the object is present.
  • whole-history or destructive fixes go in explicit maintenance scripts, never migrations or startup
  • apply pending migrations with docker compose run --rm db-migrate, not by running individual migration files or inserting migration-ledger rows by hand

Testing expectations

Before finishing a refactor or behavior change:

  • use Node 20 and run cd backend && npm ci && npm run typecheck && npm test && npm run build
  • run cd frontend && npm ci && npm test && npm run build && npm run test:e2e
  • if worker code changed, build its image and run its tests there so GDAL and osgeo exactly match production
  • run docker compose config --quiet
  • rebuild affected containers and check http://localhost:3000/healthz