feat(tamanu): TAMANU_DATABASE_URL env override for DB connection#502
Open
passcod wants to merge 1 commit into
Open
feat(tamanu): TAMANU_DATABASE_URL env override for DB connection#502passcod wants to merge 1 commit into
passcod wants to merge 1 commit into
Conversation
0c1ba7d to
d9633d0
Compare
Every bestool command that connects to or emits the Tamanu database connection now honours a TAMANU_DATABASE_URL environment variable, using it instead of the database section of the Tamanu config. - database_url() returns the override verbatim (preserving query params like sslmode and Unix-socket forms); database() parses it into fields via tokio-postgres's own connection-string parser for the consumers that need host/user/password/name (backup, greenmask, db_url, psql). - db_url and psql no longer require a Tamanu install when the override is set; psql passes it verbatim to the connection stack rather than through the url-crate path that rejects the empty-host socket form. - alertd and tamanu doctor synthesise a DB-only sweep context from the override when no Tamanu install is present: DB and host checks run, while install-dependent checks (install metadata, local HTTP, caddy, services, kopia) skip. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
d9633d0 to
0f817d0
Compare
This was referenced Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 Adds a
TAMANU_DATABASE_URLenvironment variable that, when set, is used instead of the database section of the Tamanu config. Honoured by every bestool command that connects to or emits the Tamanu DB connection:alertd,tamanu doctor,tamanu logs,tamanu lifecycle(start/stop/status/restart),tamanu psql,tamanu db_url,tamanu backup,tamanu greenmask.Behaviour
database_url()returns the override verbatim — preserving query params (sslmode) and Unix-socket forms. It flows straight into the connection stack (bestool_postgres::pool→tokio_postgres::Config, which handles sockets and disables SSL for them;db_connectusestokio_postgres::connectdirectly).database()parses the override into host/user/password/name for the consumers that need fields rather than a URL (backup'spg_dump,greenmask,db_url/psql). Parsing goes through tokio-postgres's own connection-string parser, so any form the connection path accepts parses identically — includingpostgresql://tamanu_monitoring@/tamanu?host=/var/run/postgresql(Unix socket, peer auth).db_urlandpsqlno longer require a Tamanu install when the override is set.db_urlechoes it verbatim (or re-points it at a-Urole);psqlpasses it verbatim to the connection stack rather than through theurl-crate path, which rejects the empty-host socket form.alertdandtamanu doctorsynthesise a sweep context from the override when there's no Tamanu install on the host, so DB and host checks run against it. This is what lets alertd monitor a database on a host with no Tamanu config.Check levels
The doctor sweep now distinguishes two check levels, and an install/DB distinction that affects data, not gating:
tamanu_http(local API),tamanu_service(service inventory),version_drift, and the DB checks live here, so they run in the DB-only case. Two refinements make this work:tamanu_service's one config-derived expectation (the FHIR worker) degrades toUnknownwithout config rather than being guessed —services::expectednow takesOption<&TamanuConfig>, mirroring the existing tri-state for the patient-portal signal.version_driftneeds a baseline version to compare running container tags against. That's the install's env-file version when present, otherwise the version Tamanu records in its own DB (local_system_facts.currentVersion) — the sweep populates the context version (and the wiretamanuVersion) from the DB for an install-less host. It self-skips only if neither source resolves a version.Removed the
tamanu_foundcheck. It was never a healthcheck (it always passed; a "fail" had no meaning), it just reported the install's version/root/kind. Those move into the top-level status payload instead:tamanuVersionwas already there, andtamanuRoot(when an install is on disk) andtamanuServerKind(central/facility) are now top-levelServerInfofields. With it gone, no check is install-gated anymore — the install-vs-DB distinction only chooses the version source and whethertamanuRootis reported.Tidied other report-only checks. Same reasoning, for checks whose value is already a top-level status fact:
server_idcheck —metaServerIdis already resolved at the top level (it's the/status/{id}POST target), and the check was pure reporting.db_versionanduptimeoff-wire (rendered in the CLI, omitted from the canopyhealth[]array) —pgVersionanduptimeSecsare already top-level facts, so they shouldn't alert. A new off-wire variant of the tamanu-level check arm carriesdb_version.Notes
TAMANU_DATABASE_URL=… bestool alertd runreads the env var and monitors that database, install or not. The released binary builds with default features, which includetamanu→alertd-tamanu, so it always has this path. The only exception is a deliberately-minimal compile-time configuration built with--no-default-features --features alertd(i.e. withoutalertd-tamanu): that binary has no Tamanu code at all by construction and so doesn't read the env var. This is about that feature set, not about how the daemon is run.backupandgreenmaskstill require a local install for their non-DB needs (output dirs,pg_dump/psqldiscovery); the override only changes which database they target.New tests: URL parsing (TCP, Unix socket, percent-encoded userinfo, scheme/dbname validation) in the tamanu crate; install-vs-DB check gating in the alertd doctor.