Skip to content

feat(import): adopt YAML entity UUID as primary key + duplicate handling (#853) - #863

Merged
mvkonchits-db merged 5 commits into
developmentfrom
feat/import-id-adopt
Oct 9, 2026
Merged

mvkonchits-db merged 5 commits into
developmentfrom
feat/import-id-adopt

Conversation

@larsgeorge-db

Copy link
Copy Markdown
Collaborator

Closes #853.

Stacked on #851 (PR #862) → #855 (PR #860). Base is feat/import-domain-recon. Merge #860 then #862 first; this PR's own diff is the id-adoption layer.

Adopt a valid, non-colliding UUID from the imported ODCS/ODPS file as the entity's primary key so YAML-expressed cross-entity links (product↔contract) survive import, with a lossless id round-trip on export.

Adoption (both entities)

  • create_product / create_from_upload take adopt_ids (default on): a valid UUID not already present in that entity's table is adopted as the PK; a non-UUID or a colliding/adopt-off id falls back to a generated UUID (today's behavior).
  • The incoming id is always recorded as the ontosOriginalId provenance custom property; the legacy sourceId is still written for non-UUID ids during the transition (read either). Export re-derives ontosEntityId (current PK).

Duplicate-id handling (batch)

Routes / frontend

  • Upload endpoints take adopt_ids + on_duplicate form fields; the paste /odcs/import takes them as query params. Both import dialogs gain "Adopt IDs from file" (default on) and "Import duplicate IDs as new copies" (default off) toggles; success toasts report skipped counts.

Bonus bug fix — product ODPS export was crashing

build_odps_export accessed the ODPS API model by validation-alias names (product.api_version, input_ports, output_ports, support_channels, custom_properties, authoritative_definitions, port.contract_id) that aren't attributes on the camelCase model — so GET /data-products/{id}/odps/export raised AttributeError for every product (no test covered it). Corrected to the real field names; the endpoint now works and carries the ontos* keys.

Tests & verification

  • New test_import_id_adopt: adopt valid UUID, non-UUID fallback + sourceId/ontosOriginalId, ontosEntityId on export, duplicate skip (default) and import-as-new — for both entities (10 tests). Made the domain-export-adapter test's domain fixture get-or-create so the suite is order-independent (managers commit → rows persist across tests).
  • Live (isolated 8140/3140): adoption keeps the file UUID; duplicate skip/new both work; product + contract exports emit ontosOriginalId/ontosEntityId; all three import toggles render with correct defaults.

This pull request and its description were written by Isaac.

@larsgeorge-db
larsgeorge-db requested a review from a team as a code owner September 23, 2026 19:33
@larsgeorge-db larsgeorge-db added the stakeholder/fpiz Reported by external stakeholder (codename: fpiz) label Sep 23, 2026
@larsgeorge-db
larsgeorge-db marked this pull request as draft September 24, 2026 12:57
larsgeorge-db added a commit that referenced this pull request Sep 27, 2026
…fpiz PRs

Rebuilt test/combined-mcp-odcs-odps on the current development tip and merged
every open PR labelled stakeholder/fpiz:
- import stack #860/#862/#863 (feat/import-id-adopt)
- #864 resolve-refs, #856 domain/tag dropdown scroll fix
- search #865 (improve-search) + #867 (improve-search-phase2)

Dropped a stray unused search_scoring import that the search-branch merge left
in data_products.py (query_index is used). Registry imports; 43 search+import
unit tests pass.

Co-authored-by: Isaac <no-reply@databricks.com>
@larsgeorge-db
larsgeorge-db marked this pull request as ready for review October 7, 2026 08:22

@mvkonchits-db mvkonchits-db 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.

Requesting changes. Import stays create-only, so an importer cannot overwrite or hijack an existing entity, which is good. Items marked (verified) I confirmed in the code.

