Problem/Motivation
When you have more than one usage for a file, in the list of usages of this file (the page_2 of views.view.files.yml), each usage is listed the same amount of times as results are. As the view is doing a join between file_managed and file_usage and then again another join with file_managed with the result.
Steps to reproduce
Here is some code to reproduce the issue (if you use the same file in 2 nodes, you will have 4 rows on the file usage page; if you use the same file in 3 nodes, you will have 9 rows on the file usage page; if you use the same file in 4 nodes, you will have 16 rows on the file usage page, etc.):
$file = \Drupal\file\Entity\File::create([
'uri' => 'public://test.png',
]);
$file->save();
$node = \Drupal\node\Entity\Node::create([
'type' => 'article',
'title' => 'Test 1',
'field_image' => ['target_id' => $file->id()],
]);
$node->save();
$node = \Drupal\node\Entity\Node::create([
'type' => 'article',
'title' => 'Test 2',
'field_image' => ['target_id' => $file->id()],
]);
$node->save();
drupal_flush_all_caches();
Proposed resolution
This is due to a double join between file_managed and file_usage, remove the duplicated join.
We should remove the following duplicate join of file_managed
from the FileViewsData.php
$data['file_usage']['table']['join'] = [
'file_managed' => [
'field' => 'fid',
'left_field' => 'fid',
],
Remaining tasks
- Needs review
User interface changes
N/A
API changes
TBD
Data model changes
N/A
Release notes snippet
N/A
Comment | File | Size | Author |
---|---|---|---|
#80 | 2632372-80.patch | 16.92 KB | mohit_aghera |
#75 | interdiff-2632372-73-75.txt | 6.33 KB | mohit_aghera |
#75 | 2632372-75.patch | 16.39 KB | mohit_aghera |
#67 | 2632372-67.patch | 3.56 KB | mohit_aghera |
#59 | 2632372-59.patch | 2.27 KB | ameymudras |
|
Comments
Comment #2
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedDeleted the unneeded join.
Comment #4
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedFixed the test fails.
Comment #5
no_angel CreditAttribution: no_angel as a volunteer commentedCan you please provide the steps to reproduce the issue?
Also should this be tested on the latest version of D8? 8.0.1
Comment #6
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedI found it working on this issue in monitoring module #2614000: Add aggregated table verbose output of image derivative creation sensor, I basicaly added the file usages programaticaly, and when I checked the file usage list I foud that the results where repeated as many times as results we had.
I checked the sql query generated by the view and I could see that was doing a join between file_managed and file_usage and then again another join with file_managed with the result. So to fix it I just removed the las join.
I'm working always with the 8.0.x-dev branch of drupal so it happens in the 8.0.1
So to reproduce just add more than one file_usage for one file and check the view with the list of usages.
Comment #7
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedComment #8
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedSo here is the patch that adds aggregation, for 8.0.x
Comment #9
no_angel CreditAttribution: no_angel as a volunteer commentedSteps to reproduce the issue reported:
:
After patch steps :
Comment #10
no_angel CreditAttribution: no_angel as a volunteer commentedComment #11
no_angel CreditAttribution: no_angel as a volunteer commentedChanged status to RTBC
Comment #12
no_angel CreditAttribution: no_angel as a volunteer commentedComment #13
alexpottI think we should improve
Drupal\file\Tests\FileListingTest
to test for this.Also I think we need to consider an upgrade path here.
Comment #14
no_angel CreditAttribution: no_angel as a volunteer commentedI'd like to give #13 ago if there is some time for researching how.
Comment #15
no_angel CreditAttribution: no_angel as a volunteer commentedToo big for me.
Issue summary updated.
Comment #16
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedAdded the upgrade path, finally I learned how to do it :)
Added tests.
I added 8_0_x and 8_1_x to the file names showing the branch they are for.
Comment #17
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedSorry, wrong naming in the interdiff
Comment #24
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedFixed the test fails in 8.0.x
Comment #26
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedI think we can still commit the patch for drupal 8.1 to 8.1 and 8.2.
Comment #27
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedChanged to 8.2.x
Comment #28
edurenye CreditAttribution: edurenye at MD Systems GmbH commentedComment #29
BerdirLets make this a bit more defensive. Check if the view exists, check if the setting you want to check exists and is actually set to FALSE right now.
If not, do not change/save the view.
This is default configuration, users might have deleted or changed it.
Comment #30
edurenye CreditAttribution: edurenye commentedRight, changed.
Comment #32
jcnventura CreditAttribution: jcnventura at Wunder commentedThis is neither a "new development" nor a "disruptive change".. We should not wait for 8.3.x to fix this.
Maybe it's just lack of information on my part, but looking at http://cgit.drupalcode.org/drupal/tree/core/modules/file/config/optional..., I still see that 8.2.x has that group_by still set to false. If we change it to true on the 8.1.x patch, shouldn't we do the same in the 8.2.x patch? And also add the file_update_8001() function? There will be people upgrading 8.1.x sites to 8.2.x sites, after all.
Comment #33
edurenye CreditAttribution: edurenye commentedNo, with the patch for 8.2.x we use the relationship and it uses less resources than with the group_by, while in the 8.1.x we use the group_by as we though that the other change was to big for it.
Comment #34
jcnventura CreditAttribution: jcnventura at Wunder commentedOK. Let's forget about the 8.1 patch now.. It will soon be irrelevant anyway.
However, reading the 8.2 patch, the only non-test change is to remove a join with file_managed. I don't see how that relates to your answer of "we use the relationship".
Comment #35
edurenye CreditAttribution: edurenye as a volunteer commentedIndeed, my answer was wrong, you are right.
I solved this issue long time ago and I replied too fast, sorry.
With the RC we can still put it in? Or we have to do as before, one for 8.3 and one for 8.2?
Comment #36
jcnventura CreditAttribution: jcnventura at Wunder commentedAt this point, the patch should be applied to 8.3.x, and then patched to 8.2.x.
Let's see if the testbots are ok with it.
Comment #37
jcnventura CreditAttribution: jcnventura at Wunder commentedTestbots are OK, and so is my test on a real system..
This is definitively RTBC in 8.3.x and 8.2.x (same patch applies in both at this time).
Comment #38
catchWhat happens if there are views out there relying on the automatic join?
Comment #39
jcnventura CreditAttribution: jcnventura at Wunder commentedIndeed. It may be safer, and way more appropriate to use the solution in the 8.1.x patch.
Comment #41
jcnventura CreditAttribution: jcnventura at Wunder commentedRe-rolled the 8.1 patch in #30 for Drupal 8.4. This uses only the aggregation and then runs file_update_8001 to update the config for existing views.
No interdiff, as it is identical to #30.
Comment #42
edurenye CreditAttribution: edurenye as a volunteer commentedThe real good solution was the other patch, I'm retesting that one to see if needs re-roll, your patch must focus on 8.3 not 8.4, but maybe seeing the time that this issue is been open we might just provide the good solution for 8.4, what do you think?
Comment #45
edurenye CreditAttribution: edurenye as a volunteer commentedI did rebase for 8.1.x -> 8.5.x and 8.2.x -> 8.6.x.
Comment #58
quietone CreditAttribution: quietone at PreviousNext commentedI tested on Drupal 10.1.x, standard install and this is still relevant.
have closed #2560937: Multiple file usage records as a duplicate and adding credit.
Comment #59
ameymudras CreditAttribution: ameymudras at Salsa Digital commentedStill needs to write tests
Comment #60
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis could use steps to reproduce. Not sure I'm seeing an issue.
Also as mentioned needs tests.
Comment #61
mfbA while back I added reproduce code to the duplicate issue at #2560937: Multiple file usage records, now copying it to this issue
Comment #62
smustgrave CreditAttribution: smustgrave at Mobomo commentedThanks @mfb confirmed I see the issue now.
Comment #63
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedThere was a discussion around two approaches in the issue. One patch was for 8.1.x and another one was for 8.2.x
The one that was for 8.1.x #24 #30 #45 was updating a views configuration and adding aggregation.
I think in #42it was discussed that this is not the good approach. I quite agree with that.
It is good to remove un-necessary join rather than adding aggregation.
Now patches for 8.2,x, 8.6.x mentioned in #30and #45 comes to picture. I feel that this is the correct approach.
Prefereed solution:
Remove the additional join happening in hook_views_data of the FileViewsData.php class.
The test cases added in #45 seem a bit confusing, so I've removed that test case and added a new simple one to validate the error. Attached test-only patch as well. Since we are not updating any views this time, I think we won't need any update hook or upgrade path test.
I've updated issue summary accordingly focussing approach #2 which is about removing additional join in the hook_views_data.
#59 re-roll is missing a couple of changes mentioned in the #45
Reportedly FileUsageTest.php file's changes are not included in the re-roll.
Comment #64
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedComment #67
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedOpps, putting back the views related changes.
Didn't realised that those changes might break the view's functional tests.
Not uploading the test only patch since test-only patch is failing for the new test case only.
Comment #68
smustgrave CreditAttribution: smustgrave at Mobomo commentedConfirmed the issue using the snippet in the issue summary and the patch in #67 solves the issue.
#63 shows the tests fail as expected.
Comment #71
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedI think previous failures were un-related.
Triggering run again for 11.x
Let's see how goes 🤞
Comment #73
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedTried to fix the issue.
To fix the issue, I had to update the view.
Which leads me to think that can there be any regression on the existing views because of the change?
Do we need to consider any upgrade path here?
I had to add this whole relationship.
Which makes me think do we need to consider any upgrade path here?
Looking forward to see opinions from other folks.
Comment #75
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedUpdating another view which was causing issues in relationship.
Hiding the patch in #73.
Comment #76
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis was previously RTBC and since the update needed was for a test view think it's safe to go back to RTBC!
Comment #77
jcnventura CreditAttribution: jcnventura commentedRe: #73, an upgrade path should not be necessary as long as the views being changed are only used for testing. At the moment, there are no changes to any views config, so an upgrade path is not necessary.
Comment #78
larowlanI manually tested this, and it worked as designed, without any updates to existing config.
It did however require a cache clear to work.
Should we add an empty post update hook to trigger the cache invalidation that resets the views config?
Or should we leave it as is knowing that the missing cache clear doesn't break anything, its just the bug doesn't get fixed until it is cleared
Comment #79
smustgrave CreditAttribution: smustgrave at Mobomo commentedIf the standard is to do an empty post_update hook think it wouldn't be terrible to add here.
Comment #80
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedAdded an empty post_update hook.
Comment #82
mfbThere is a literally random failure on this patch, but it seems to work. I just don't understand the "why": Removing the built-in relationship between file_usage and file_managed (which seems like a useful thing to define) only to add this relationship back in the view itself?
If it really does make sense to remove this from the views data, then the above comment should be updated too:
Removing this from views data seems problematic if every custom view that relied on it needs to be updated, so it presumably would need a change record?
By the way, I noticed that media was never added here as one of the core entity type base tables..