Skip to content

Issue #1063: test: round-trip the shipped seed through the portable format - #1497

Merged
bjagg merged 2 commits into
mainfrom
issue-1063-portable-round-trip
Nov 10, 2026
Merged

bjagg merged 2 commits into
mainfrom
issue-1063-portable-round-trip

Conversation

@groybal

@groybal groybal commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

From Claude:

Description of Change

#1063 now covers the portable format's done-test (bjagg's re-scope). Export the shipped models, mapping groups and crosswalks, import them into an empty install with different row Ids, export again, and compare byte for byte (docs/specs/mdr-portable-file-format.md §3, guarantee 5). Per the plan's note on this issue, the test fails rather than skips when CI has no database.

1. Seed round trip: test/bases/lif/mdr_restapi/test_portable_seed_round_trip.py

  • A: every live model in backup.sql that the converter exports, then every mapping group and crosswalk whose two models are both among them.
  • B: A imported into an empty install and exported again. C: B imported into a second empty install and exported again. The installs are tenant schemas from clone_lif_schema(..., false), and the second one's sequences start at 1000.
  • Assertions: A == B == C byte for byte, file by file. The model Ids are disjoint across the three: {4, 10, 13}, then {1, 2, 3}, then {1001, 1002, 1003}.
  • Floor: models {4, 10, 13} must still export, so losing one fails the test.
  • Limitation: only those three models round-trip today. Every exportable group and crosswalk needs model 1 (LIF), which is still NOT_MIGRATED. Content is discovered rather than listed, so groups and crosswalks join the round trip on their own once model 1 exports (MDR portability: convert models 17 and 18 from overlays to self-contained models, in place #1388/MDR portability: measure and clean data that blocks export, in every schema #1393).

2. A converter bug the test found: components/lif/mdr_services/model_converter.py

Model 10 (CTDL) didn't round-trip. 45 entities that are only children of list references came back with "Array": true. The cause:

  • Import set an entity's stored Array from any association that places it in a list, references included.
  • Those entities have no containing parent, so export writes their own Array.

The generator emits a reference as a single object whatever its child's Array (spec §11, #1471). Setting it from a reference therefore did nothing for the reference, and it made the entity's top-level schema a list. Import now skips references there; containing associations and root entities behave as before.

A new test, test_a_list_reference_leaves_its_child_entity_list_setting_alone, failed before the fix. It belongs in this PR because the round trip can't pass without it.

3. Fail, don't skip, in CI

The testing.postgresql fixtures now call pytest.fail when CI is set and PostgreSQL can't start. They still skip locally. That covers conftest.py and the two copies in test/scripts/. pr-ci.yml has no Postgres service; the runner image just happens to ship it. test_postgres_unavailable.py covers both branches.

How to review / test

PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH" uv run pytest \
  test/bases/lif/mdr_restapi/test_portable_seed_round_trip.py \
  test/bases/lif/mdr_restapi/test_model_converter.py \
  test/bases/lif/mdr_restapi/test_postgres_unavailable.py -rs

Without the PATH entry on macOS, the database tests skip locally.

Related Issues

Closes #1063
Refs #1223

Type of Change
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
Project Area(s) Affected
  • components/
  • test/ or e2e/

Checklist
  • commit message follows commit guidelines (see commitlint.config.mjs)
  • tests are included (unit and/or integration tests)
  • code passes linting checks (uv run ruff check)
  • code passes formatting checks (uv run ruff format)
  • code passes type checking (uv run ty check)
  • pre-commit hooks have been run successfully
Testing
  • Automated tests added/updated

  • test/bases/lif/mdr_restapi/ + test/scripts, with Postgres running: 481 passed. The one skip is the existing LIF_UPDATE_SCHEMA_SNAPSHOTS switch.

  • pre-commit on the changed files, including the full pytest test hook: passed.

  • Checks that the test fails when it should: it caught the CTDL difference before the fix, and adding model 1 to the floor makes it fail with seed models that no longer export: {1}.

  • Not checked end to end: the CI gate with Postgres absent. testing.postgresql finds the Homebrew install even off PATH, so the unit tests, which make the server start fail, are the evidence.

Additional Notes
  • Shared brick: mdr_services is packaged only by lif_mdr_api. .github/workflows/lif_mdr_api.yml watches components/lif/mdr_services/**.
  • Behavior change on import: an entity that is only the child of list references is now stored with Array = "No", where it used to be "Yes". Models already imported keep their rows.
  • Out of scope: the original OpenAPI generate→upload matrix, the has/relevant relationship cases and skipped_constraints in the upload response. The re-scope drops them, because the converter doesn't use that path.

🤖 Generated with Claude Code

…ormat

Exports every seed model, mapping group and crosswalk the converter
accepts, imports it into two empty installs with different row Ids, and
asserts A == B == C byte for byte (Portability Spec §3, guarantee 5).
Today models 4, 10 and 13 round-trip; groups and crosswalks join once
model 1 exports.

Fixes the model 10 (CTDL) failure the test found: import marked an entity
Array=Yes from a list reference, so an entity with no containing parent
came back with its own Array. References no longer set it (#1471).

The MDR database fixtures now fail instead of skipping when CI is set and
PostgreSQL can't start.

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

@bjagg bjagg 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.

Thanks, Gary. I checked it at dc97c73:

  • It proves the guarantee for what it covers. It compares dump_canonical output, the same bytes the export endpoint returns, byte for byte, A against B and A against C. Model Ids really differ: {4, 10, 13} in the seed, {1, 2, 3} in the first install and {1001, 1002, 1003} in the second. It runs in about 6 s.
  • It catches import losses. Making import drop element Description or association Notes fails it. Reverting your model_converter.py fix fails both it and the new unit test.
  • Fail-in-CI, skip-locally works for test/bases/lif/mdr_restapi/. With no Postgres and CI unset: 84 passed, 296 skipped. With CI=true: 295 errors instead of skips.

Notes, none blocking:

  1. Coverage is narrower than "the seed" suggests. Today only models 4 (CEDS) and 10 (CTDL) carry content; 13 is 267 bytes with no elements. No mapping group or crosswalk round-trips, because each one has a model that's refused. _try (:80-85) drops any refused file, and the only floor, EXPORTING_MODELS, covers models. Once groups start exporting after #1388, a converter regression that makes them refuse would leave this passing with 0 groups. A floor on group and crosswalk counts, raised as they come in, would close that.
  2. A round trip can't see a field export drops. Dropping entity Description from export still passes, because file A never had it. The same goes for fields the seed leaves empty in models 4 and 10, such as ActivationDate, Tags and Example. test_model_converter.py's synthetic round trip covers those, so it's a limit of this test rather than a gap in the suite. It may be worth one line in the docstring.
  3. Conflict with #1498. #1498 deletes the whole "any association" merge in _import_elements that this PR narrows, so the two will conflict, and with #1498 in, your fix is moot. Whichever lands second should take #1498's version and rerun this test. It's the round trip that would catch a mistake there.
  4. Other suites still skip in CI: control_plane_restapi/test_api_key_endpoints.py, developer_keys/test_workspace_store.py, test_schema_drift_service.py and four test/scripts/ files. The description scopes the change correctly; this is a possible follow-up.
  5. Minor: on a Mac where Homebrew links initdb into /opt/homebrew/bin, the tests run without the PATH entry, so "they skip" isn't true on every Mac.

Approving.

Conflicts: model_converter.py takes main's _import_elements (#1498 removes the
"any association" merge this branch narrowed); test_model_converter.py keeps
both this branch's list-reference test and #1498's cardinality tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bjagg
bjagg merged commit 8f93bed into main Nov 10, 2026
4 checks passed
@bjagg
bjagg deleted the issue-1063-portable-round-trip branch November 10, 2026 18:00
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Current Community Efforts Nov 10, 2026
dereck-symmetry pushed a commit that referenced this pull request Nov 10, 2026
…1497) into the frontend branch

Resolves the cspell.json conflict by keeping both word lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dereck-symmetry pushed a commit that referenced this pull request Nov 10, 2026
…ter branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bjagg pushed a commit that referenced this pull request Nov 11, 2026
…tained models (#1489)

##### Description of Change
Adds the one-time conversion of the overlay models 17 (StateU LIF) and
18 (Org2 LIF) into self-contained models, in place, as decided on #1388
(ADR `data_model/0004`). Merging changes nothing at runtime: the new
module is imported only by the script, and running the script is a
manual step on each environment.

**`components/lif/mdr_services/overlay_converter.py`**, per overlay:
- **Step 0:** attributes stored with an overlay's model but associated
directly from a base entity move to the base model (cbeach47's §11
line). Ids are unchanged. In the seed: 8 of model 17's attributes move
to model 1. It refuses two candidates that would give the base one
`UniqueName` twice.
- **Steps 1–4:** the overlay's schema decides what it shows. Every
foreign element it shows is copied into it, with the inclusion row's
flags, along with any value set a shown attribute uses from another
model.
- **Step 5:** associations are rewritten or copied between the model's
own rows, and every `ExtendedBy` row the overlay declares is
soft-deleted. That includes the 69 inside model 16 and the ownerless
1846.
- **Step 6:** own elements the schema doesn't show are soft-deleted.
- **Step 7:** `BaseDataModelId` is cleared, `CopiedFrom*` is set to the
base model, and the inclusion rows are deleted.
- **Step 8:** every live mapping row on the model's side is re-pointed
through the Id map.

**It checks itself before anything commits.** Every model it touches (1,
16, 17 and 18) must generate the same schema in all 16 flag
combinations, once Ids are mapped, and 17, 18 and their mapping groups
must export. `required` is compared as a set, since own elements keep
their Ids and the copies get new ones (option 2 on #1388). Any failure
raises `ConversionFailed`, and the caller rolls back.

**It refuses what the comparison can't see** (`outside_references`).
After every step, a live row of a model it doesn't compare that still
uses an entity or attribute it soft-deleted or changed in place fails
the run: an association at either end, a `TargetEntityId`, or an
inclusion row. So does a soft-deleted association of such a model, and
any live mapping row, on any side, that still goes through a
soft-deleted element. A row belongs to its `ExtendedByDataModelId`, or
with none to its parent entity's model.

**`scripts/convert_overlays.py`** runs it in `public` and then every
`tenant_*` schema, each in its own transaction:
- It refuses to start while `remove_test_data.py`'s rows are still live
(#1479, #1487).
- It skips a schema that's already converted, so a re-run converts only
what's left.
- It locks the 11 tables the conversion reads or writes, in `EXCLUSIVE`
mode with a 10s `lock_timeout`. Reads go on and writes wait, so an MDR
edit can't commit mid-run and be lost.
- It writes each schema's undo record to `--undo-dir` (on a dry run
too): the Id map, every row inserted or soft-deleted, and the old value
of every column updated in place.
- It rolls back unless `--apply`. A schema that fails its check, can't
get its locks, or hits any other error is rolled back and reported, and
the other schemas still run.
- The undo record says `committed: false` until the commit succeeds, so
a failed commit leaves a record that says so.

**Docs:** four *Migration defaults* lines in spec §11 (step 0, a value
set used from another model, the `ExtendedBy` rows inside other models,
and 1846), a `scripts/README.md` row, and a CHANGELOG entry.

**Not in this PR: `reference_data/transformations/`.** I measured it
rather than regenerating it (details on #1388). The paths are names, and
105 rules in the three top-level files go through model 1 elements that
17's schema doesn't show, so they can't resolve after the conversion.
Regenerating the numeric Ids from a seed run would give Ids that are
wrong on dev. bjagg checked dev read-only, in all 10 schemas: none of
its live mapping rows fail step 8, so the 105 rules are only in the
reference files. **A follow-up PR re-exports the files from dev after
dev is converted, and lists the rules that drop out** (agreed on #1388).

##### Before `--apply` on dev
The MDR API image must include this PR first. Its workflow's `paths:`
already cover `components/lif/mdr_services/**`.
1. Run `remove_test_data.py --apply`, including #1487's rows.
2. Take an RDS snapshot. A committed run has no automatic undo: the undo
record lists every change, but reversing it is manual.
3. Run the dry run and read its output for every schema. bjagg's
read-only check on 11-09 found no live row that step 8 would refuse, and
no row outside 17/18 that uses what 17 soft-deletes.
4. Announce an MDR edit freeze. Writes wait on the locks, and a request
can time out while they do.
5. Run `--apply`, then copy the undo directory out of the container
before it stops.

##### Related Issues
Refs #1388

##### Type of Change
- [x] New feature (non-breaking change which adds functionality)

##### Project Area(s) Affected
- [x] components/
- [x] scripts/
- [x] test/ or e2e/
- [x] Documentation (docs/, READMEs, ARCHITECTURE.md, CLAUDE.md)

`mdr_services` is packaged only by `lif_mdr_api`.

##### Testing
35 tests, all against a real Postgres. The DB-backed ones skip without
`initdb`, so locally I ran them in a container with Postgres.
- **On the seed after #1444's and #1479's cleanups:**
- the whole seed converts and passes every check, leaving no inclusion
rows on 17 or 18. Its figures are pinned: each model's copy counts, 17's
soft-deleted entities and attributes by Id, every soft-delete count, and
the 5 re-pointed mapping rows (group 25's 4 on step 0's attributes,
group 26's 1);
  - step 0 moves exactly the 8 attributes;
- step 0 refuses an attribute whose name the base already has, or that
has another association;
  - it refuses an overlay with inclusion rows but no base (#1445);
- it refuses a mapping row whose path goes through a row the schema
hides;
- it refuses when another model still uses a row it soft-deletes
(bjagg's probe: a model-2 association to attribute 1820), and when a
mapping row on another side goes through one;
  - step 0 refuses two candidates with one `UniqueName`;
- the self-check fails a conversion whose copies lose a column
(`Description` on attributes, `Relationship` on entity associations).
- **Pure parts (16):**
  - the render set;
- the schema rewrite, which changes only Ids, `DataModelId`,
`ExtendedBy` and `Extension`;
  - mapping paths;
- `required` order ignored, while a changed `required` and a changed
`enum` order are still caught.
- **Runner (8):**
  - it refuses before the cleanup;
  - it skips a converted schema;
- a dry run converts, writes its undo record with `committed: false`,
and changes no row in the 9 tables it writes;
  - `--apply` commits and a re-run skips;
  - it doesn't start while another transaction holds a write;
  - any other error rolls that schema back and is reported;
  - a failed commit leaves the undo record at `committed: false`;
- a `tenant_*` schema converts its own rows and leaves `public`
unchanged.

Mutation checks: no `required` sorting, sorting every list, no commit on
`--apply`, the undo record always marked `committed`, and no table lock.
Each one is caught by exactly the test it should be.

Full suite, Postgres-backed tests included: 1864 passed, 1 skipped (the
snapshot regenerator, which runs only with
`LIF_UPDATE_SCHEMA_SNAPSHOTS=1`). That run was on `e9e4e58`. After the
lock fix, the merge of `main` (#1487) and the pins, I re-ran the
converter's, the runner's and `remove_test_data`'s tests on `92adef4`:
38 passed, 0 skipped. After bjagg's review and a merge of `main` (#1464,
#1496), on `af9f658`: the converter's and the runner's 35 pass, 0
skipped. After a further merge of `main` (#1488), the full suite with
Postgres on `3abc49c`: 1893 passed, 1 skipped (the snapshot
regenerator). After a merge of `main` (#1501, #1498, #1497) on
`99e7079`: the converter's and the runner's 35 pass against Postgres, 0
skipped (#1498 changed the generator the self-check uses), and the full
suite with Postgres: 1909 passed, 1 skipped (the snapshot regenerator).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: dereck <dereck.haskins@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

MDR schema round-trip: true data-portability test matrix (follow-up to #1007)

2 participants