Skip to content

Fixes #39793: stale foreign keys no longer block schema restore (backport 2.1 #39812) - #39844

Merged
manerow merged 1 commit into
2.1from
fix/39793-stale-fk-restore-2.1
Dec 4, 2026
Merged

manerow merged 1 commit into
2.1from
fix/39793-stale-fk-restore-2.1

Conversation

@manerow

@manerow manerow commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Issue

Cherry-pick of #39812 (Fixes #39793) onto 2.1.

Restoring a soft-deleted schema failed with Invalid column name <col> when a table in it had a foreign key to a column that was later removed from another table. 2.1.1 has the bug.

What this PR changes

The same change as #39812, applied cleanly:

  • Removing a column now also drops the foreign keys in other tables that point to it, soft-deleted tables included. A column whose data type changes keeps the keys that point to it.
  • A foreign key a table already stores is no longer rejected when its target column is gone. It is kept and a warning is logged, so keys left stale by older versions stop blocking writes and restores. A new or changed foreign key must still point to an existing column.
  • The error now names the table: Invalid column name pos_id in table <fqn>.

Differences from main

One import (Relationship) added to TableResourceIT. The 2.1 copy of that file did not import it, and a new test helper uses it. The production change is identical to main (same patch id).

Tests

Backend integration tests, in TableResourceIT:

  • Remove a column that another table's foreign key points to, then soft-delete and restore that table's schema: the restore succeeds and only that foreign key is gone.
  • The same when the other table's schema is already soft-deleted when the column is removed.
  • A table that already stores a stale foreign key restores, and the key is kept.
  • A new foreign key to a missing column returns 400, and the message names the table.
  • Changing a column's data type keeps the foreign keys that point to it.

The 5 new tests and the 3 existing column-removal tests pass on MySQL (8 of 8) on this branch. The same tests were proven red on the old code on main (4 of the 5 fail there).

Not run: Postgres and the rest of TableResourceIT on 2.1.

@manerow
manerow requested a review from a team as a code owner December 4, 2026 13:29
@manerow
manerow requested review from pmbrull and removed request for a team December 4, 2026 13:29
@github-actions

github-actions Bot commented Dec 4, 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.

Comment on lines +1698 to +1712
private void removeConstraintsReferringTo(Table table, Set<String> removedColumns) {
if (removedColumns.isEmpty()) {
return;
}
List<EntityReference> referrers =
findFrom(table.getId(), TABLE, Relationship.RELATED_TO, TABLE, ALL);
for (EntityReference referrer : referrers) {
if (!referrer.getId().equals(table.getId())) {
dropConstraintsReferringTo(referrer.getId(), table, removedColumns);
}
}
}

