Skip to content

fix(imports): preserve link_hub_source_mapping for self-referencing links on JSON round trip - #176

Open
tkirschke wants to merge 3 commits into
mainfrom
fix/link-hub-source-mapping-roundtrip
Open

fix(imports): preserve link_hub_source_mapping for self-referencing links on JSON round trip#176
tkirschke wants to merge 3 commits into
mainfrom
fix/link-hub-source-mapping-roundtrip

Conversation

@tkirschke

Copy link
Copy Markdown
Member

Context

Follow-up to #174. That PR made link_source_mapping survive JSON import (dependent-child-key / payload columns were being dropped). This PR closes the remaining gap on the other link mapping table, link_hub_source_mapping.

Note: the change in this PR was briefly pushed directly to main by mistake (bypassing review) and has been reverted on main (commit reverting c3fcfa9). This PR re-introduces it properly for review.

Problem

For a link that references the same hub more than once (e.g. customer_duplicate_customer_l, which points at CUSTOMER_H twice with aliases HK_CUSTOMER_H and HK_CUSTOMER_DUPLICATE_H), the JSON export could not say which source column feeds which reference. LinkColumnMapping carried only (link_column_name, link_column_type, source_column_name) — both business-key mappings serialized with the identical link_column_name, so the importer collapsed them onto the first reference with the wrong source column.

Verified against the bundled TPCH model: link_hub_source_mapping round-tripped 14 → 13 with one wrong row.

Fix

Add an optional target_foreign_hashkey to LinkColumnMapping — the hub hashkey alias (else hub hashkey name), which uniquely identifies a hub reference:

  • models.py — new optional field (backward compatible; defaults to None).
  • builder.py — populate it for business-key mappings from hub_ref.hub_hashkey_alias_in_link or hub.hub_hashkey_name.
  • json_parser.py — resolve the hub reference by target_foreign_hashkey; fall back to the previous column-name match when the field is absent, so legacy exports still import.

Verification

  • Full TPCH round trip (DB → JSON export → JSON import): both link_hub_source_mapping (14 → 14) and link_source_mapping (14 → 14) now come back with zero diff, including the same-hub-twice link, composite business keys, and a renamed foreign hashkey.
  • New tests: whole-model round-trip equality (asserts both mapping tables as sets) + a legacy-export fallback test (strips target_foreign_hashkey).
  • Full suite: 100 passed, 11 skipped. ruff clean.

🤖 Generated with Claude Code

…ound trip

link_source_mapping already survived after the previous commit, but
link_hub_source_mapping was still lossy for links that reference the same hub
more than once (e.g. customer_duplicate_customer_l): LinkColumnMapping had no
per-reference discriminator, so both business-key mappings serialized with the
same link_column_name and the importer collapsed them onto the first reference
with the wrong source column.

Add an optional target_foreign_hashkey to LinkColumnMapping (the hub hashkey
alias, else hub hashkey name — uniquely identifies a hub reference), populate
it in the export builder for business-key mappings, and have the JSON parser
resolve the hub reference by it. Falls back to the previous name-matching when
the field is absent, so legacy exports still import.

Verified on the full TPCH model: both link_hub_source_mapping (14) and
link_source_mapping (14) now round-trip DB -> export -> import with zero diff.
Adds a whole-model round-trip regression test and a legacy-export fallback test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@tkirschke
tkirschke requested a review from obause July 1, 2026 14:24
…inks on JSON round trip

Adds target_foreign_hashkey to LinkColumnMapping so the JSON export/import can
disambiguate hub references when a link points at the same hub more than once,
keeping link_hub_source_mapping (and link_source_mapping) intact across a
DB -> export -> import round trip.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@tkirschke tkirschke changed the title Preserve link_hub_source_mapping for same-hub-twice links on JSON round trip fix(imports): preserve link_hub_source_mapping for self-referencing links on JSON round trip Jul 1, 2026
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