fix: prevent foreign key violations during entity merges#3555
Conversation
…comments and debug output
…letedAt timestamp
|
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability. Example:
Projects:
Please add a Jira issue key to your PR title. |
There was a problem hiding this comment.
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
deleteOrganizationEnrichmentto handle deletion of organization enrichment data - Refactored
deleteOrganizationAttributesto accept organization IDs instead of attribute IDs and removed the now-redundantdeleteOrgAttributesByOrganizationIdfunction - Enhanced member-organization deletion functions to properly clean up affiliation overrides before deleting roles
- Significantly refactored the
mergeRolesfunction 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.
|
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability. Example:
Projects:
Please add a Jira issue key to your PR title. |
|
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability. Example:
Projects:
Please add a Jira issue key to your PR title. |
|
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability. Example:
Projects:
Please add a Jira issue key to your PR title. |
Changes
1. Added override cleanup to role deletion
Modified
removeMemberRoleto delete associated overrides before removing the role, preventing foreign key violations.2. Refactored merge logic to preserve overrides
Restructured
mergeRolesinto a 3-phase approach: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.