Must fix:

  1. (verified) ID squatting. adopt_ids defaults to True and any user with write can claim an arbitrary UUID. A legitimate entity imported later with that id is then skipped, and anything resolving references by id (see #864) resolves to the squatter's entity. Please make adoption opt-in, or restrict it to a stronger permission.
  2. (verified) UUIDs are validated with uuid.UUID(...) but stored and compared as the raw string (adopted_id = original_id, exact-match repo.get). Uppercase, braced or hyphenless spellings pass validation, miss the duplicate lookup, and create a second identity for the same logical UUID. Canonicalize before lookup and storage.
  3. (verified) on_duplicate is a free Form("skip") string. Any value other than exactly "skip" creates a copy, so a typo silently duplicates. Validate against skip / new.
  4. (verified by absence) on_duplicate="new" assigns a fresh id but nothing remaps references that still contain the supplied id, so imported ports keep pointing at the pre-existing entity.

Should fix:
5. (verified) A skipped duplicate returns the existing row's id (entity_id=source_id, "already present (same id)") with no visibility check, which acts as an existence oracle.
6. Raw exception text in per-item messages (see the note on #860).

larsgeorge-db added a commit that referenced this pull request Oct 8, 2026
…dation (#853 review)

Addresses mvkonchits-db's must-fix items on #863:

1. ID squatting (#853.1): adopt_ids defaults to False on all three import
   endpoints. Even when explicitly on, adoption requires either the new
   per-role `can_adopt_entity_ids` privilege (prior commit) or feature
   ADMIN on the target entity. Non-privileged callers silently fall back
   to a generated UUID with the file id preserved as ontosOriginalId
   provenance — no hard 403 so unattended tooling degrades gracefully.
2. Canonicalize UUIDs before dup lookup and storage (#853.2): a new
   _canonical_uuid() parses via uuid.UUID (accepting upper/braced/urn/
   hyphenless spellings) and returns the canonical lowercase hyphenated
   form. All three dup-check sites + both adoption sites use it, so a
   spelling variant can no longer bypass the duplicate check or create a
   second identity for the same logical UUID.
3. Validate on_duplicate at the route (#853.3): must be exactly 'skip' or
   'new', otherwise 400. A typo no longer falls through to the "new" path
   and silently duplicates.
4. Skipped-dup existence oracle (#853.5): the batch result for a skipped
   duplicate now only surfaces the existing row's id/name when the caller
   is a feature-admin (new reveal_dup_detail flag computed at the route).
   Non-admin callers get a generic "Supplied id cannot be adopted" skip
   without entity_id/name, so a sibling import can't be used to probe for
   invisible entities.
5. Intra-batch reference remap for on_duplicate=new (#853.4): documented
   as vacuous given current architecture (ODCS carries no intra-file
   cross-refs; ODPS's only cross-ref inputPort.contractId points at
   contracts, which are imported through a separate endpoint). Scaffold
   hook noted for a future cross-entity batch flow.

Tests: canonicalization (UPPERCASE + braced spelling), redaction for
non-admin on both products and contracts, retained reveal-dup-detail
behavior for admin callers.

Co-authored-by: Isaac <no-reply@databricks.com>

@mvkonchits-db mvkonchits-db 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.

Re-reviewed the changes since my last review (a086bc68f, ae844340f, 3d7ebce61, plus the original fab391b21). All of my items are addressed in the code:

  • ID squatting: adopt_ids now defaults to false on all three import endpoints. Adoption also needs the new can_adopt_entity_ids role privilege or ADMIN on the feature, and non-privileged callers fall back to a generated id with ontosOriginalId kept. The audit entry records both the requested and the effective value.
  • UUIDs are canonicalized before the duplicate lookup and before storage.
  • on_duplicate is validated at the route (400 unless skip or new).
  • A skipped duplicate no longer returns the existing row's id or name unless the caller is a feature admin.
  • Reference remap: I accept your reasoning that nothing needs remapping inside one batch. What remains is the cross-endpoint case (a product imported later whose port points at the old contract id after on_duplicate=new). That is a limitation, not a defect, and your comment documents it.
  • The new migration b1_add_adopt_entity_ids is a single head, descends from n1_search_documents, and the revision id is 23 characters. I checked the head chain against current development.

Notes, none blocking:

  • user_can_adopt_entity_ids resolves roles by group, so a user granted a role only through assigned_users will not get the privilege. That fails closed, but you may want to include them.
  • The unguarded add_column is fine, since fresh databases use create_all plus stamp.
  • 3d7ebce61 is a WIP commit; it is harmless under a squash merge.

I am not approving yet, for two reasons. This PR's diff against its current base is 117 files, mostly development content merged into the branch, and I have not inspected those merge resolutions. An unnoticed merge resolution is exactly how file_inputs disappeared on #862. And the backend tests and the Alembic check have not run on this branch, because stacked PRs only run the title check.

Plan: once #862 is rebased and merged, rebase this branch onto development (for example git rebase --onto origin/development <old #862 tip> feat/import-id-adopt), so the diff is just this change and the full CI runs. I will re-review and approve then.

@larsgeorge-db

Copy link
Copy Markdown
Collaborator Author

Thanks for the detailed re-review. Agreed on both the plan and the two non-blocking notes.

On the retarget:
Will do exactly as you proposed — once #862 merges into development, I'll run git rebase --onto origin/development <old #862 tip> feat/import-id-adopt and retarget this PR to development, so the diff is just this feature (adopt-ids / can_adopt_entity_ids / UUID canonicalization / on_duplicate validation / existence-oracle redaction) and the backend tests + Alembic check actually run on CI. Holding off from touching anything on this branch in the meantime so you don't review stale deltas.

On can_adopt_entity_ids + assigned_users:
Fair point that a user granted a role only through assigned_users currently misses the privilege (same cliff is_user_admin has). I'll fold assigned_users into the lookup in a follow-up — not blocking this PR, but worth doing for parity with how roles are actually assigned in practice.

On 3d7ebce61 (WIP commit):
Confirmed harmless under squash; its diff (the DB column add) is a strict subset of the net-effect feature commit, so a squash merge produces the correct final tree.

On the cross-endpoint remap case:
Noted — a product imported later whose port points at the pre-on_duplicate=new contract id will see a dangling reference. Documented in the manager scaffold-note; could add a one-shot repair later, but that's an "import ordering" artifact rather than a defect in this PR.

mvkonchits-db and others added 5 commits October 9, 2026 16:44
Carried over from merge commit 7789b29 on feat/import-id-adopt, where this
change was made while resolving conflicts and not as its own commit.

The cross-type guard from #860 classifies entities by `kind`, then by
structural markers. A sparse ODPS product that declares `productType` but no
ports has neither, so it was classified as unrecognizable and skipped. The
#853 tests build products exactly that way (name, version, productType), so
without this marker they are skipped instead of created.

`productType` is an ODPS-only top-level field, so it cannot misclassify an
ODCS contract.

Co-authored-by: Lars George <lars.george@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
…ing (#853)

Adopt a valid, non-colliding UUID from the imported ODCS/ODPS file as the entity's
primary key so YAML-expressed cross-entity links (product↔contract) survive import,
with a lossless id round-trip on export. Stacked on #851 (feat/import-domain-recon).

Adoption (both entities)
- create_product / create_from_upload take adopt_ids (default on): a valid UUID that
  does not already exist in that entity's table is adopted as the PK; a non-UUID or a
  colliding/adopt-off id falls back to a generated UUID (today's behavior). Contracts
  adopt via id= on DataContractDb; products via product_data['id'].
- The incoming id is always recorded as the ontosOriginalId provenance custom property;
  the legacy sourceId is still written for non-UUID ids during the transition (read
  either). Export re-derives ontosEntityId (current PK) as a custom property.

Duplicate-id handling (batch, both entities)
- create_*_from_files take on_duplicate = "skip" (default) | "new". A payload id that is
  a valid UUID already present is either recorded as skipped ("already present (same
  id)") — making re-import idempotent — or imported as a new copy with a fresh UUID.
  Import stays create-only. Uses the skipped status already in BatchImportResult (#855).

Routes/frontend
- upload endpoints take adopt_ids + on_duplicate form fields; the paste /odcs/import
  takes them as query params. Both import dialogs gain "Adopt IDs from file" (default on)
  and "Import duplicate IDs as new copies" (default off) toggles; success toasts now
  report skipped counts.

Also fixes build_odps_export: it accessed the ODPS API model by validation-alias names
(product.api_version, input_ports, output_ports, support_channels, custom_properties,
authoritative_definitions, port.contract_id) that are not attributes on the camelCase
model, so the product ODPS export endpoint crashed with AttributeError for every product.
Corrected to the real field names; the endpoint now works (and carries the ontos* keys).

Tests
- New test_import_id_adopt: adopt valid UUID, non-UUID fallback + sourceId/ontosOriginalId,
  ontosEntityId on export, duplicate skip (default) and import-as-new — for both entities.
- Made the domain-export-adapter test's domain fixture get-or-create so the suite is
  order-independent (managers commit, so rows persist across tests in the shared session).

Verified live (isolated 8140/3140): adoption keeps the file UUID; duplicate skip/new both
work; product + contract exports emit ontosOriginalId/ontosEntityId; UI toggles render.

Session: claude -r 37a1420d-86af-4380-bef3-0b74c6f8b4b9
Adds a new per-role boolean privilege shown in the role editor Privileges tab
under a new "Import Privileges" section, as the explicit app-role gate for
adopting an entity's UUID from an imported file as the primary key. The gate
itself (#853.1) replaces the previous default-on-for-any-writer behavior.

- DB: AppRoleDb.can_adopt_entity_ids Boolean (default false, server_default
  'false'); migration b1_add_adopt_entity_ids descends from n1_search_documents.
- Pydantic: AppRoleBase/AppRoleUpdate/AppRole gain can_adopt_entity_ids;
  settings_manager._map_db_to_api surfaces it (repo write path serializes
  booleans unchanged via model_dump).
- AuthorizationManager.user_can_adopt_entity_ids(groups): mirrors
  is_user_ontos_admin, resolves roles by case-insensitive group intersection.
- Frontend: new "Import Privileges" section in the Privileges tab of
  role-form-dialog, mirroring Admin Role; TS AppRole gains the field; i18n keys
  added to all 8 locales (en + de/es/fr/it/ja/nl/pt).

The enforcement at the import routes (and the flip of adopt_ids default to
false) lands in the next commit.

Co-authored-by: Isaac <no-reply@databricks.com>
…dation (#853 review)

Addresses mvkonchits-db's must-fix items on #863:

1. ID squatting (#853.1): adopt_ids defaults to False on all three import
   endpoints. Even when explicitly on, adoption requires either the new
   per-role `can_adopt_entity_ids` privilege (prior commit) or feature
   ADMIN on the target entity. Non-privileged callers silently fall back
   to a generated UUID with the file id preserved as ontosOriginalId
   provenance — no hard 403 so unattended tooling degrades gracefully.
2. Canonicalize UUIDs before dup lookup and storage (#853.2): a new
   _canonical_uuid() parses via uuid.UUID (accepting upper/braced/urn/
   hyphenless spellings) and returns the canonical lowercase hyphenated
   form. All three dup-check sites + both adoption sites use it, so a
   spelling variant can no longer bypass the duplicate check or create a
   second identity for the same logical UUID.
3. Validate on_duplicate at the route (#853.3): must be exactly 'skip' or
   'new', otherwise 400. A typo no longer falls through to the "new" path
   and silently duplicates.
4. Skipped-dup existence oracle (#853.5): the batch result for a skipped
   duplicate now only surfaces the existing row's id/name when the caller
   is a feature-admin (new reveal_dup_detail flag computed at the route).
   Non-admin callers get a generic "Supplied id cannot be adopted" skip
   without entity_id/name, so a sibling import can't be used to probe for
   invisible entities.
5. Intra-batch reference remap for on_duplicate=new (#853.4): documented
   as vacuous given current architecture (ODCS carries no intra-file
   cross-refs; ODPS's only cross-ref inputPort.contractId points at
   contracts, which are imported through a separate endpoint). Scaffold
   hook noted for a future cross-entity batch flow.

Tests: canonicalization (UPPERCASE + braced spelling), redaction for
non-admin on both products and contracts, retained reveal-dup-detail
behavior for admin callers.

Co-authored-by: Isaac <no-reply@databricks.com>
@mvkonchits-db
mvkonchits-db changed the base branch from feat/import-domain-recon to development October 9, 2026 14:47
@mvkonchits-db
mvkonchits-db requested a review from a team October 9, 2026 14:47
@mvkonchits-db

Copy link
Copy Markdown
Contributor

I rebased feat/import-id-adopt onto development (force-push, 9a55cc44 to 620f4d6d) and retargeted this PR to development, so the full CI now runs against it. The diff is 25 files, which is just this change.

If you have this branch checked out locally, please reset to the remote instead of pushing your old history: git fetch origin && git checkout feat/import-id-adopt && git reset --hard origin/feat/import-id-adopt. The previous tip was 9a55cc44ad3fb269343c03b205e3368ef68e57e7, still reachable by SHA if you need anything from it.

What the branch contains now: your four commits (fab391b2, the WIP column commit, the role privilege, and the gate/canonicalization/validation fix), replayed in order with authorship and messages unchanged, plus one new commit at the front.

The new commit carries the productType marker for _PRODUCT_MARKERS in entity_kind.py. That change only existed inside your merge commit 7789b2913, not in any regular commit, so a plain replay of the commits would have dropped it. It is needed: the #853 tests build products as {name, version, productType} with no kind and no ports, and without the marker #860's cross-type guard skips them as unrecognizable. It is its own commit so it is visible in review. I credited you as co-author. Drop or reword it if you would rather handle it differently.

Conflicts (four hunks, all caused by #860 and #862 having been squash-merged):

  • In both managers the wrong-type guard now runs first and the duplicate-id handling follows it, which matches your resolution. I kept your comment noting the ordering.
  • In data-products.tsx I kept development's unimportedItems handling, which already covers skipped items.
  • In data_product_routes.py I kept the file_inputs line as it is on development.

How I checked it: the rebased tree differs from your previous tip only in files changed by #864 and #939 after your last merge. None of the #853 code (routes, authorization, role privilege, migration, locales, tests) differs. All changed Python files compile, pyflakes reports no new undefined names, and Alembic is a single head (b1_add_adopt_entity_ids on top of n1_search_documents). I did not run the backend tests locally; CI is running now.

I will re-review the diff once CI is green.

@mvkonchits-db mvkonchits-db 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.

Re-reviewed the rebased branch at 620f4d6d. Approving.

  • The diff against development is 25 files and is only this change.
  • CI ran the full suite on the development base: Backend Tests, Frontend Tests, TypeScript, Frontend Build, Alembic Single-Head and Requirements Lockfile all pass. The Coverage Report aggregate job was still running when I checked; it is not one of the required checks.
  • I compared every line the PR adds against the commits I reviewed earlier (fab391b2, 3d7ebce6, a086bc68, ae844340). The only added lines outside them are the carried-over productType marker commit and a four-line comment on guard ordering. Lines from the earlier commits that are missing were superseded by the later fix commit or dropped in the conflict resolution.
  • All five of my earlier items are in place: adopt_ids defaults to off and needs the can_adopt_entity_ids privilege or feature ADMIN, UUIDs are canonicalized before the duplicate lookup and before storage, on_duplicate is validated, a skipped duplicate only reveals the existing row to feature admins, and the reference-remap question is documented.
  • The migration b1_add_adopt_entity_ids is a single head on top of n1_search_documents, and the revision id is 23 characters.

Non-blocking, unchanged from my earlier note: user_can_adopt_entity_ids resolves roles by group only, so a user granted a role only through assigned_users will not get the privilege. That fails closed.

@mvkonchits-db
mvkonchits-db added this pull request to the merge queue Oct 9, 2026
Merged via the queue into development with commit c1c0431 Oct 9, 2026
10 checks passed
larsgeorge-db added a commit that referenced this pull request Oct 9, 2026
…eps #860/#862/#863 landed)

# Conflicts:
#	src/frontend/src/views/data-contracts.tsx
#	src/frontend/src/views/data-products.tsx
mvkonchits-db pushed a commit to jgperrin/ontos that referenced this pull request Oct 12, 2026
databrickslabs#930)

* feat(import): single + batch (array-in-file + multi-file) upload for ODCS & ODPS (databrickslabs#855)

Unify contract (ODCS) and product (ODPS) upload so both accept a single entity,
an array of entities in one file, and multiple files in one operation, reported
via a truthful BatchImportResult summary (N created / M skipped / K failed).

Backend
- New shared models.import_results.BatchImportResult / ImportItemResult. The
  `skipped` status and source_id/entity_id provenance fields are part of the
  contract from the start so the sibling import features (databrickslabs#851 domain
  reconciliation, databrickslabs#853 UUID-as-PK duplicate handling) can populate them without
  another response-shape change.
- Contracts: add parse_uploaded_entities (array-in-file parity) and
  create_contracts_from_files; /data-contracts/upload now takes List[UploadFile],
  /odcs/import accepts an object or an array. Both return BatchImportResult.
- Products: extract _parse_products_content, add create_products_from_files;
  /data-products/upload takes List[UploadFile] and returns BatchImportResult.
  Fix a latent bug where json.JSONDecodeError was referenced in an except clause
  before json was imported (only reachable now that non-json files flow through).
- Per-entity failures are recorded as failed items and never abort the batch
  (each entity commits in its own transaction).

Frontend
- Shared types/import-results.ts. data-products and data-contracts views send
  every selected file under `files` (multi-file), and render the truthful
  summary + per-entity failures.

Tests
- New manager-level unit tests for both entities (array-in-file, multi-file,
  bad-entity-continues, unsupported/unparseable file, sourceId preservation).
- Updated the contract upload/import integration tests to the new
  files/BatchImportResult contract.

Session: claude -r 37a1420d-86af-4380-bef3-0b74c6f8b4b9

* feat(import): domain reconciliation + ontos* round-trip on ODCS/ODPS import (databrickslabs#851)

Best-effort domain reconciliation on import, lossless round-trip on export, and a
per-upload "Create missing domains" toggle. Stacked on databrickslabs#855 (feat/import-batch).

Reconciliation (import, both entities via the shared DomainExportAdapter)
- Match ID first, then unique name. Priority: ontosDomainId (authoritative) →
  legacy domainIds/primaryDomainId/domainId → standard `domain` name (+ legacy
  additionalDomains, still read for backward compat).
- No match → left UNASSIGNED (the previous silent auto-create in contracts'
  _resolve_domain is removed). The raw incoming domain string(s) are always
  preserved as the `ontosOriginalDomain` provenance custom property.
- Opt-in "Create missing domains" toggle (default OFF) auto-creates unresolved
  names by name and assigns them. Threaded route → manager → adapter; applied
  per-upload to every entity in the batch. Contract file upload + product upload
  take a `create_missing_domains` form field; the paste /odcs/import takes a query
  param.

Round-trip (export, both entities)
- apply_odcs now emits `ontosDomainId` (all assigned ids, primary first — the
  authoritative rebind key) as a custom property, and keeps the standard `domain`
  = primary name. `ontosOriginalDomain` rides along as a stored property.
- Drops the legacy top-level `domainIds`/`primaryDomainId` and the
  `customProperties.additionalDomains` entry (superseded by `ontosDomainId`);
  both are still READ on import so pre-databrickslabs#851 exports keep working.
- merge_custom_properties now preserves `ontosDomainId` across the export builders'
  customProperties rebuild.

Also fixes a latent `json` UnboundLocalError in build_odcs_from_db: four redundant
inner `import json` statements shadowed the module-level import, crashing the custom-
properties deserialization — newly exercised because ontos* values are list-valued
(stored as JSON strings, deserialized on export).

Frontend
- "Create missing domains" toggle in the contract upload dialog (covers file drop
  and pasted JSON) and a new compact product import-options dialog hosting the same
  toggle; both post the flag with the upload.

Tests
- Reworked test_domain_export_adapter to the ontosDomainId/ontosOriginalDomain
  contract; added create_missing + stale-id-fallthrough + provenance coverage.
- New manager-level test_import_domain_reconcile: match, no-match-unassigned +
  provenance, create_missing toggle, and export→re-import UUID rebind, for both
  contracts and products.

Verified live (isolated 8140/3140): API round-trip rebinds by UUID; UI toggle
auto-creates a missing domain on product upload.

Session: claude -r 37a1420d-86af-4380-bef3-0b74c6f8b4b9

* feat(import): adopt YAML entity UUID as primary key + duplicate handling (databrickslabs#853)

Adopt a valid, non-colliding UUID from the imported ODCS/ODPS file as the entity's
primary key so YAML-expressed cross-entity links (product↔contract) survive import,
with a lossless id round-trip on export. Stacked on databrickslabs#851 (feat/import-domain-recon).

Adoption (both entities)
- create_product / create_from_upload take adopt_ids (default on): a valid UUID that
  does not already exist in that entity's table is adopted as the PK; a non-UUID or a
  colliding/adopt-off id falls back to a generated UUID (today's behavior). Contracts
  adopt via id= on DataContractDb; products via product_data['id'].
- The incoming id is always recorded as the ontosOriginalId provenance custom property;
  the legacy sourceId is still written for non-UUID ids during the transition (read
  either). Export re-derives ontosEntityId (current PK) as a custom property.

Duplicate-id handling (batch, both entities)
- create_*_from_files take on_duplicate = "skip" (default) | "new". A payload id that is
  a valid UUID already present is either recorded as skipped ("already present (same
  id)") — making re-import idempotent — or imported as a new copy with a fresh UUID.
  Import stays create-only. Uses the skipped status already in BatchImportResult (databrickslabs#855).

Routes/frontend
- upload endpoints take adopt_ids + on_duplicate form fields; the paste /odcs/import
  takes them as query params. Both import dialogs gain "Adopt IDs from file" (default on)
  and "Import duplicate IDs as new copies" (default off) toggles; success toasts now
  report skipped counts.

Also fixes build_odps_export: it accessed the ODPS API model by validation-alias names
(product.api_version, input_ports, output_ports, support_channels, custom_properties,
authoritative_definitions, port.contract_id) that are not attributes on the camelCase
model, so the product ODPS export endpoint crashed with AttributeError for every product.
Corrected to the real field names; the endpoint now works (and carries the ontos* keys).

Tests
- New test_import_id_adopt: adopt valid UUID, non-UUID fallback + sourceId/ontosOriginalId,
  ontosEntityId on export, duplicate skip (default) and import-as-new — for both entities.
- Made the domain-export-adapter test's domain fixture get-or-create so the suite is
  order-independent (managers commit, so rows persist across tests in the shared session).

Verified live (isolated 8140/3140): adoption keeps the file UUID; duplicate skip/new both
work; product + contract exports emit ontosOriginalId/ontosEntityId; UI toggles render.

Session: claude -r 37a1420d-86af-4380-bef3-0b74c6f8b4b9

* fix(import): guard batch upload against cross-type ODCS/ODPS files (databrickslabs#929)

The batch importers created a contract from any uploaded file (and a product
from any file on the products page), silently turning ODPS Data Products into
Data Contracts because kind was never checked. Both upload views also only
rendered failed items, so skips were invisible.

- Add src/common/entity_kind.py: classify an entity as contract/product via
  kind (primary) + exclusive structural markers; ambiguous stays unknown.
- create_contracts_from_files / create_products_from_files skip cross-type
  entities as 'skipped' items with a message pointing to the correct page.
- Both upload views surface skipped (not just failed) in the import summary.
- Tests for the classifier and cross-type/mixed-batch import.

Folded into databrickslabs#860 (batch import). Tracks databrickslabs#929.

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* chore: ignore node_modules symlinks, not just directories

Drop the trailing slash so the pattern matches a node_modules symlink (a file to
git) as well as a real directory. The per-worktree node_modules symlinks used in
local dev otherwise slip past 'node_modules/' and can be staged by git add -A.

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* fix(import): reject non-ODCS/ODPS files instead of fabricating entities (databrickslabs#929)

A README (or any free-form text) dropped on the contract uploader was turned
into a junk contract: parse_uploaded_entities wrapped unstructured content into
a minimal contract, and the kind guard only rejected positively-identified
products, letting 'unknown' entities through.

- parse_uploaded_entities now accepts only .yaml/.yml/.json structured payloads;
  an unsupported type or non-mapping/array raises (reported as a failed item).
- Both batch importers now require a positively recognized entity: a contract
  must classify as 'contract', a product as 'product'. Cross-type files are
  skipped with a page hint; unrecognized files are skipped with a clear reason.
- Tests: README/text rejection, structured-but-unrecognized skip, cross-type;
  updated the integration test that asserted the old lenient text fallback.

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* feat(import): unify ODCS/ODPS upload dialogs into one shared component

The Data Contracts and Data Products upload dialogs had drifted apart: one had
a drag-&-drop zone + paste box, the other only a 'Choose file(s)' button and a
subtitle. Extract a single <ImportEntityDialog> used by both so they are
identical: subtitle, the three import toggles, a multi-file drag-&-drop zone,
and a paste-JSON box.

- New components/common/import-entity-dialog.tsx (dropzone multiple:true).
- Both views pass their own i18n labels, toggle state, and upload/paste
  handlers; data-products gains multi-file selection + drag-drop + paste.
- Tighten accept to .json/.yaml/.yml (drop the stale 'plain text' affordance,
  matching the server-side non-ODCS/ODPS rejection).

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* fix(import): correct contract uploader copy to match ODCS-only intent

The dropzone advertised 'plain text' and 'a contract file' (singular); update
the en strings to 'JSON or YAML (ODCS)' and 'file(s)', add the dialog subtitle
key, and drop 'text files' from the upload tooltip — consistent with the shared
ImportEntityDialog and the server-side non-ODCS rejection.

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* fix(export): reuse caller's session in build_odcs_from_db semantic injection

build_odcs_from_db accepts a db_session but the semantic-assignment injection
block ignored it and opened a second session via get_db_session(), which commits
on exit. Under the test harness's shared single in-memory SQLite connection that
commit lands on the outer per-test transaction, defeating its rollback and leaking
committed contracts/products across tests (inflating the global-count assertions
in test_version_family once the import round-trip tests started exercising the
export path). In production it was a redundant session + commit on every export.

Reuse the passed session when present (read-only lookups need no own session);
fall back to get_db_session() only when no session was provided.

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* fix(import): harden batch upload — caps, per-file decode, generic errors (databrickslabs#855 review)

Addresses mvkonchits-db's review on databrickslabs#860:

1. Cap uploads before parsing: new src/common/upload_limits.read_uploads_capped
   enforces max file count, per-file bytes and total bytes (413 on breach),
   reading one byte past the per-file cap so an oversize/alias-heavy payload
   cannot be loaded fully into memory. Both upload routes use it.
2. Decode UTF-8 per file in the contract route (not up front): a single
   non-UTF-8 file is now a failed item, no longer a 500 that aborts the batch.
3. Stop echoing raw exception text to clients — the two contract 500 details,
   the product 500 detail, and the per-entity create-failure item messages now
   return a generic message and log the full detail. (Parse/format messages,
   which describe the user's own file and carry no server internals, are kept.)
4. An empty ODPS array is a failed item (parity with the contract path), not a
   silent "0 imported successfully".

Tests: unit coverage for the caps helper (count/per-file/total 413) and the
product empty-array parity; route tests for non-UTF-8 -> failed item (not 500)
and the file-count 413.

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* fix(import): gate create-missing-domains on data-domains write + record creator (databrickslabs#851 review)

Addresses mvkonchits-db's must-fix on databrickslabs#862 (the authorization issue) plus the
case/whitespace name-match should-fix:

- The "create missing domains" toggle now requires data-domains WRITE on top of
  the entity's own write permission, enforced at all three import endpoints
  (contract upload, contract paste-import, product upload) via a new reusable
  user_has_feature_level() helper. Without it a contract/product writer could
  create top-level domains they cannot otherwise manage; now a clear 403.
- Auto-created domains record the real uploader instead of the repo's "system"
  fallback: DataDomainCreate gains an optional created_by, threaded through
  _create_domain.
- Name reconciliation is now trimmed and case-insensitive
  (data_domain_repo.get_by_name_normalized), so " finance " matches "Finance"
  rather than being left unassigned or creating a near-duplicate under auto-create.

Also merges databrickslabs#860 (upload hardening + the cross-type/non-ODCS guards) so this
branch is a proper superset of its base.

Deferred to a follow-up (reviewer "should fix", not blocking): stale ontosDomainId
list name-fallback (databrickslabs#851.2) and clearing a stale ontosDomainId on domain-less
export (databrickslabs#851.3).

Tests: real-creator attribution and trimmed/case-insensitive match (manager-level).

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

* WIP: db column can_adopt_entity_ids (will squash into feature commit)

* feat(settings): can_adopt_entity_ids app-role privilege (databrickslabs#853 review)

Adds a new per-role boolean privilege shown in the role editor Privileges tab
under a new "Import Privileges" section, as the explicit app-role gate for
adopting an entity's UUID from an imported file as the primary key. The gate
itself (databrickslabs#853.1) replaces the previous default-on-for-any-writer behavior.

- DB: AppRoleDb.can_adopt_entity_ids Boolean (default false, server_default
  'false'); migration b1_add_adopt_entity_ids descends from n1_search_documents.
- Pydantic: AppRoleBase/AppRoleUpdate/AppRole gain can_adopt_entity_ids;
  settings_manager._map_db_to_api surfaces it (repo write path serializes
  booleans unchanged via model_dump).
- AuthorizationManager.user_can_adopt_entity_ids(groups): mirrors
  is_user_ontos_admin, resolves roles by case-insensitive group intersection.
- Frontend: new "Import Privileges" section in the Privileges tab of
  role-form-dialog, mirroring Admin Role; TS AppRole gains the field; i18n keys
  added to all 8 locales (en + de/es/fr/it/ja/nl/pt).

The enforcement at the import routes (and the flip of adopt_ids default to
false) lands in the next commit.

Co-authored-by: Isaac <no-reply@databricks.com>

* fix(import): adopt-ids gate, UUID canonicalization, on_duplicate validation (databrickslabs#853 review)

Addresses mvkonchits-db's must-fix items on databrickslabs#863:

1. ID squatting (databrickslabs#853.1): adopt_ids defaults to False on all three import
   endpoints. Even when explicitly on, adoption requires either the new
   per-role `can_adopt_entity_ids` privilege (prior commit) or feature
   ADMIN on the target entity. Non-privileged callers silently fall back
   to a generated UUID with the file id preserved as ontosOriginalId
   provenance — no hard 403 so unattended tooling degrades gracefully.
2. Canonicalize UUIDs before dup lookup and storage (databrickslabs#853.2): a new
   _canonical_uuid() parses via uuid.UUID (accepting upper/braced/urn/
   hyphenless spellings) and returns the canonical lowercase hyphenated
   form. All three dup-check sites + both adoption sites use it, so a
   spelling variant can no longer bypass the duplicate check or create a
   second identity for the same logical UUID.
3. Validate on_duplicate at the route (databrickslabs#853.3): must be exactly 'skip' or
   'new', otherwise 400. A typo no longer falls through to the "new" path
   and silently duplicates.
4. Skipped-dup existence oracle (databrickslabs#853.5): the batch result for a skipped
   duplicate now only surfaces the existing row's id/name when the caller
   is a feature-admin (new reveal_dup_detail flag computed at the route).
   Non-admin callers get a generic "Supplied id cannot be adopted" skip
   without entity_id/name, so a sibling import can't be used to probe for
   invisible entities.
5. Intra-batch reference remap for on_duplicate=new (databrickslabs#853.4): documented
   as vacuous given current architecture (ODCS carries no intra-file
   cross-refs; ODPS's only cross-ref inputPort.contractId points at
   contracts, which are imported through a separate endpoint). Scaffold
   hook noted for a future cross-entity batch flow.

Tests: canonicalization (UPPERCASE + braced spelling), redaction for
non-admin on both products and contracts, retained reveal-dup-detail
behavior for admin callers.

Co-authored-by: Isaac <no-reply@databricks.com>

* fix(import-dialog): en product-dialog keys + reset on reopen + keep paste on failure + component test (databrickslabs#930 review)

Addresses mvkonchits-db's review on databrickslabs#930 (shared ImportEntityDialog):

- en/data-products.json: add the dialog i18n keys the products view was
  passing (dialogTitle, dialogDescription, createMissingDomains,
  createMissingDomainsHint, adoptIds, adoptIdsHint, duplicatesAsNew,
  duplicatesAsNewHint, dropActive, dropInactive, supportedFormats,
  orPasteOdps, pasteOdpsPlaceholder, importJsonButton, partialSuccess) —
  previously they only resolved via the hard-coded second-arg fallback.
  (The other 6 locales still need parity; defer to the databrickslabs#471 i18n sweep
  so this PR stays focused.)
- Reset dialog state when the dialog opens: both views now zero
  createMissingDomains, adoptIds, duplicatesAsNew, the paste box, and
  any lingering error whenever openUploadDialog transitions true, so a
  later import doesn't silently inherit earlier settings.
- Default adoptIds to false on the UI, matching the backend-level
  privilege gate (databrickslabs#853 review).
- Keep pasted JSON on partial-success/failure: uploadProductFiles now
  returns a boolean (allCreated) and importPastedOdps only clears the
  textarea on full success; the contract-side importPastedOdcs no
  longer clears on partial. The user keeps the input they need to fix
  the failing entries and retry.
- Component test for the shared dialog: label rendering, optional paste
  section, paste-submit wiring, uploading indicator. File-dropzone
  interactions are left to Playwright per the team-dialog precedent
  (jsdom + react-dropzone is unreliable).

Co-authored-by: Isaac <no-reply@databricks.com>

* fix(import): restore lost file_inputs assignment in product upload route (databrickslabs#862 re-review)

The previous commit (4610d15, data-domains gate) silently dropped the
`file_inputs = [(name, raw) for name, raw, _ in capped]` line that databrickslabs#860 had
introduced to feed `create_products_from_files`. Every product upload then
raised `NameError` inside the generic `except Exception`, surfacing as a
generic 500. A PR whose base isn't `development` only runs the title
check, so backend tests never flagged it.

Changes:
- Restore the `file_inputs` build-up right after `read_uploads_capped()`.
- Add two route-level tests: a golden-path product upload (guards the
  NameError regression) and a 403 for `create_missing_domains=True` when
  the user lacks `data-domains` write.
- Expand the `user_has_feature_level` docstring to call out that it
  resolves group-based permissions only (no team/applied-role override)
  and fails closed on resolution error.

Follow-up tracker filed as databrickslabs#944 (stale ontosDomainId on re-import +
clearing a stale id on export).

Session: claude -r 171078d5-61c9-42b9-aeb6-5d378c386f35

Co-authored-by: Isaac <no-reply@databricks.com>

---------

Co-authored-by: Isaac <no-reply@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stakeholder/fpiz Reported by external stakeholder (codename: fpiz)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(import): adopt YAML entity id (UUID) on ODCS/ODPS import to preserve links

2 participants