Skip to content

fix: delete branches after content delivery configs in hard delete - #3853

Open
bdshadow wants to merge 1 commit into
mainfrom
bdshadow/fix-hard-delete-project-branch-cdn
Open

fix: delete branches after content delivery configs in hard delete#3853
bdshadow wants to merge 1 commit into
mainfrom
bdshadow/fix-hard-delete-project-branch-cdn

Conversation

@bdshadow

@bdshadow bdshadow commented Aug 11, 2026

Copy link
Copy Markdown
Member

Problem

The orphan-project purge scheduler (DeletedOrganizationProjectPurgeSchedulerProjectHardDeletingService.hardDeleteProject) is failing in production and leaving projects of deleted organizations undeleted (deleted_at set on the org, projects never removed).

Sentry shows the current failure:

update or delete on table "branch" violates foreign key constraint
"FK5jtc226unswiodyqp8r4isds6" on table "content_delivery_config"
  at io.tolgee.service.project.ProjectHardDeletingService.hardDeleteProject

hardDeleteProject deleted branches (branchService.deleteAllByProjectId) before the content_delivery_config rows, which reference branch via branch_id. A single project with branch-scoped content delivery configs is enough to throw, and because the scheduler processes projects in a batch, that one project aborts the whole purge run — which is why deleted-org projects keep piling up.

Fix

  • Move branchService.deleteAllByProjectId to run after the ContentDeliveryConfig deletion.
  • Flush/clear right after it. branchService load-and-removes the branch entities, which previously reached the DB via the flush() that immediately followed the old call site. Moving the call without a flush left the branches in the session, so DELETE FROM Project then failed on branch.project_id → project. The explicit flush pushes the branch removals to the DB before the project row is deleted.

Audited all 9 FKs into branch; every other referencing table (key, import, task, language_stats, and the branch-internal ones) is already deleted before branches — content_delivery_config was the only one out of order.

Test

Adds deletes project with content delivery configs on branches to ProjectHardDeletingServiceTest, using the existing ContentDeliveryConfigBranchingTestData (CDN configs on main/feature branches). It reproduced both FK violations before the fix and passes now; the full suite is green.

Context

Follow-up to the same purge-path FK-ordering work as #3810 — this is the next constraint the scheduler surfaced once the earlier imports/tasks gaps were fixed.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where projects with branch-based content delivery configurations could not be fully deleted.
    • Improved project deletion cleanup to ensure related configurations and branches are removed correctly.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Project hard deletion now removes content delivery configurations before branches, flushes branch deletions, and then continues cleanup. A regression test covers projects with branch-based content delivery configurations and verifies that hard deletion removes the project.

Changes

Project hard deletion

Layer / File(s) Summary
Order dependent entity cleanup
backend/data/src/main/kotlin/io/tolgee/service/project/ProjectHardDeletingService.kt
Branch deletion now follows content delivery configuration deletion. The persistence context is flushed before project cleanup continues.
Branch configuration deletion test
backend/app/src/test/kotlin/io/tolgee/service/project/ProjectHardDeletingServiceTest.kt
The test creates branch-based content delivery configuration data, hard-deletes the project, and verifies that the project no longer exists.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: anty0, dkrizan, stepan662

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the fix: deleting branches after content delivery configurations during hard deletion.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bdshadow/fix-hard-delete-project-branch-cdn

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

hardDeleteProject deleted branches before content_delivery_config rows,
which reference branch via branch_id, so deleting a project that had
branch-scoped content delivery configs failed with a foreign key
violation. This aborted the orphan-project purge batch in production,
leaving projects of deleted organizations undeleted.

Move branchService.deleteAllByProjectId after the ContentDeliveryConfig
deletion, and flush right after it so the branch removals (done through
the persistence context) reach the DB before the project row is deleted
(branch.project_id references project).
@bdshadow
bdshadow force-pushed the bdshadow/fix-hard-delete-project-branch-cdn branch from 537cb6b to 08badd6 Compare August 11, 2026 10:39
@bdshadow
bdshadow requested a review from Anty0 August 11, 2026 13:59
@bdshadow
bdshadow marked this pull request as ready for review August 11, 2026 13:59

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@backend/app/src/test/kotlin/io/tolgee/service/project/ProjectHardDeletingServiceTest.kt`:
- Around line 139-140: Assign the newly created
ContentDeliveryConfigBranchingTestData instance to testDataToClean before
calling testDataService.saveTestData, so cleanTestDataAfterTest can remove the
fixture during `@AfterEach` cleanup.

In
`@backend/data/src/main/kotlin/io/tolgee/service/project/ProjectHardDeletingService.kt`:
- Around line 189-190: Update the branch deletion sequence in
ProjectHardDeletingService to call entityManager.clear() immediately after
entityManager.flush(), ensuring the persistence context is cleared before
subsequent bulk deletes.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e440cbbd-9d88-4e18-b8a9-8285a4320b25

📥 Commits

Reviewing files that changed from the base of the PR and between dd7f597 and 08badd6.

📒 Files selected for processing (2)
  • backend/app/src/test/kotlin/io/tolgee/service/project/ProjectHardDeletingServiceTest.kt
  • backend/data/src/main/kotlin/io/tolgee/service/project/ProjectHardDeletingService.kt

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