Skip to content

Optimized RemoveFromToManyRelationship endpoint #1039

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
merged 3 commits into from
Aug 13, 2021

Conversation

bart-degreed
Copy link
Contributor

@bart-degreed bart-degreed commented Aug 12, 2021

Optimized DELETE Relationship endpoints (remove from to-many relationship)

Before we'd fetch the full left resource including attributes, along with the full relationship (all related resources with all of their attributes). This commit changes that to only fetch the left ID and the subset of right IDs to be removed, then makes the EF Core change tracker believe the full relationship was loaded, so it can perform change detection and generate SQL for the diff.

Replaced PlaceholderResourceCollector usage with resetting the change tracker on CreateResource/UpdateResource and failure in SaveChanges. This fixes broken JSON:API change tracking, where a side effect in database during save goes unnoticed. The re-fetch-after-save would execute the query, but did not refresh tracked entities. This is by design, see https://stackoverflow.com/questions/46205114/how-to-refresh-an-entity-framework-core-dbcontext. Resetting the full change tracker made this bug surface.

Breaking change: we don't call IResourceDefinition.OnPrepareWriteAsync anymore from RemoveFromToMany endpoint.

Fixes #1030.

QUALITY CHECKLIST

  • Changes implemented in code
  • Complies with our contributing guidelines
  • Adapted tests
  • N/A: Documentation updated
  • N/A: Created issue to update Templates: {ISSUE_NUMBER}

Sorry, something went wrong.

@codecov
Copy link

codecov bot commented Aug 12, 2021

Codecov Report

Merging #1039 (2c3f516) into master (d555bb5) will decrease coverage by 1.68%.
The diff coverage is 78.22%.

❗ Current head 2c3f516 differs from pull request most recent head cde3954. Consider uploading reports for the commit cde3954 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1039      +/-   ##
==========================================
- Coverage   91.63%   89.95%   -1.69%     
==========================================
  Files         252      257       +5     
  Lines        6777     7026     +249     
==========================================
+ Hits         6210     6320     +110     
- Misses        567      706     +139     
Impacted Files Coverage Δ
...otNetCoreExample/Definitions/TodoItemDefinition.cs 0.00% <0.00%> (ø)
.../JsonApiDotNetCoreExample/Startups/EmptyStartup.cs 0.00% <ø> (ø)
...EntityFrameworkExample/Services/WorkItemService.cs 85.00% <ø> (ø)
...ore/AtomicOperations/OperationProcessorAccessor.cs 95.00% <0.00%> (ø)
...DotNetCore/Controllers/JsonApiCommandController.cs 0.00% <0.00%> (ø)
...Core/Queries/Expressions/QueryExpressionVisitor.cs 4.34% <ø> (ø)
...tNetCore/Queries/Internal/Parsing/IncludeParser.cs 100.00% <ø> (ø)
...nternal/Parsing/QueryStringParameterScopeParser.cs 83.33% <ø> (ø)
.../Internal/QueryableBuilding/SelectClauseBuilder.cs 97.67% <ø> (ø)
...s/Internal/QueryableBuilding/WhereClauseBuilder.cs 94.00% <ø> (ø)
... and 127 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2f157a5...cde3954. Read the comment docs.

@bart-degreed bart-degreed force-pushed the optimized-remove-to-many branch 5 times, most recently from 5943ccb to 861dc80 Compare August 12, 2021 17:21
@bart-degreed bart-degreed marked this pull request as ready for review August 12, 2021 19:27
@bart-degreed bart-degreed requested a review from maurei August 12, 2021 19:27
@bart-degreed bart-degreed force-pushed the optimized-remove-to-many branch from 861dc80 to c529811 Compare August 13, 2021 08:56
Bart Koelman added 3 commits August 13, 2021 14:29
…ship)

Before we'd fetch the full left resource including attributes, along with the full relationship (all related resources with all of their attributes). This commit changes that to only fetch the left ID and the subset of right IDs to be removed, then makes the EF Core change tracker believe the full relationship was loaded, so it can perform change detection and generate SQL for the diff.

Replaced PlaceholderResourceCollector usage with resetting the change tracker on CreateResource/UpdateResource and failure in SaveChanges. This fixes broken JSON:API change tracking, where a side effect in database during save goes unnoticed. The refetch-after-save would execute the query, but did not refresh tracked entities. This is by design, see https://stackoverflow.com/questions/46205114/how-to-refresh-an-entity-framework-core-dbcontext. Resetting the full change tracker made this bug surface.
@bart-degreed bart-degreed force-pushed the optimized-remove-to-many branch from c529811 to cde3954 Compare August 13, 2021 12:32
@bart-degreed bart-degreed merged commit ea16aaf into master Aug 13, 2021
@bart-degreed bart-degreed deleted the optimized-remove-to-many branch August 13, 2021 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Explore: Optimize removing from a to-many relationship
2 participants