private void dropConstraintsReferringTo(
UUID referrerId, Table table, Set<String> removedColumns) {

@gitar-bot gitar-bot Bot Dec 4, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Quality: New helpers use guard-clause returns, contradicting the returns-once ADR

Two new helpers break ADR:2026-11-20-a-method-returns-once, which says void methods have no return and no guard clauses. removeConstraintsReferringTo opens with if (removedColumns.isEmpty()) { return; }, and dropConstraintsReferringTo returns early with if (kept.size() == stored.size()) { return; }. The review rules treat a contradiction of an Accepted record as blocking. Either wrap the work in the condition that allows it, or add a record that supersedes the ADR. The empty-set guard isn't needed anyway: with an empty set, refersToRemovedColumn never matches, so nothing gets written.

Put the work inside the allowing condition instead of returning early:

private void removeConstraintsReferringTo(Table table, Set<String> removedColumns) {
  if (!removedColumns.isEmpty()) {
    findFrom(table.getId(), TABLE, Relationship.RELATED_TO, TABLE, ALL).stream()
        .filter(referrer -> !referrer.getId().equals(table.getId()))
        .forEach(referrer -> dropConstraintsReferringTo(referrer.getId(), table, removedColumns));
  }
}

// in dropConstraintsReferringTo:
if (kept.size() != stored.size()) {
  persistKeptConstraints(original, kept, table);
  LOG.info(...);
}

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 🔴 High risk 0 closed / 1 findings

Changing foreign-key handling by dropping references to removed columns and retaining stale stored keys could leave table relationships inconsistent.

Adds fixes so removing a column also drops foreign keys in other tables that point to it, and stale stored foreign keys no longer block schema restore or writes; the error now names the table.

  • One open finding: the new helpers removeConstraintsReferringTo and dropConstraintsReferringTo in TableRepository.java use guard-clause early returns, which contradicts the returns-once ADR 2026-11-20-a-method-returns-once. The empty-set guard is also unnecessary and should be removed, or the ADR should be superseded by a new record.
⚠️ Quality: New helpers use guard-clause returns, contradicting the returns-once ADR

📄 openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TableRepository.java:1698-1712

Two new helpers break ADR:2026-11-20-a-method-returns-once, which says void methods have no return and no guard clauses. removeConstraintsReferringTo opens with if (removedColumns.isEmpty()) { return; }, and dropConstraintsReferringTo returns early with if (kept.size() == stored.size()) { return; }. The review rules treat a contradiction of an Accepted record as blocking. Either wrap the work in the condition that allows it, or add a record that supersedes the ADR. The empty-set guard isn't needed anyway: with an empty set, refersToRemovedColumn never matches, so nothing gets written.

Put the work inside the allowing condition instead of returning early
private void removeConstraintsReferringTo(Table table, Set<String> removedColumns) {
  if (!removedColumns.isEmpty()) {
    findFrom(table.getId(), TABLE, Relationship.RELATED_TO, TABLE, ALL).stream()
        .filter(referrer -> !referrer.getId().equals(table.getId()))
        .forEach(referrer -> dropConstraintsReferringTo(referrer.getId(), table, removedColumns));
  }
}

// in dropConstraintsReferringTo:
if (kept.size() != stored.size()) {
  persistKeptConstraints(original, kept, table);
  LOG.info(...);
}
🤖 Prompt for agents
Code Review: Adds fixes so removing a column also drops foreign keys in other tables that point to it, and stale stored foreign keys no longer block schema restore or writes; the error now names the table.
  
  - One open finding: the new helpers `removeConstraintsReferringTo` and `dropConstraintsReferringTo` in `TableRepository.java` use guard-clause early returns, which contradicts the returns-once ADR `2026-11-20-a-method-returns-once`. The empty-set guard is also unnecessary and should be removed, or the ADR should be superseded by a new record.

1. ⚠️ Quality: New helpers use guard-clause returns, contradicting the returns-once ADR
   Files: openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TableRepository.java:1698-1712

   Two new helpers break ADR:2026-11-20-a-method-returns-once, which says void methods have no `return` and no guard clauses. `removeConstraintsReferringTo` opens with `if (removedColumns.isEmpty()) { return; }`, and `dropConstraintsReferringTo` returns early with `if (kept.size() == stored.size()) { return; }`. The review rules treat a contradiction of an Accepted record as blocking. Either wrap the work in the condition that allows it, or add a record that supersedes the ADR. The empty-set guard isn't needed anyway: with an empty set, `refersToRemovedColumn` never matches, so nothing gets written.

   Fix (Put the work inside the allowing condition instead of returning early):
   private void removeConstraintsReferringTo(Table table, Set<String> removedColumns) {
     if (!removedColumns.isEmpty()) {
       findFrom(table.getId(), TABLE, Relationship.RELATED_TO, TABLE, ALL).stream()
           .filter(referrer -> !referrer.getId().equals(table.getId()))
           .forEach(referrer -> dropConstraintsReferringTo(referrer.getId(), table, removedColumns));
     }
   }
   
   // in dropConstraintsReferringTo:
   if (kept.size() != stored.size()) {
     persistKeptConstraints(original, kept, table);
     LOG.info(...);
   }

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

@manerow
manerow merged commit 6dfc128 into 2.1 Dec 4, 2026
44 of 46 checks passed
@manerow
manerow deleted the fix/39793-stale-fk-restore-2.1 branch December 4, 2026 14:00
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