Skip to content

fix: prevent foreign key violations during entity merges#3555

Merged
skwowet merged 19 commits intomainfrom
bugfix/stuck-organization-merge
Oct 30, 2025
Merged

fix: prevent foreign key violations during entity merges#3555
skwowet merged 19 commits intomainfrom
bugfix/stuck-organization-merge

Conversation

@skwowet
Copy link
Copy Markdown
Collaborator

@skwowet skwowet commented Oct 29, 2025

Changes

1. Added override cleanup to role deletion
Modified removeMemberRole to delete associated overrides before removing the role, preventing foreign key violations.

2. Refactored merge logic to preserve overrides
Restructured mergeRoles into a 3-phase approach:

  • Analyze: Plan which roles to remove/add and track their original IDs
  • Remove: Delete roles while storing their overrides for recreation
  • Add: Create new roles and intelligently transfer overrides to them (or to existing duplicate roles)

3. Fixed role transformation
When moving a role from secondary to primary entity, it's now properly transformed with the target entity's ID and the original is removed, preventing orphaned records.

4. Updated legacy code paths
Added override cleanup and transactions to older enrichment worker functions for consistency.

@skwowet skwowet self-assigned this Oct 29, 2025
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Jira Issue Key Missing

Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.

Example:

  • feat: add user authentication (CM-123)
  • feat: add user authentication (IN-123)

Projects:

  • CM: Community Data Platform
  • IN: Insights

Please add a Jira issue key to your PR title.

@skwowet skwowet requested review from Copilot and ulemons October 29, 2025 16:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors organization and member-organization deletion logic to ensure proper cleanup of related data, particularly focusing on cascade deletion of affiliation overrides and enrichment data.

Key changes:

  • Added new function deleteOrganizationEnrichment to handle deletion of organization enrichment data
  • Refactored deleteOrganizationAttributes to accept organization IDs instead of attribute IDs and removed the now-redundant deleteOrgAttributesByOrganizationId function
  • Enhanced member-organization deletion functions to properly clean up affiliation overrides before deleting roles
  • Significantly refactored the mergeRoles function to handle affiliation overrides more reliably during role merging operations

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
services/libs/data-access-layer/src/organizations/enrichment/index.ts Adds deleteOrganizationEnrichment function to delete enrichment data for an organization
services/libs/data-access-layer/src/organizations/attributes.ts Renames and refactors deleteOrgAttributes to deleteOrganizationAttributes, changing parameter from attribute IDs to organization IDs
services/libs/data-access-layer/src/old/apps/script_executor_worker/organization.repo.ts Adds enrichment-related tables to cleanup list
services/libs/data-access-layer/src/old/apps/members_enrichment_worker/index.ts Updates deleteMemberOrg and deleteMemberOrgById to properly delete affiliation overrides
services/libs/data-access-layer/src/members/segments.ts Adds early return guard for empty segment IDs
services/libs/data-access-layer/src/members/organizations.ts Major refactoring of mergeRoles and updates to removeMemberRole to handle affiliation overrides
services/apps/merge_suggestions_worker/src/activities/organizationMergeSuggestions.ts Removes debug logging statements
services/apps/entity_merging_worker/src/activities/organizations.ts Updates to use renamed functions and adds enrichment deletion
backend/src/database/repositories/organizationRepository.ts Updates import and usage of renamed function
backend/src/database/repositories/memberOrganizationRepository.ts Adds deletion of affiliation overrides before role deletion

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread services/libs/data-access-layer/src/members/organizations.ts Outdated
Comment thread services/libs/data-access-layer/src/members/organizations.ts
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Jira Issue Key Missing

Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.

Example:

  • feat: add user authentication (CM-123)
  • feat: add user authentication (IN-123)

Projects:

  • CM: Community Data Platform
  • IN: Insights

Please add a Jira issue key to your PR title.

@skwowet skwowet requested a review from themarolt October 29, 2025 16:53
Comment thread services/libs/data-access-layer/src/members/organizations.ts Outdated
Comment thread services/libs/data-access-layer/src/members/organizations.ts Outdated
Comment thread services/libs/data-access-layer/src/members/organizations.ts Outdated
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Jira Issue Key Missing

Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.

Example:

  • feat: add user authentication (CM-123)
  • feat: add user authentication (IN-123)

Projects:

  • CM: Community Data Platform
  • IN: Insights

Please add a Jira issue key to your PR title.

@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Jira Issue Key Missing

Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.

Example:

  • feat: add user authentication (CM-123)
  • feat: add user authentication (IN-123)

Projects:

  • CM: Community Data Platform
  • IN: Insights

Please add a Jira issue key to your PR title.

@skwowet skwowet merged commit 63710de into main Oct 30, 2025
13 checks passed
@skwowet skwowet deleted the bugfix/stuck-organization-merge branch October 30, 2025 08:26
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.

4 participants