Skip to content

fix: Orm WithContext function#978

Merged
hwbrzzl merged 2 commits intomasterfrom
bowen/fix-orm-with-context
Mar 29, 2025
Merged

fix: Orm WithContext function#978
hwbrzzl merged 2 commits intomasterfrom
bowen/fix-orm-with-context

Conversation

@hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Mar 29, 2025

📑 Description

The Gorm instance should not be modified directly, we need to call the WithContext function to get a new Gorm instance to avoid concurrence.

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings March 29, 2025 10:16
@hwbrzzl hwbrzzl requested a review from a team as a code owner March 29, 2025 10:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures that a new Gorm instance is created when applying a context, rather than modifying the original instance directly. Key changes include:

  • Updating mocks to reflect a return value for the context function.
  • Introducing a new QueryWithContext mock that implements the WithContext method.
  • Replacing the SetContext method with the WithContext method in the production code and contracts.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
mocks/database/orm/QueryWithSetContext.go Updated SetContext to return orm.Query; may need renaming for consistency
mocks/database/orm/QueryWithContext.go Added a new mock for WithContext to support the new interface
database/orm/orm.go Modified Query() to use the WithContext method based on the context
database/gorm/query.go Replaced SetContext with WithContext creating a new Query instance
contracts/database/orm/orm.go Updated the interface from QueryWithSetContext to QueryWithContext
Comments suppressed due to low confidence (2)

mocks/database/orm/QueryWithSetContext.go:26

  • Consider renaming the 'SetContext' method to 'WithContext' to match the updated interface in contracts (QueryWithContext) or remove this outdated mock if it is no longer used.
func (_m *QueryWithSetContext) SetContext(ctx context.Context) orm.Query {

database/orm/orm.go:128

  • Directly comparing r.ctx to context.Background() may not reliably determine if a custom context is set; consider a more robust check or clear documentation on why this comparison is sufficient.
if r.ctx != context.Background() {

@codecov
Copy link

codecov bot commented Mar 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.10%. Comparing base (ed5742b) to head (89e732c).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #978   +/-   ##
=======================================
  Coverage   69.10%   69.10%           
=======================================
  Files         168      168           
  Lines       11340    11340           
=======================================
  Hits         7836     7836           
  Misses       3152     3152           
  Partials      352      352           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hwbrzzl hwbrzzl merged commit e95a34e into master Mar 29, 2025
13 checks passed
@hwbrzzl hwbrzzl deleted the bowen/fix-orm-with-context branch March 29, 2025 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants