Skip to content

Check portability rules in one place: schema generation and MDR editing should call mdr_portability #1468

Open

Description

@cbeach47

From Claude:

Why

The portability rules live in mdr_portability, but only export and import call them. Schema generation and the editing endpoints each check things their own way, or not at all. So a model can be saved in a state that export refuses and schema generation breaks on, and nobody learns about it until they try to download it.

#1462 is a concrete case. A cycle of embedded associations crashed OpenAPI schema generation. #1463 is the stopgap: find_children and find_ancestors in schema_generation_service.py now track the path they walk and return a 400 on a cycle. The same rule already existed in model_rules._check_for_containment_cycles (§5.4), which export runs through check_model_file. The rule now exists twice: once keyed by name over a ModelFile, once keyed by ID over DB rows and raised as HTTPException.

Grounding in code

  • Export and import call the rules. model_converter, mapping_group_converter and crosswalk_converter run check_model_file, check_mapping_group_file and check_crosswalk_file, so export never writes a file that import would refuse.
  • Schema generation does not call them. Beyond the Issue #1462: fix: refuse an embedded-association cycle instead of crashing schema generation #1463 cycle guard, schema_generation_service.py has its own refusals ("multiple ancestor lines", "entity is referenced by an association but is not in the data model's entity set"). Some of these overlap with portability rules and some may not. Nobody has compared them.
  • Editing does not call them. create_entity_association checks only for a duplicate parent/child pair, so the UI can save an embedded cycle. create_transformation / update_transformation check that IDs exist, not the mapping-group rules.
  • UI shows refusals only after export or import fails (RefusalList, PortableFailureDialog). It never shows them while someone edits.
  • Existing data already has blockers. scripts/measure_export_blockers.py (Issue #1393: feat: read-only count of what keeps each model and mapping group from exporting #1442) counts what export refuses today in each schema.

Direction (per discussion with Benito on #1463)

One set of portability rules, checked in one place:

  1. Schema generation checks the model against the portability rules before it walks the model, and refuses with the same {code, element, reason} shape that export uses. The ad-hoc guards in schema_generation_service.py are then removed, including Issue #1462: fix: refuse an embedded-association cycle instead of crashing schema generation #1463's guard once this replaces it.
  2. The model, mapping-group and crosswalk editing endpoints run the same check on the result of the edit, so the UI can warn or block before the model goes non-portable.
  3. The UI shows those refusals inline (reusing RefusalList) and does not keep its own copy of the rules.

Open questions

  • Block or warn on edit? Existing models already fail rules (Issue #1393: feat: read-only count of what keeps each model and mapping group from exporting #1442's counts), so blocking every edit to one of them would lock users out. One option: block only edits that add a new violation, and warn about the ones already there.
  • Checking a whole model on every edit means building its ModelFile each time. Is that fast enough, or do some rules need an incremental check?
  • Which of schema generation's own refusals are portability rules, and which (for example, Extension inclusions) belong only to generation?

Done means

  • Schema generation and every editing endpoint refuse through mdr_portability.
  • No portability rule is implemented a second time outside that component.
  • A test shows an embedded cycle refused at edit time with the same code that export uses.

Refs: #1462, #1463, #1442, #1333, #1393

Activity

  1. self-assigned this
    on Nov 4, 2026
  2. added a commit that references this issue on Nov 12, 2026
  3. bjagg commented on Nov 12, 2026

    @bjagg
    Contributor

    Another case for this issue: the MDR's create path accepts two mappings with the same target in one group. The rule that a mapping is identified by its group and target path (§6.2) is checked only in mdr_portability/mapping_group_rules.py. So POST /transformation_groups/ creates the group, and export later refuses it with MAPPING_TARGET_SHARED.

  4. cbeach47 commented on Nov 13, 2026

    @cbeach47
    ContributorAuthor

    From Claude:

    Refinement: the plan, split into PRs

    This issue is done as a series of PRs. Each one leaves main consistent, says Refs #1468 (not Closes), and ticks its row below. Whoever picks up next starts at the first unticked row.

    Decided

    • Nothing is saved that breaks a portability rule. Each edit is checked on its result, in the same transaction, before commit: run the check, apply the edit with flush, run it again, and refuse with {code, element, reason} (422) if the edit adds any refusal. A model that already breaks rules can still be repaired, because edits that only remove refusals pass. Once no model breaks a rule, this is the same as "the model after every edit is portable".
    • One change, one request. The UI creates an entity and then its parent association in two calls (handleCreateEntity, ModelExplorer.tsx:381-390). Between them the entity sits at the top level, so a nested Identifier would break TOP_LEVEL_NAME_TAKEN. Entity create takes its parent in the same request, as attribute create already does (MDR: create-attribute is non-atomic — a dropped response orphans the attribute (persisted but unassociated) #1028).
    • The check is export_model / export_mapping_group / export_crosswalk, the converters' own refusals, so nothing restates the rules. On the seed, export_model takes 0.3–0.4 s on the largest models (4 and 10), against 3.4–10.9 s for schema generation. Checking an edit exports the model twice, before and after, so about 0.8 s on the largest seed model. That's fast enough to check the whole model, without a narrower check of only the part that changed.

    The PRs

    # Scope After Tests
    [ ] 1 Generation checks the rules first. generate_openapi_schema runs the model check before walking it and refuses with any code except NOT_MIGRATED (which includes overlays) and MODEL_IDENTITY_INCOMPLETE (seed model 26, and models POST /datamodels/ saves with no organization). One app-level handler turns PortabilityErrorGroup into the 422 that /portable returns. The schema download's toast lists the refusals (describePortableFailure + RefusalList) — A cycle added to a model that exports is refused with ASSOCIATION_CYCLE; test_schema_snapshots.py is unchanged; #1463's tests still pass
    [ ] 2 Edit guard + entity associations (create, update, delete), with a per-model lock so two concurrent edits can't each pass the check and together break a rule. Remove check_name_and_array's blank-Name refusal (#1508), which the guard now makes as FIELD_INVALID 1 An embedded cycle refused at edit time with ASSOCIATION_CYCLE; two children under one parent with the same Name refused with ASSOCIATION_NAME_TAKEN
    [ ] 3 Guard: entity–attribute associations (create, update, delete). Deleting an attribute's last association is refused 2 Duplicate Name refused; deleting the last association refused with UNASSOCIATED_ATTRIBUTE
    [ ] 4 Entity create in one request. create_entity takes an optional parent; handleCreateEntity makes one call — Entity and association written together or not at all; Vitest for the dialog
    [ ] 5 Guard: entities and attributes (create, update, delete) 2, 4 Array on a contained entity refused; DataType entity refused as NOT_MIGRATED
    [ ] 6 Guard: value sets, values, model details, constraints, inclusions. Adding a constraint or an inclusion to a model that exports makes it NOT_MIGRATED or an overlay, so it's refused 2 Duplicate ValueName; blank ContributorOrganization; a constraint on a portable model
    [ ] 7 Guard: transformations and transformation groups, through export_mapping_group. Check whether MappingsView.tsx's own duplicate source→target check (:1149) can rely on the refusal instead 2 MAPPING_TARGET_SHARED; PATH_INVALID
    [ ] 8 Guard: value mappings, through export_crosswalk 2 CROSSWALK_SOURCE_DUPLICATE
    [ ] 9 Model edits re-check what depends on the model. Mapping paths resolve against the model's live rows (mapping_group_converter._export_path), so deleting a mapped attribute breaks the group with PATH_INVALID. Value-set edits can break crosswalks the same way 5, 7, 8 Deleting an attribute a live mapping targets is refused, naming the group
    [ ] 10 FE: edit refusals shown inline in the edit dialogs, with an edit variant of RefusalList 2 Vitest per dialog
    [ ] 11 Cleanup. Generation refuses NOT_MIGRATED and MODEL_IDENTITY_INCOMPLETE too; delete #1463's guards (find_children, find_ancestors) and _entity_name's refusal #1513, which converts the seed, dev and demo (#1388 and #1393 are closed) test_schema_generation_cycle.py expects ASSOCIATION_CYCLE

    "Tests" means automated tests in the PR (pytest for the backend, Vitest for the frontend). PRs that change the UI also list manual steps under "How to test".

    Why the cycle guard is deleted last, not first. export_model refuses NOT_MIGRATED before it runs check_model_file (model_converter.py:96-114), so for a model that isn't migrated the portability cycle rule never runs. Deleting #1463's guard now would bring back #1462's crash for any such model with an embedded cycle. On the raw seed, 17 of 21 models are refused as overlays or NOT_MIGRATED before the rules run; dev's public now exports every model (#1393), and demo isn't converted yet. #1513 converts both. PR 1 makes the portability rule refuse cycles on every model it can check, and PR 11 deletes the guard once that's all of them.

    Reusing _find_containment_cycles on database rows to delete the guard sooner isn't worth it. The generator would still build its own graph from rows, so "which associations contain" would still be written twice. And #1486 deletes that code anyway.

    Related, outside this issue

    Other paths that write model content without the rules:

    Each PR also updates the mdr_services README and the spec's list of the converter's callers (§8) where it adds one.

  5. added a commit that references this issue on Nov 18, 2026
  6. added 3 commits that reference this issue on Nov 18, 2026
  7. cbeach47 commented on Nov 19, 2026

    @cbeach47
    ContributorAuthor

    From Claude:

    From #1521: clone_datamodel sets no ContributorOrganization (CreateCloneDTO has none), so a clone's portable identity is incomplete. Once this issue removes MODEL_IDENTITY_INCOMPLETE from CODES_NOT_YET_REFUSED, schema generation refuses every clone. test_clone_datamodel_sql works around it by copying the source's organization onto the clone.

    Before the pass goes, this needs a decision on which organization a clone gets (the source's, the caller's tenant, or a required field on CreateCloneDTO), and then the change in clone_datamodel.

  8. added a commit that references this issue on Nov 20, 2026
  9. cbeach47 commented on Nov 20, 2026

    @cbeach47
    ContributorAuthor

    I need to pause this effort for now.

    Handoff notes for whomever picks it up next:

    1468-handoff.md

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions