Problem/Motivation
When an entity tracked by a workspace is deleted, the workspace association table is not updated to remove the association data for that entity.
Steps to reproduce
Create a node inside a workspace and delete it. The workspace_association still tracks the deleted entity.
Proposed resolution
Implement hook_entity_delete() and hook_entity_revision_delete() to maintain data integrity in the workspace_association table.
Remaining tasks
Review.
User interface changes
Nope.
API changes
A new optional argument is added to \Drupal\workspaces\WorkspaceAssociationInterface::deleteAssociations().
Data model changes
Nope.
Release notes snippet
TBD.

| Comment | File | Size | Author |
|---|---|---|---|
| #8 | interdiff-8.txt | 1.28 KB | amateescu |
| #8 | 3216107-8.patch | 7.02 KB | amateescu |
| #6 | interdiff-6.txt | 1.97 KB | amateescu |
| #6 | 3216107-6.patch | 7.03 KB | amateescu |
| #2 | 3216107.patch | 6.85 KB | amateescu |
Comments
Comment #2
amateescu commentedThis should do it.
Comment #4
fabianx commentedThat's a pretty dangerous function. Do we have other functions in Core that allows deleting all entities of a given type? [e.g. if I call this with just an entity_type_id I would all entities (which are just the associations) across all workspaces for that type]
So do we really want to have such a dangerous interface or is there a user for that case?
How does this prevent a workspace_association entity to recursively call this?
Should this not check if the entity is supported by workspaces instead of just `!== workspace`?
Feels wasteful to do a no-op delete query.
Is it safe to provide revision_id without entity_id?
(In general I think yeah - just feels strange to me)
I would probably pass also the entity_id as usually a query is for entity_id + revision_id (as long as appropriate indexes are on there).
4. Overall the patch is great and fixes a major unexpected bug. I think given that delete is always such a dangerous operation, I would add some more safety measures - like needing a entity_type + entity_id or (workspace_id) as required argument.
5.I also think it would be good to see that the entity_type / entity is even eligible to have been tracked by workspaces in the first place.
6. Also how can an entity right now be deleted from something else than the current workspace - e.g. should this not make the workspace_id of the entity being deleted explicit?
Thanks - great patch overall and a great improvement -- just some little details that are IMHO worth discussing.
Comment #6
amateescu commentedThanks for the review, fixed all points from #4!
Comment #8
amateescu commentedThis should fix it :)
Comment #10
amateescu commentedThose fails are from some unrelated javascript tests, so I queued a re-test.
Comment #13
adriancidWithout patch:
Once the node is added and before delete
After delete
The node is still on the workspace association table.
With patch:
Once the node is added and before delete
After delete
The node is deleted from the workspace association table.

Comment #14
alexpottGiven that without this we have data integrity issues - don't we need an update path to fix existing data?
Comment #15
amateescu commented@alexpott, I'm not sure tbh, the data integrity gets fixed "automatically" over time when a workspace is published and all its records in the
workspace_associationtable are deleted. It's up to core committers to decide if we need an upgrade path (and tests?) or not, so putting back to RTBC.. pending for that decision.Comment #16
catchIf this is self-healing (as in workspace publishing and deletion removes the records anyway), then I think we should skip an upgrade path here altogether.
If it wasn't, I would probably suggest doing the upgrade path in a follow-up so that no new bad data is created asap anyway.
Comment #17
catchI think #14 is resolved, so I've gone ahead and committed/pushed to 10.1.x
Didn't backport to 10.0.x or 9.5.x due to the new API surface - given this is self-healing that doesn't seem urgent.
Comment #18
adriancid@catch I don't see any commit here and the issue's status is fixed.
Comment #19
catch@adriancid, it's there, Drupal.org posting back is/was broken: