Skip to content

OpenConceptLab/ocl_online#798 | A FHIR update saves onto HEAD from HEAD, and its new version is made from that HEAD - #1101

Draft
paynejd wants to merge 9 commits into
masterfrom
fhir-452-b9-codesystem
Draft

paynejd wants to merge 9 commits into
masterfrom
fhir-452-b9-codesystem

Conversation

@paynejd

@paynejd paynejd commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Added Dec 4: reads and a test that depended on where rows sit in their table. Two commits, 91fa9125 and fdb270b6, to review on their own (git diff 2c4026a5 fdb270b6, about 25 lines of code and 60 of tests). They're here because this PR already changes core/code_systems/serializers.py.

  • What was wrong: a query with no order gives rows as they sit in the table, and a row moves when it's saved again.
    • FHIR CodeSystem reads, $lookup and stored $expand listed a concept's names in that order, and took its definition from the first description in it. So the same request could list the names in another order later. test_a_supplements_display_is_its_name_in_the_code_systems_language failed now and then because of it.
    • test_mapped_codes_read_mappings_for_a_few_concepts_a_query compared two such reads in order, and failed now and then too.
  • Now:
    • Names and descriptions are sorted by id (core.concepts.models.in_order), from the rows a list has already loaded, so there are no more queries. The definition is the first description written. If a concept has two preferred names in the code system's language, the first one written is a supplement's display.
    • The mapped codes of a search document have no order, so only that test changes: it compares them in any order.
  • Tests: each saves a row again between two reads and compares them. Without its fix, each fails every time.
  • Full gate on afedc981 (with master merged in), as pr.yml runs it: pylint 10.00; 4,296 tests OK (2 skipped) and 815 search-index tests OK; coverage 97%.

Part of phase B (B-9, accepted Dec 1). API users' own writes don't change.

Ticket: OpenConceptLab/ocl_online#798 (B-9: a resource written through FHIR keeps what it says).

The defect

A FHIR CodeSystem, ValueSet or ConceptMap update (PUT) loaded the version its URL resolves, usually the latest release, applied the resource to it, and saved it over HEAD. Any HEAD field the resource doesn't carry took that release's value. So an edit made to HEAD through the OCL API since that release (description, website, extras, a validation schema, HEAD's access and so on) was silently undone by a metadata-only PUT.

What changes

One helper on FhirRepositorySerializerMixin (core/fhir/serializers.py), used by all three serializers:

  • update_head: HEAD is the stored HEAD with what the resource says. It's read FOR NO KEY UPDATE in the transaction that saves it, so an edit committed meanwhile is the one kept.
    • HEAD keeps its own access, which a FHIR resource doesn't carry (OpenConceptLab/ocl_online#698): the save keeps the access the locked row has, so a concurrent make-private or publish isn't undone (keep_private and keep_access).
    • Its validation schema is HEAD's. A FHIR resource can't change it, so no revalidation of HEAD's concepts is queued (persist_changes(revalidates_schema=False)).
    • The resource's id never renames HEAD.
  • as_new_version: the new version is made from HEAD as stored once the update has written it. It takes the resource's version label and status, and it's the latest version. Like any new version, it starts with no background tasks, member counts or record of removed members.
  • A CodeSystem update that gives no language writes a supplement's concepts in HEAD's language, the one HEAD keeps.

Kept as they were: the version-exists check; concepts (CodeSystem), references (ValueSet) and mappings (ConceptMap); and the supplement fields.

Tests

  • core/fhir/tests_updates.py, for each of CodeSystem, ValueSet and ConceptMap:
    • an API edit to HEAD survives an update that doesn't mention it;
    • what the update says wins;
    • the new version is made from HEAD, and is HEAD as stored once written, even after a concurrent edit;
    • the new version is the latest, with none of HEAD's tasks or member counts;
    • HEAD's validation schema is kept, and no revalidation is queued;
    • a supplement's concepts are written in HEAD's language.
  • core/capabilities/tests/test_make_private.py:
    • a viewable HEAD keeps its access whatever the release's;
    • a publish since the update read HEAD is kept;
    • the resource is applied to HEAD as read for update.
  • Three existing tests described the old behaviour (a private release's access copied onto HEAD; a release without supplements refusing the PUT). They now check the new one.
  • Each fix's test fails without its fix.
  • Full gate on 2c4026a, as pr.yml runs it: pylint 10.00; 4,186 tests OK (2 skipped) and 804 search-index tests OK; coverage 97%.

Review

  • Three Codex defect passes found 2, 3 and 4 findings.
    • Fixed: the validation schema passed as None; a concurrent publish undone; a needless schema revalidation; an edit committed before the lock overwritten; a version made from a mix of two HEAD states; HEAD's member counts inherited.
    • Accepted, with reasons on the tracking ticket:
      • an untitled ConceptMap's empty full_name is filled from its name, as every edit does;
      • a locale reindex is queued just before its transaction commits;
      • a supplement's language is read before HEAD is locked.
  • A simplification review proposed 4 cuts: 2 applied, 2 declined (they're the tests of two fixes).
  • A final Codex check of the later commits found 2: a concurrent change to HEAD's retired flag could reach the new version (the version now takes retired from the update's status, as released), and a test assertion that compared two counts together. Both fixed in 2c4026a.

Not in this PR

  • A ValueSet update still never removes a reference its new compose leaves out. That's older than this change.
  • The ValueSet compose itself (includes as written, compose details kept) is a separate draft PR that merges this branch.

Rollback

Revert this PR. No migrations, no settings.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH

paynejd and others added 6 commits December 1, 2026 04:36
…AD, and its new version is made from that HEAD

A FHIR CodeSystem, ValueSet or ConceptMap update (PUT) loaded the version its URL resolved, usually the latest
release, applied the resource to it and saved it over HEAD. Any HEAD field the resource doesn't carry took that
release's value, so an edit made to HEAD through the API since that release was undone by a metadata-only PUT, and so
was HEAD's own access.

Now one helper on FhirRepositorySerializerMixin does it for all three:
- update_head: HEAD is the stored HEAD with what the resource says. A private HEAD still stays private whatever a
  concurrent write does (persist_changes keep_private), and the resource's id never renames HEAD.
- as_new_version: the new version is made from that HEAD, with the resource's version and status, as the latest
  version, without HEAD's background tasks or its record of removed members.

A CodeSystem update that doesn't give a language writes a supplement's concepts in HEAD's language, which is the one
it keeps. Kept as they were: the version-exists check, concepts, references and mappings, and the supplement fields.

Tests: core/fhir/tests_updates.py, for each resource type. Three existing tests described the old behaviour (a private
release's access copied onto HEAD; a release without supplements refusing the PUT) and now check the new one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH
… schema and the access HEAD has when it's saved

- HEAD is saved with its stored validation schema as the original schema, as an API edit that sends none is. With
  None, a HEAD whose schema differs from its latest release's, and which has concepts, was revalidated to None and
  the update failed.
- The save keeps the access HEAD has once it's locked for the save (keep_access beside keep_private), so a publish
  made between the update's read of HEAD and its save isn't undone, as a make-private already wasn't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH
… HEAD as read for update, and queues no schema revalidation

- update_head reads HEAD FOR NO KEY UPDATE in the transaction that saves it, and applies the resource to that row, so
  an API edit committed before the update locks HEAD is the one kept, not the values read earlier.
- persist_changes takes revalidates_schema=False from a write that can't change the validation schema. A FHIR update
  can't, so it no longer queues a revalidation of HEAD's concepts, whose full-row save could undo a concurrent change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH
…s stored once written, without HEAD's member counts

- as_new_version reloads HEAD before making the version from it, so an edit made to HEAD after the update saved it
  (while concepts, references or mappings were written) gives one consistent version, not the update's values mixed
  with HEAD's.
- The new version starts with no member counts, as any new version does, so a version that isn't expanded doesn't
  show HEAD's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH
…of revalidates_schema; the ValueSet update uses as_new_version's result

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH
… retired status from the update, as its released

The version is made from HEAD reloaded, so a concurrent change to HEAD's retired flag no longer reaches it: like
released, retired is the update's status, else the resolved version's. The member-count test checks each count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXBZMHpxmJnf6SMKTH
paynejd and others added 3 commits December 4, 2026 02:24
…rder written

CodeSystem reads, $lookup and $expand listed names, and chose the first
description as the definition, in the order the database gave them, which
changes when a row is saved again. They're sorted by id now, so the same
request always lists them in the same order.

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

As with the names FHIR lists, this depended on where rows sit in their table:
the mappings are read with no order, and the test compared two reads of them in
order, so it failed now and then. A search document's codes have no order, so
only the test changes. It now saves one mapping again between the two reads,
which made it fail every time.

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

1 participant