Skip to content

Fix /schema/apply dropping a collection when its first diff entry is a nested meta delete - #27880

Merged
Nitwel merged 19 commits into
directus:mainfrom
thribhuvan003:fix/schema-apply-nested-meta-delete
Sep 17, 2026
Merged

Nitwel merged 19 commits into
directus:mainfrom
thribhuvan003:fix/schema-apply-nested-meta-delete

Conversation

@thribhuvan003

@thribhuvan003 thribhuvan003 commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

What's Changed

Fixes #27877

Nested additions and removals inside meta were emitted by deep-diff as N or D. Since schema apply uses the first diff kind to choose between create, update, and delete, those property changes could be mistaken for whole-entity operations.

This now gets normalized in getSnapshotDiff, before the diff reaches validation or apply:

  • when both entities exist, nested metadata additions/removals are emitted as one E change at meta
  • root-level N and D remain unchanged for actual entity creation and deletion
  • the same behavior is applied to collections, fields, and relations

Tests

Added regression coverage for:

  • adding and removing nested metadata on collections, fields, and relations
  • keeping field and relation changes when a collection has a nested metadata removal
  • creating a grouped child collection when its existing parent has a nested metadata addition

Checklist

Leave unchecked where not applicable

  • Tests added/updated
  • Documentation PR created in directus/docs
  • OpenAPI updated
  • SDK (@directus/sdk) updated to reflect the changes

@thribhuvan003
thribhuvan003 force-pushed the fix/schema-apply-nested-meta-delete branch from c375176 to 5702a70 Compare July 22, 2026 13:02

@ComfortablyCoding ComfortablyCoding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!

This partially addresses the issue but looks to miss the following cases:

  • Adding a meta property to an existing collection is treated as creating a new collection.
  • For D cases (and potentially N), all field/relation changes are dropped before apply runs.
  • Slightly outside the current scope but should be addressed: Adding or removing a meta property on a relation has the same issues.

@thribhuvan003

Copy link
Copy Markdown
Contributor Author

good catch, all three were real.

the field/relation drop was get-snapshot-diff's deletedCollections filter, same unguarded kind check. guarded it there.

collection creation now skips nested meta adds, and relations got the same three guards fields already had.

added a test for each, all four fail on main.

thanks for merging main in.

@g-hausammann

Copy link
Copy Markdown

Reporter of #27877 here. I went back over this against current main (197d141a, 2026-08-07) and wanted to leave what I found, in case it helps the re-review.

The three review points are addressed in the diff. Mapping each to the code it touches:

  • A meta property added to an existing collection is treated as creating a new collection — filterCollectionsForCreation now also tests !isNestedMetaUpdate(...).
  • For D cases, field/relation changes are dropped before apply runs — the deletedCollections filter uses the same unguarded kind === DELETE test and is now guarded too. This was the third point in my original report, and it's better handled here than separately — a client that strips deletions from the diff silently loses genuine field changes along with them.
  • Relations have the same issue — the relation NEW/EDIT/DELETE branches get the guards the field branches already had; the relation delete is unguarded today.

One thing changed after the review was written. v12.2.0 introduced is-delete-allowed.ts, which suppresses whole-entity deletions in merge mode but deliberately lets nested-path ones through:

// `deep-diff` reports a whole-entity deletion as a root-level `D` (i.e. no `path`)
return change.path !== undefined;

A nested-meta D has a path, so it survives the merge filter and then reaches the unguarded collection delete. Without this PR, that means merge — the additive mode — does not prevent the drop. With this PR the entry is routed to the update branch instead and the case is handled correctly. So this isn't a gap in the PR; it raises what the PR is worth.

Still reproducible: apply-diff.ts:182-186 is unchanged from v11.17.4.

Happy to test a build against a real schema with a few hundred custom collections if that would be useful.

(For the record: #28030 covered the same ground and was closed shortly after it opened.)

@ComfortablyCoding ComfortablyCoding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the updates, I believe their are still some gaps present, for example:

New grouped collection is dropped if nested change is present in parent
  1. Create parent collection parent and generate snapshot
  2. Adjust generated snapshot to have a:
  • a new property to parent.meta
  • a new collection named child
  1. Build diff based on the adjusted snapshot
  2. Apply snapshot, observe child is not actually created

Let's improve test coverage here, given the missing edge cases seems we do still have gaps.

@thribhuvan003

Copy link
Copy Markdown
Contributor Author

good catch on the parent meta update, guarded \parentWillBeCreatedInThisApply\ with !isNestedMetaUpdate\ so nested changes on an existing parent don't cause new grouped children to be skipped, and added a test covering that case. all 25 apply-diff unit tests pass.

@Nitwel Nitwel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of having a isNestedMetaUpdate, could we maybe look into fixing this a step before, just after the diff generation, so that the final diff reflects 100% on what changes will actually be made?

@thribhuvan003

Copy link
Copy Markdown
Contributor Author

Good point. Refactored away from isNestedMetaUpdate and addressed this directly at the diff classification level.

deep-diff emits root-level changes without a path when an entire entity is created or deleted, whereas property-level changes on existing entities (in meta, schema, etc.) always include path.

Added isNewEntity and isDeletedEntity helpers in diff-helpers.ts checking for root-level changes (!diff[0]?.path), removing the circular dependency between get-snapshot-diff.ts and apply-diff.ts.

Updated getSnapshotDiff so deletedCollections only matches actual root-level collection drops, as well as validateDiff, CLI output, and applyDiff to consistently separate whole-entity creation/deletion from nested property updates.

@thribhuvan003
thribhuvan003 force-pushed the fix/schema-apply-nested-meta-delete branch from 4b96fbf to d5b433c Compare September 11, 2026 04:15

@Nitwel Nitwel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As I said before, please move your changes to before the actual applying logic so that when we start applying the diff, it is the final diff and the changes reflect 1:1 on what is gonna be changed.

thribhuvan003 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying. I’ve moved the fix into diff generation, removed the apply-time changes, and added regression tests for the cases raised. Please let me know if I’ve missed anything.

@Nitwel Nitwel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Added e2e tests, but finally looks good to me! Thanks for the work.

@Nitwel
Nitwel merged commit 5cf557b into directus:main Sep 17, 2026
99 of 101 checks passed
@github-actions github-actions Bot added this to the Next Release milestone Sep 17, 2026
@thribhuvan003

Copy link
Copy Markdown
Contributor Author

Thanks for the guidance @Nitwel, appreciate the reviews and support.❤️

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/schema/apply drops an entire collection (DROP TABLE + data loss, HTTP 204) when a collection's first diff entry is a nested meta delete

4 participants