-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(core): remove taxonomy assignments when content is permanently deleted #3091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ascorbic
merged 2 commits into
emdash-cms:main
from
danielmlr:fix/permanent-delete-taxonomy-assignments
Sep 13, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes permanently deleting an entry leaving its taxonomy term assignments in the database. The assignments are removed with the last translation of the entry and kept while another translation exists, including one in the trash. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/core/tests/integration/content/permanent-delete-terms.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /** | ||
| * Term assignments belong to the translation group, so permanent delete | ||
| * removes them only with the group's last row, trashed rows included. | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, expect, it } from "vitest"; | ||
|
|
||
| import { handleContentPermanentDelete } from "../../../src/api/handlers/content.js"; | ||
| import { ContentRepository } from "../../../src/database/repositories/content.js"; | ||
| import { TaxonomyRepository } from "../../../src/database/repositories/taxonomy.js"; | ||
| import { | ||
| describeEachDialect, | ||
| setupForDialectWithCollections, | ||
| teardownForDialect, | ||
| type DialectTestContext, | ||
| } from "../../utils/test-db.js"; | ||
|
|
||
| describeEachDialect("handleContentPermanentDelete: term assignments", (dialect) => { | ||
| let ctx: DialectTestContext; | ||
| let content: ContentRepository; | ||
| let taxonomies: TaxonomyRepository; | ||
| let termId: string; | ||
|
|
||
| beforeEach(async () => { | ||
| ctx = await setupForDialectWithCollections(dialect); | ||
| content = new ContentRepository(ctx.db); | ||
| taxonomies = new TaxonomyRepository(ctx.db); | ||
| const term = await taxonomies.create({ name: "tag", slug: "news", label: "News" }); | ||
| termId = term.id; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await teardownForDialect(ctx); | ||
| }); | ||
|
|
||
| async function assignmentsFor(group: string) { | ||
| return ctx.db | ||
| .selectFrom("content_taxonomies") | ||
| .select("taxonomy_id") | ||
| .where("collection", "=", "post") | ||
| .where("entry_id", "=", group) | ||
| .execute(); | ||
| } | ||
|
|
||
| async function trashAndPurge(id: string) { | ||
| expect(await content.delete("post", id)).toBe(true); | ||
| const result = await handleContentPermanentDelete(ctx.db, "post", id); | ||
| expect(result.success).toBe(true); | ||
| } | ||
|
|
||
| async function createTranslatedPost() { | ||
| const en = await content.create({ type: "post", locale: "en", data: { title: "Hello" } }); | ||
| const de = await content.create({ | ||
| type: "post", | ||
| locale: "de", | ||
| translationOf: en.id, | ||
| data: { title: "Hallo" }, | ||
| }); | ||
| await taxonomies.attachToEntry("post", en.id, termId); | ||
| return { en, de, group: en.translationGroup! }; | ||
| } | ||
|
|
||
| it("removes the assignments when the last translation is deleted", async () => { | ||
| const post = await content.create({ type: "post", data: { title: "Only" } }); | ||
| await taxonomies.attachToEntry("post", post.id, termId); | ||
| expect(await assignmentsFor(post.translationGroup!)).toHaveLength(1); | ||
|
|
||
| await trashAndPurge(post.id); | ||
|
|
||
| expect(await assignmentsFor(post.translationGroup!)).toEqual([]); | ||
| }); | ||
|
|
||
| it("keeps the assignments while another translation exists", async () => { | ||
| const { en, de, group } = await createTranslatedPost(); | ||
|
|
||
| await trashAndPurge(en.id); | ||
| expect(await assignmentsFor(group)).toHaveLength(1); | ||
|
|
||
| await trashAndPurge(de.id); | ||
| expect(await assignmentsFor(group)).toEqual([]); | ||
| }); | ||
|
|
||
| it("keeps the assignments while another translation is in the trash", async () => { | ||
| const { en, de, group } = await createTranslatedPost(); | ||
| expect(await content.delete("post", de.id)).toBe(true); | ||
|
|
||
| await trashAndPurge(en.id); | ||
|
|
||
| expect(await assignmentsFor(group)).toHaveLength(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] The cleanup block uses a read-then-write pattern: it deletes the content row, probes whether any group rows remain, and then deletes taxonomy assignments. This is wrapped in
withTransaction, butpackages/core/src/database/transaction.tsdocuments that D1 does not support transactions—the wrapper falls back to running the callback directly against the database, so multi-statement atomicity is lost on the production runtime. If the handler fails or the worker is evicted after the content row is deleted but before the assignment cleanup runs, the assignments stay orphaned, which partly undermines the fix.Under the default
READ COMMITTEDisolation used by SQLite and PostgreSQL, the same pattern is also vulnerable to a TOCTOU race: two concurrent permanent deletes of the last two rows in a group can each see the other's row during the probe and both skip cleanup.Consider making the assignment cleanup a single self-checking statement so the delete itself is atomic, e.g. by adding to
TaxonomyRepository:Then replace the probe-and-delete block with a single call to that method after
permanentDelete. This does not close the crash-between-statements gap on D1 (only a future deferred cleanup can), but it removes the read-then-write race and makes the cleanup statement atomic. The same limitation also applies to the existing SEO/comment/revision cleanup in this handler, so fixing it comprehensively may belong in a follow-up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm keeping the probe. I ran both cleanups as plain SQL against PostgreSQL with two connections: each deleted one of a group's last two translations, then ran its cleanup before either committed. Neither saw the other's uncommitted delete, so with the probe and
DELETE … WHERE NOT EXISTSalike, the assignment stayed. Run in sequence, the second cleanup removes it in either case. SQLite lets one connection write at a time (a second connection's delete gotdatabase is locked), and D1 is single-writer, aswithTransactionnotes, so on both, the check after the second delete finds no row left. The D1 gap between statements is the lost multi-statement atomicitywithTransactioncalls a known D1 limitation, and the SEO, comment and revision cleanup here share it, so it's a question for the whole handler, not this PR. The description now states both limits, and that assignments orphaned before this change stay.