Skip to content

Backport #31863 + #32113 to 2.0: keep table constraints and FK edges on recursive CSV import - #37244

Merged
sonika-shah merged 4 commits into
2.0from
backport/2.0-csv-recursive-constraints-fk
Nov 6, 2026
Merged

sonika-shah merged 4 commits into
2.0from
backport/2.0-csv-recursive-constraints-fk

Conversation

@sonika-shah

Copy link
Copy Markdown
Collaborator

Summary

Backport to 2.0 of two related recursive CSV import fixes (import at service / database / schema level):

They ship together because #32113 rebuilds the FK edge from tableConstraints; without #31863 the constraints are already dropped on 2.0 and there is nothing to rebuild from. The earlier 2.0 backport of #31863 (#32095) was auto-closed as stale and never merged.

What changed

Verification (on this branch)

  • mvn spotless:check and mvn clean install -pl openmetadata-service,openmetadata-integration-tests -am -DskipTests: pass
  • EntityCsvTest: 68/68 pass
  • DatabaseServiceResourceIT#test_importExportRecursive_preservesTableConstraints (MySQL + Elasticsearch): 1/1 pass. Postgres lane left to CI.
Backport details

Cherry-picked with -x, all applied without conflicts:

  1. 0a603cdd45b (Fixes #31857, #31858: preserve table constraints and column custom properties on recursive CSV import #31863 squash commit): diff stats identical to main (5 files, +277 / -4).
  2. 25880b2e7c, 4482fac6cd, 23bf8e238fe (fix(csv): restore FK RELATED_TO edge on recursive/bulk import #32113 commits; the merge-main commits are not included).

The resulting TableRepository FK methods and the IT test are line-for-line identical to the #32113 head on main. If #32113 changes further during review, this PR will be updated to match.

…operties on recursive CSV import (#31863)

* Fixes #31857: preserve table constraints on recursive CSV import

createTableEntity fetched the existing table without tableConstraints/
tablePartition, so the subsequent createOrUpdate persisted them as null and
the recursive CSV import silently dropped every table's PK/UNIQUE/FK. Load
those fields so they carry through unchanged.

Adds DatabaseServiceResourceIT.test_importExportRecursive_preservesTableConstraints.

* test: cover FOREIGN_KEY (ER) in recursive CSV constraint round trip

Adds a referenced table and a FOREIGN_KEY constraint (with referredColumns)
so the recursive round trip also asserts the referenced-table linkage
survives, not just PRIMARY_KEY/UNIQUE.

* Fixes #31858: preserve column custom properties on recursive CSV import

createTableEntity fetched the table with "extension" but not "columns";
TableRepository.setFields only hydrates column.extension when both fields are
requested, so the recursive table-row createOrUpdate persisted columns with
null extension and dropped column custom properties before the column rows
patched back. Add "columns" to the fetch so column extension is hydrated and
carried through.

Adds DatabaseServiceResourceIT.test_importExportRecursive_preservesColumnCustomProperties.
Verified: 3 recursive round-trip ITs pass (constraints+FK, column custom
properties, and the pre-existing column tags/glossary test) — Tests run: 3,
Failures: 0, Errors: 0.

* test: update EntityCsvTest mock stub for new createTableEntity fetch fields

createTableEntity now fetches tableConstraints,tablePartition,columns; the
unit test stubbed getEntityWithDependencyResolution on the exact old field
string, so the stub stopped matching and returned null -> NPE. Update the stub
to the new field string. EntityCsvTest: Tests run: 65, Failures: 0.

* Fixes #31858: preserve column custom properties via targeted removeExtensions

The complete fix. The bulk import update path (updateManyEntitiesForImport)
called removeExtensions(originals), which did a blanket
entityExtensionDAO.deleteAllBatch(id) -- deleting EVERY entity_extension row
for a table (including its columnExtension rows) whenever the entity carried
table-level extension -- then re-stored only the table-level extension. So a
recursive CSV import of a table that had BOTH table-level and column-level
custom properties silently dropped the column custom properties.

Make removeExtensions targeted, mirroring the single-entity removeExtension()
(delete only the entity's own custom-property rows), so columnExtension rows
are no longer collateral-deleted.

Also revert the earlier createTableEntity 'columns' fetch addition (it only
masked the column-only case; the real defect was the blanket delete above) --
the fetch keeps tableConstraints,tablePartition for #31857.

Strengthen DatabaseServiceResourceIT.test_importExportRecursive_preservesColumnCustomProperties
to define + set a table-level custom property alongside column-level ones, so
the regression (which only manifests when both are present) can't false-green.

Verified live on a 2.0 stack: recursive export->import at table/schema/database/
service now preserves table + column custom properties AND table constraints;
EntityCsvTest 65/0; the 3 recursive ITs pass.

* perf(csv-import): batch removeExtensions custom-property delete (avoid N+1)

The prior fix looped removeExtension(entity) per entity, which fires one DELETE
per custom-property field -- an N+1 for bulk imports of many entities.

Restore a single batched delete but scope it to the entity's own custom-property
rows via a new jsonSchema-filtered DAO batch:
  DELETE FROM entity_extension WHERE id IN (<ids>) AND jsonSchema = 'customFieldSchema'
run one statement per chunk (updateInChunks). This keeps columnExtension (and
version) rows untouched -- so column custom properties still survive a bulk
import that updates table-level extension -- without the per-field N+1.

Verified: EntityCsvTest 65/0; DatabaseServiceResourceIT recursive tests 0 fail
(incl. table+column custom properties); ColumnCustomPropertiesIT 30/0.

(cherry picked from commit 0a603cd)
The bulk relationship path was asymmetric for tables: clearEntitySpecificRelationshipsForMany
deletes both the schema CONTAINS edge and the foreign-key RELATED_TO (ER) edge, but
storeEntitySpecificRelationshipsForMany only re-added CONTAINS. So on a recursive CSV import
(and bulk create), a table's FK relationship to its referenced table disappeared even though the
tableConstraints JSON was preserved. The single-entity path is unaffected (storeEntity ->
addConstraintRelationship), which is why non-recursive imports kept the edge.

Fix: after the bulk CONTAINS insert, re-add the constraint RELATED_TO edges via
addConstraintRelationship, mirroring the single-entity store path.

Test: DatabaseServiceResourceIT#test_importExportRecursive_preservesTableConstraints now also
asserts the RELATED_TO edge survives a recursive round trip (read from the DAO, not the
eventually-consistent search endpoint). Verified before/after on a live stack: without the fix the
edge is dropped, with the fix it survives.

(cherry picked from commit 25880b2)
Addresses two review points on the initial fix:

- N+1: the per-table addConstraintRelationship() did a getEntityReferenceByName lookup
  and a single addRelationship insert per foreign key. Replaced with buildConstraintRelationships(),
  which resolves all referred tables in one findByNames() query and folds the RELATED_TO edges into
  the caller's existing bulkInsertRelationships() call -- no per-FK query or insert.

- Cross-chunk forward reference (greptile P1): addConstraintRelationship() resolved the referred
  table eagerly and threw EntityNotFoundException when the target was created later in another bulk
  chunk, failing the source table with BAD_REQUEST. The batched resolver skips a referred table that
  is not yet persisted instead of throwing, so a forward reference cannot fail the request. The edge
  is best-effort in the bulk path and is created whenever the target already exists -- always true
  for a recursive re-import of existing tables, the case that regressed. Duplicate edges are harmless
  (bulkInsertTo is INSERT IGNORE / ON CONFLICT DO NOTHING).

Re-verified: recursive-import edge IT still GREEN, and live before/after confirms the FK edge
survives a recursive import.

(cherry picked from commit 4482fac)
Referred tables already in the batch (the common recursive-import case) are
resolved from memory, so only out-of-batch targets hit findByNames. Also walks
constraints once and dedupes composite-FK targets per table.

(cherry picked from commit 23bf8e2)
@sonika-shah
sonika-shah requested a review from a team as a code owner November 6, 2026 08:20
@sonika-shah
sonika-shah requested review from manerow and removed request for a team November 6, 2026 08:20
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Nov 6, 2026
@gitar-bot

gitar-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved

🔴 High risk · Recursive CSV persistence now retains constraints and custom properties and rebuilds FK edges; unresolved targets could leave imported table relationships incomplete.

Backports recursive CSV import fixes to 2.0 that keep table constraints and column custom properties and restore the foreign-key RELATED_TO edge between tables, with FK targets resolved from the import batch before querying. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@sonika-shah
sonika-shah merged commit 1021d5a into 2.0 Nov 6, 2026
60 of 62 checks passed
@sonika-shah
sonika-shah deleted the backport/2.0-csv-recursive-constraints-fk branch November 6, 2026 10:36

This branch was previously deployed

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

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants