Skip to content

OpenConceptLab/ocl_issues#3092 OpenConceptLab/ocl_issues#3093 | Supplements and reserved code spaces: where OCL stores them - #1103

Open
paynejd wants to merge 5 commits into
fhir-452-b-apartfrom
fhir-452-c-storage
Open

paynejd wants to merge 5 commits into
fhir-452-b-apartfrom
fhir-452-c-storage

Conversation

@paynejd

@paynejd paynejd commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Branch fhir-452-c-storage, base fhir-452-b-apart (#1090). The storage of code system supplements (OpenConceptLab/ocl_issues#3092) and reserved code spaces (OpenConceptLab/ocl_issues#3093), for OpenConceptLab/ocl_online#452.

Merge it right after #1090, so every migration of the FHIR work reaches staging early. #1091 merges this branch, so the migration graph stays linear.

Size

Three commits, 9 files, +293/−1: 170 lines of code and migrations, 123 of tests. 7336bac3 adds the storage; a53b7ff3 and a744936b bound the migrations' lock waits (from review).

What it adds

Four additive migrations, and the model fields and index they create. Nothing reads or writes them yet. They're used by OpenConceptLab/ocl_issues#3092 and OpenConceptLab/ocl_issues#3093 later in the series (#1104 in OCL's model, #1105 in the FHIR layer), under FHIR's rules only. The API never reads or writes them: as every model field, they show in the create serializers' schema (fields = '__all__') and in CSV exports (values()), as empty lists.

Migration Adds What it's for
sources/0053_source_supplements_base the partial index source_supplements_base on split_part(supplements, '|', 1), over supplement sources only finding a code system's supplements by its canonical URL
sources/0054_source_reserved_code_spaces Source.reserved_code_spaces, as [{pattern, display}] the code spaces a code system reserves for its users to define codes in (FHIR's codesystem-reservedCodeSpace)
collections/0079_supplements Collection.declared_supplements, Expansion.supplements_used the supplements a value set declares (FHIR's valueset-supplement), and what an expansion built under FHIR's rules applied
collections/0080_expansion_reserved_codes Expansion.reserved_codes the codes in reserved code spaces that an expansion built under FHIR's rules holds and no concept defines

New source and collection versions copy their HEAD's reserved code spaces and declared supplements.

Each migration sets lock_timeout (0.3 s) before its first DDL, applying and reversing: behind a long transaction on a table it alters (an expansion build on collection_expansions, say), it fails within a second and changes nothing, instead of holding its locks and every query queued behind them. Run it again once that transaction is over. The waits of one migration together stay below PostgreSQL's deadlock_timeout (1 s), so if it and another transaction wait for each other, it's the migration that gives up.

Production

Read-only counts, Dec 1: 3,962 sources (0 supplements), about 25,800 collections and 26,000 expansions. Each new column is nullable with a constant default, which PostgreSQL records in the catalog without rewriting the table. The index holds supplement rows only (none today), and CREATE INDEX reads the small sources table once.

Reading order

  1. The four migrations.
  2. core/sources/models.py (supplements_base, the index, reserved_code_spaces), then core/collections/models.py.
  3. Tests: core/sources/tests/tests_supplement_storage.py (the migrations reverse and apply again; the index is used), then tests_supplements.py and tests_reserved_codes.py (copies and defaults; both grow in the core PR).

Tests

  • Full gate on a744936b, as pr.yml runs it: pylint 10.00; 4,376 tests OK (2 skipped) and 804 search-index tests OK; coverage 97%.
  • makemigrations --check: no changes for sources and collections.

Review

Three Codex defect passes and a simplification review.

  • Pass 1 found 1: a migration that waits on a long transaction's lock while holding another table's. Fixed: each migration bounds its lock waits (a53b7ff3).
  • Pass 2 found 3. Fixed: the timeout is set before the first DDL in both directions (a744936b). Accepted: the create serializers (fields = '__all__') and the CSV exports (values()) show every model column, these as they do others; nothing reads them.
  • Pass 3: nothing new. The simplification review cut nothing here.

Rollback

manage.py migrate sources 0052 and manage.py migrate collections 0078 (after rolling back anything that merged on top), then revert this PR. Nothing reads the columns or the index.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qWapJWFsFc1Xjopf9k

paynejd and others added 3 commits December 1, 2026 08:05
…ements and reserved code spaces: where OCL stores them

Four additive migrations, and the model fields and index they create. Nothing reads or writes them yet: they are
used by OpenConceptLab/ocl_issues#3092 and OpenConceptLab/ocl_issues#3093, which come later in the series, under FHIR's rules only.

- sources/0053: source_supplements_base, a partial index on the code system a supplement names (its supplements URL
  without the |version), over supplement sources only, so a code system's supplements are found by its canonical URL
- sources/0054: Source.reserved_code_spaces, the code spaces a code system reserves for its users to define codes in
  ([{pattern, display}], FHIR's codesystem-reservedCodeSpace), copied to new versions
- collections/0079: Collection.declared_supplements, the supplements a collection version declares (FHIR's
  valueset-supplement), copied to new versions; Expansion.supplements_used, what an expansion built under FHIR's
  rules applied
- collections/0080: Expansion.reserved_codes, the codes in reserved code spaces an expansion built under FHIR's rules
  holds that no concept defines

Each column is nullable with a constant default, recorded in the catalog without rewriting the table. None is in the
API's serializers. Tests: the migrations reverse and apply again, the index is used, and the copies and defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWapJWFsFc1Xjopf9k
…torage migrations bound their lock waits

Each of the four migrations first sets lock_timeout (5s) in its own transaction, both ways. Behind a long transaction
on a table it alters (a long expansion build on collection_expansions, say), a migration now fails in seconds and
changes nothing, where it would have held its locks, and every query queued behind them (collections reads among
them), until that transaction ended. It can be run again once that transaction is over.

Test: each migration bounds its lock waits first, applying and reversing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWapJWFsFc1Xjopf9k
…torage migrations bound their lock waits before their first DDL, both ways

A migration reversed runs its operations last first, so a timeout set by the first operation's reverse came after the
DDL it should bound. Each migration now sets lock_timeout in a forward-only first operation and a reverse-only last
one, so both directions set it before any DDL.

Test: the first operation sets it applying, the last one reversing, and no other operation is SQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWapJWFsFc1Xjopf9k
…o fhir-452-c-storage

Takes #1090's new head, which brings in master
(#1089, #1109 and
#1106): REFERENCE_SEARCH_OPTIONS_TOGGLE is on in every
environment, as on master. Merged cleanly; no change to this branch's own
code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 2, 2026
…nto fhir-452-b-imports

Takes #1103's new head, which brings in master
(#1089, #1109 and
#1106): REFERENCE_SEARCH_OPTIONS_TOGGLE is on in every
environment, as on master. The one conflict is the constants import in
core/collections/models.py, where both sides add names: all are kept. No
change to this branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: ready, but not mergeable yet. It's stacked on #1090, which needs one migration fix. Once #1090 merges and this PR is retargeted to master with CI green, it can merge as is.

Merge order: #1087, then #1090, then this PR right after #1090 (as the description asks), then #1091 and the rest.

I reviewed only this PR's own diff against fhir-452-b-apart.

  • Migrations:
    • lock_timeout is set before the first DDL, applying and reversing.
    • The new columns are nullable with constant defaults, which PostgreSQL 11 and later add in the catalog only.
    • The partial index on sources (about 4,000 rows, with no supplements today) blocks writes there only briefly.
    • Each migration reverses, and the tests apply and reverse all four.
  • API writes: fields = '__all__' puts the new fields in the create serializers, but prepare_object copies only the attributes it lists, so the API can't write them. The schema and CSV exposure was already accepted in the Codex pass.
  • Migration numbers: collections 0079/0080 and sources 0053/0054 assume nothing else adds a migration to those apps before the stack lands. Run makemigrations --check after the last master merge before merging.

Nothing in this PR crosses into commercial logic.

… PostgreSQL's deadlock check

Each lock wait is bounded at 0.3 s instead of 5 s, applied and reversed. 0079
locks collections and then collection_expansions: with 5 s, PostgreSQL could
cancel a transaction that had read the second and then read the first. Its two
waits now end before that check, so it's the migration that gives up. Reads of
a table that 0054, 0079 or 0080 waits for are held up for well under a second,
not for 5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
@paynejd

paynejd commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

One commit since the review, 87ba778: the four migrations now wait 0.3 s for each lock, not 5 s.

While I fixed the migration of #1090, a 5 s bound turned out to have a fault of its own, and this PR's migrations had it too. collections.0079 locks collections and then collection_expansions. Tried on PostgreSQL 14.7:

  • With 5 s: a transaction that had read collection_expansions and then read collections was cancelled by PostgreSQL's deadlock check, which runs after 1 s.
  • With 0.3 s: that transaction goes on, and the migration gives up and changes nothing.
  • Reads held up behind a waiting migration (sources.0054, collections.0079 and 0080): 5 s before. Now well under a second: 0.3 s measured, and 0.6 s at most for 0079, which waits for two tables.

The schema is the same, and nothing else in the PR changes. The description says 0.3 s now.

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict unchanged: ready, waiting only on #1090.

87ba778 lowers the lock wait of all four migrations from 5 s to 0.3 s, applied and reversed, and the test follows. Nothing else changes. That matches 0078 on #1090 and 0052 on #1087. 0079 waits for two tables, so it can hold reads up for 0.6 s at most, still under deadlock_timeout. CI is green at 87ba778.

Merge order: right after #1090. After you retarget to master, merge master in, then run makemigrations --check and CI before merging.

paynejd added a commit that referenced this pull request Dec 4, 2026
…nto fhir-452-b-imports

Takes #1103's new head. Merged cleanly; no change to this
branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 4, 2026
…nto fhir-452-b-imports

Takes #1103's new head. Merged cleanly; no change to this
branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 4, 2026
…nto fhir-452-b-imports

Takes #1103's new head. Merged cleanly; no change to this
branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants