project-nomad/admin/database/migrations
caweis 77c36b3fef
Add offline FDA drug reference (labels, interaction view, conditions, remedies) (#1040)
* feat(drug-reference): offline FDA drug labels, conditions, and remedies

Adds an offline medical-reference feature with three coupled layers:

- Drug Reference: full-text search over openFDA drug-label indications,
  a detail page per label, and a side-by-side single-drug comparison view.
  A two-phase background pipeline downloads the openFDA label parts to the
  storage volume (resumable) and ingests them into the search table.
- Conditions ("When to use what"): a curated spine of first-aid situations
  that maps each situation to matching OTC drugs, each linking back to its
  Drug Reference detail page.
- Curated remedies: hand-authored natural and home-remedy entries drawn from
  US-government public-domain sources (NCCIH, CDC, MedlinePlus, FDA), shown
  with their source links and the same safety disclaimers as the rest of the
  feature.

The drug-reference and conditions layers are intentionally coupled: the drug
detail page shows the situations a drug treats, and the conditions controller
reads the same drug_labels table.

Safety surfaces ship as written. The amber SafetyBanner ("informational only,
not medical advice, not an FDA endorsement, not a drug-interaction checker, in
an emergency call emergency services") renders on the condition pages and the
search page; the detail and comparison pages carry their own "not a cross-drug
interaction checker" callout; and every page carries the openFDA CC0 source
citation and no-FDA-affiliation footer.

Wiring on this branch:
- start/routes.ts: the /drug-reference and /conditions page GETs plus their
  /api/* groups.
- commands/queue/work.ts: the drug-download and drug-ingest queues, both at
  concurrency 1. The two drug queues get a per-queue stall override
  (lockDuration 1_800_000, maxStalledCount 3) because each part is one long
  stream; every other queue keeps the existing 300000 default.
- inertia/pages/home.tsx: Drug Reference and "When to use what" tiles. The
  icon and display_order are a starting point, open to change.
- types/kv_store.ts: the two drugReference.* keys the pipeline reads and writes.
- package.json: yauzl and stream-json (plus their @types), used by the ingest
  job to stream the label JSON out of the downloaded zips.

The app reads the conditions and remedy data from the compiled TS constants in
app/data/; the repo-root collections/*.json files are the browseable mirrors.
The natural-remedies standalone test reads collections/natural_remedies.json to
assert the two stay in sync, so that file is also a test fixture.

The four standalone tests pass (drug_interactions, drug_ingest_status,
conditions, natural_remedies). tsc reports no errors in the feature code.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* chore(drug-reference): drop a fork-internal comment reference

* fix(drug-reference): address review — defer FULLTEXT, fallback remedies prop, strip fork refs

- migration: wrap the FULLTEXT ALTER in this.defer so it runs after the
  deferred createTable (was silently swallowed, index never created)
- controller: add remedies:[] to the index() error fallback (required prop)
- strip fork-internal issue/spec references from ported comments
- tsconfig: exclude tests/standalone (node --experimental-strip-types only)
- correct the varchar(768) byte-math comment; extend remedy-spine test

* fix(drug-reference): make the interaction comparison readable at five drugs

The comparison view laid its columns out on an equal-fraction CSS grid
(repeat(N, minmax(0, 1fr))), so each added drug shrank every column; at the
five-drug maximum the FDA interaction text was squeezed into unreadable slivers.

Lay the columns out with flex instead: full-width and stacked on phones, then
fixed-width columns that scroll sideways from the sm: breakpoint up, so they
never shrink below a readable width. Theme the columns with the same palette as
the rest of the page (they were on stock gray), and give the headers a fixed
min-height so columns line up when drug names wrap.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(collections): route a 'dataset' tier resource to the drug pipeline

Add an optional `type` discriminator to SpecResource ('zim' | 'dataset',
absent == 'zim'), so the tier installer can carry a DB-ingested resource
alongside ZIM files. ZimService.downloadCategoryTier branches on it: a
'dataset' resource dispatches the existing FDA download+ingest pipeline
instead of RunDownloadJob, guarded against duplicate dispatch by the drug
ingest status. Every existing manifest entry has no `type` and keeps the
exact ZIM path.

Widen InstalledResource.resource_type to include 'dataset' and exclude
dataset rows from the ZIM/map catalog-update scan (datasets aren't
filename-versioned; their freshness path is separate). No dataset rows are
written yet: the InstalledResource 'dataset' row on ingest-ready, the
manifest entry, install-gating, and the downloads-aggregator integration
are follow-up commits.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(drug-reference): rework into an opt-in medicine-standard tier dataset

Reshapes the offline FDA drug reference from a built-in feature into curated
content installed by selecting the Medicine / Standard tier, per maintainer
direction.

- install-state: the ingest writes an installed_resources 'dataset' row on
  ready (version = the openFDA export_date), threaded installer to download to
  ingest; the tier-status math and the home-tile gate read it. Manual ingests
  write no row, so install-state stays tied to the curated path.
- manifest: declare the dataset in the medicine-standard tier (runtime fetches
  the remote manifest, so this also needs to land upstream).
- install-gating: the drug-reference home tiles render only when installed.
- uninstall: DrugReferenceService.uninstall() stops the two drug queues, deletes
  the on-disk parts, truncates drug_labels (schema kept), clears the KV markers,
  and drops the install row. Best-effort, logged, scoped to drug data only.
- downloads: the download phase reports the canonical {percent, downloadedBytes,
  totalBytes} shape as one drug-data card in the Active Downloads aggregator with
  cancel/remove; the heavy ingest stays in the IngestStatus surface with an
  Indexing handoff on the card.
- auto-update: a daily DrugAutoUpdateJob compares the manifest export_date and
  re-downloads when newer, gated on installed + no active job.

typecheck clean; the drug standalone suites pass. Three points are flagged in
code for the maintainer: the InstalledResource 'dataset' approach, the tier
home, and the export_date string format.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(downloads): keep the drug download card live across all parts

The Active Downloads card filtered to the deterministic jobId, but the
download's continuations run under auto-generated jobIds (only part 0 uses the
deterministic one). So the card tracked part 0 and then vanished while parts
2..N kept downloading. The queue is concurrency 1, so collapse to whichever
single part is in flight and report the deterministic jobId: one card tracks
aggregate progress through the whole download and cancel/remove still routes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(drug-reference): co-locate a persistent safety note with affirmative remedy guidance

Add RemedySafetyNote at the head of every natural-remedy section — the two on
Drug Reference and the one on "When to use what" — so the "informational only,
not medical advice, seek real medical care in an emergency" framing appears with
the guidance itself, not only in the page-top banner. Replaces the terse
per-section caveat with the same amber alert language as SafetyBanner.

Addresses the upstream #1040 review request that the disclaimer be unmistakable
and present wherever affirmative self-care guidance appears, not a one-time banner.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(drug-reference): fold dataset freshness into the content-auto-update path

The drug dataset auto-updated on its own daily cron that ignored the
contentAutoUpdate.* master switch, so it would refresh even with content
auto-update turned off, and it didn't ride the content-update path the way
ZIMs and maps do.

Move the export_date freshness/apply orchestration onto
DrugReferenceService.attemptAutoUpdate(), add
ContentAutoUpdateService.attemptDrugDataset() gated on the same enabled +
window config, and have the hourly ContentAutoUpdateJob drive both. Retire
the standalone DrugAutoUpdateJob. The ZIM/map attempt() path is unchanged.

Addresses the upstream #1040 request to wire the openFDA export_date check
into the content updater so the dataset updates alongside ZIMs and maps.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(drug-reference): strengthen the remedy note to consult a clinician before combining with meds

Widen the affirmative-remedy safety note from "talk to a clinician before use"
to explicitly cover using a remedy AND combining one with a medication the user
already takes — the interaction case is the higher-risk path for an off-grid
user self-treating.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(drug-reference): gate affirmative remedy content behind an off-by-default flag

Add drugReference.remediesEnabled (default off), independent of the tier
install. When off, the server emits no remedy data at any boundary — the
drug-reference page prop, conditions show, and the /api/conditions/drugs
situation search — and the "Natural" filter is hidden, so installing the
medicine-standard tier lights up the verbatim FDA label search and the
condition-to-OTC matching but not the hand-authored self-care and herbal
sections. No user-facing toggle: it is flipped on after a clinician content-pass.

Implements the upstream #1040 split-by-risk request: the regulated label content
ships with the tier; the authored remedy guidance stays gated until sign-off.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Jake Turner <52841588+jakeaturner@users.noreply.github.com>
2026-07-21 11:06:18 -07:00
..
1751086751801_create_services_table.ts feat: openwebui+ollama and zim management 2025-07-09 09:08:21 -07:00
1763499145832_update_services_table.ts feat(Services): friendly names and descriptions 2025-11-18 14:02:22 -08:00
1764912210741_create_curated_collections_table.ts feat: curated zim collections 2025-12-05 15:47:22 -08:00
1764912270123_create_curated_collection_resources_table.ts feat: curated zim collections 2025-12-05 15:47:22 -08:00
1768170944482_update_services_add_installation_statuses_table.ts fix(admin): improve duplicate install request handling 2026-01-13 06:58:05 -08:00
1768453747522_update_services_add_icon.ts feat: improved app cards and custom icons 2026-01-15 15:54:59 -08:00
1769097600001_create_benchmark_results_table.ts feat: Add system benchmark feature with NOMAD Score 2026-01-22 21:48:12 -08:00
1769097600002_create_benchmark_settings_table.ts feat: Add system benchmark feature with NOMAD Score 2026-01-22 21:48:12 -08:00
1769300000001_add_powered_by_and_display_order_to_services.ts feat: Use friendly app names on Dashboard with open source attribution 2026-01-22 16:43:32 -08:00
1769300000002_update_services_friendly_names.ts fix: service name defs and ollama ui location 2026-02-01 05:46:23 +00:00
1769324448000_add_builder_tag_to_benchmark_results.ts fix(migrations): Fix timestamp ordering for builder_tag migration 2026-01-28 13:43:21 -08:00
1769400000001_create_installed_tiers_table.ts feat: Improve curated collections UX with persistent tier selection 2026-01-24 15:33:50 -08:00
1769400000002_create_kv_store_table.ts feat(RAG): [wip] RAG capabilities 2026-01-31 20:39:49 -08:00
1769500000001_create_wikipedia_selection_table.ts feat: Add dedicated Wikipedia Selector with smart package management 2026-01-31 21:00:51 -08:00
1769646771604_create_create_chat_sessions_table.ts feat: [wip] native AI chat interface 2026-01-31 20:39:49 -08:00
1769646798266_create_create_chat_messages_table.ts feat: [wip] native AI chat interface 2026-01-31 20:39:49 -08:00
1769700000001_create_zim_file_metadata_table.ts feat(ContentManager): Display friendly names instead of filenames 2026-02-03 23:14:28 -08:00
1770269324176_add_unique_constraint_to_curated_collection_resources_table.ts fix(Curated Collections): ensure resources are not duplicated on fetch-latest 2026-02-04 21:35:18 -08:00
1770273423670_drop_installed_tiers_table.ts fix: rework content tier system to dynamically determine install status 2026-02-04 22:58:21 -08:00
1770849108030_create_create_collection_manifests_table.ts feat: curated content system overhaul 2026-02-11 15:44:46 -08:00
1770849119787_create_create_installed_resources_table.ts feat: curated content system overhaul 2026-02-11 15:44:46 -08:00
1770850092871_create_drop_legacy_curated_tables_table.ts feat: curated content system overhaul 2026-02-11 15:44:46 -08:00
1771000000001_add_update_fields_to_services.ts feat: support for updating services 2026-03-11 14:08:09 -07:00
1771000000002_pin_latest_service_images.ts feat: support for updating services 2026-03-11 14:08:09 -07:00
1771100000001_migrate_kiwix_to_library_mode.ts feat(Kiwix): migrate to Kiwix library mode for improved stability (#622) 2026-04-03 14:26:50 -07:00
1771200000001_create_map_markers_table.ts feat(maps): add scale bar and location markers (#636) 2026-04-03 14:26:50 -07:00
1772000000001_add_supply_depot_fields_to_services.ts feat: supply depot 2026-06-23 04:46:45 +00:00
1772000000002_add_user_modified_to_services.ts feat: edit curated apps + fix dropdown clip + stale _old rollback 2026-06-23 04:46:46 +00:00
1772000000003_add_app_auto_update_fields_to_services.ts feat(supply-depot): opt-in automatic updates for installed apps 2026-06-23 04:46:52 +00:00
1775100000001_create_custom_library_sources_table.ts feat(Content): custom ZIM library sources with pre-seeded mirrors (#593) 2026-05-20 10:16:00 -07:00
1776000000001_create_kb_ingest_state_table.ts feat(KB): per-file ingest state machine (Phase 1 of RFC #883) (#888) 2026-05-20 10:16:00 -07:00
1776100000001_create_kb_ratio_registry_table.ts fix(KB): align chunks_per_mb column type with TS contract 2026-05-20 10:16:00 -07:00
1776200000001_add_content_auto_update_fields_to_installed_resources.ts feat(content): opt-in automatic updates for installed ZIM & map content 2026-06-23 04:47:01 +00:00
1776200000001_add_custom_url_to_services.ts feat(supply-depot): add custom launch URLs for apps 2026-06-23 04:46:56 +00:00
1776300000001_deprecate_legacy_kolibri.ts feat: replace legacy Kolibri image default with latest v19 image (#1019) 2026-06-23 04:47:07 +00:00
1776400000001_add_benchmark_harness_metadata.ts feat(benchmark): harness hardening — fail loudly + pin sysbench + record provenance (#1089) 2026-07-20 13:48:09 -07:00
1776400000001_add_collection_to_kb_ingest_state.ts feat(rag): add subject/collection organization to knowledge base (#1063) 2026-07-20 15:09:11 -07:00
1776400000001_sunset_meshtastic_daemon.ts chore(catalog): sunset orphaned Meshtastic Daemon card (#1049) 2026-07-18 09:06:57 -07:00
1776400000002_add_benchmark_score_v2.ts feat(benchmark): Score v2 app client — raws, uncapped score, v2 payload + UI 2026-07-21 09:36:14 -07:00
1778600000004_create_drug_labels_table.ts Add offline FDA drug reference (labels, interaction view, conditions, remedies) (#1040) 2026-07-21 11:06:18 -07:00
1778600000006_add_indications_fulltext_index.ts Add offline FDA drug reference (labels, interaction view, conditions, remedies) (#1040) 2026-07-21 11:06:18 -07:00
1778700000006_add_ingested_at_default_drug_labels.ts Add offline FDA drug reference (labels, interaction view, conditions, remedies) (#1040) 2026-07-21 11:06:18 -07:00