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.

Comments

amateescu created an issue. See original summary.

amateescu’s picture

Status: Active » Needs review
StatusFileSize
new2.91 KB
new6.85 KB

This should do it.

The last submitted patch, 2: 3216107-test-only.patch, failed testing. View results

fabianx’s picture

  1. +++ b/core/modules/workspaces/src/WorkspaceAssociation.php
    @@ -212,9 +212,16 @@ public function postPublish(WorkspaceInterface $workspace) {
    +  public function deleteAssociations($workspace_id = NULL, $entity_type_id = NULL, $entity_ids = NULL, $revision_ids = NULL) {
    

    That'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?

  2. +++ b/core/modules/workspaces/workspaces.module
    @@ -143,6 +143,26 @@ function workspaces_entity_predelete(EntityInterface $entity) {
    +  if ($entity->getEntityTypeId() !== 'workspace') {
    +    \Drupal::service('workspaces.association')
    +      ->deleteAssociations(NULL, $entity->getEntityTypeId(), [$entity->id()]);
    +  }
    

    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.

  3. +++ b/core/modules/workspaces/workspaces.module
    @@ -143,6 +143,26 @@ function workspaces_entity_predelete(EntityInterface $entity) {
    +    \Drupal::service('workspaces.association')
    +      ->deleteAssociations(NULL, $entity->getEntityTypeId(), NULL, [$entity->getRevisionId()]);
    

    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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

amateescu’s picture

StatusFileSize
new7.03 KB
new1.97 KB

Thanks for the review, fixed all points from #4!

Status: Needs review » Needs work

The last submitted patch, 6: 3216107-6.patch, failed testing. View results

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new7.02 KB
new1.28 KB

This should fix it :)

Status: Needs review » Needs work

The last submitted patch, 8: 3216107-8.patch, failed testing. View results

amateescu’s picture

Status: Needs work » Needs review

Those fails are from some unrelated javascript tests, so I queued a re-test.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

adriancid’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new203.24 KB
new201.72 KB
new201.1 KB
new222.57 KB

Without 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.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Given that without this we have data integrity issues - don't we need an update path to fix existing data?

amateescu’s picture

Status: Needs work » Reviewed & tested by the community

@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_association table 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.

catch’s picture

If 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.

catch’s picture

Status: Reviewed & tested by the community » Fixed

I 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.

adriancid’s picture

@catch I don't see any commit here and the issue's status is fixed.

catch’s picture

@adriancid, it's there, Drupal.org posting back is/was broken:

git log --pretty=oneline | grep 3216107
b13dae8bd0d08a48af2aef2c7a55d35147da11af Issue #3216107 by amateescu, adriancid, alexpott, Fabianx: Workspace association data is not updated when an entity is deleted

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.