Repository navigation
RHINENG-28312: simplify API db context handling - #2375
MichaelMraka wants to merge 4 commits into
Conversation
Reviewer's GuideSimplifies API database context handling by resolving GORM handles directly from the current request context instead of storing them in Gin middleware, while preserving read-replica routing, directing writes to the primary database, migrating all affected handlers, and adding enforcement tests. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="manager/middlewares/db_test.go" line_range="47" />
<code_context>
+ assert.False(t, useReadReplica(ginWithMethod(http.MethodGet)))
+
+ utils.CoreCfg.DBReadReplicaEnabled = true
+ assert.True(t, useReadReplica(ginWithMethod(http.MethodGet)))
+ assert.False(t, useReadReplica(ginWithMethod(http.MethodPost)))
+ assert.False(t, useReadReplica(ginWithMethod(http.MethodPut)))
</code_context>
<issue_to_address>
**Replica selection goes untested**
When `DB(c)` is changed to always return the primary handle while `useReadReplica` still returns true for GET requests, `TestUseReadReplica` asserts only that `useReadReplica` returns true; the database-handle tests force replica use off. If `DB(c)` always returns the primary handle, these assertions still pass and replica reads silently go to the primary.
With replica use enabled, assert that `DB(c)` returns the configured replica's pool.
</issue_to_address>
### Comment 2
<location path="base/core/probes.go" line_range="4" />
<code_context>
import (
- "app/base/database"
+ "app/manager/middlewares"
"net/http"
</code_context>
<issue_to_address>
**Core probes depend on manager middleware**
`base/core` now imports `manager/middlewares` to obtain a primary database handle, so shared probe code used by services such as the aggregator, evaluator, and listener also depends on the manager layer. This couples foundational health checks to API middleware and makes future manager-middleware dependencies leak into those services.
Move the request-scoped primary database accessor into a shared base/database layer and have the probes use it directly.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: manager/middlewares/db_test.go:47
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2375 +/- ##
==========================================
+ Coverage 60.14% 60.26% +0.12%
==========================================
Files 158 157 -1
Lines 10161 10158 -3
==========================================
+ Hits 6111 6122 +11
+ Misses 3424 3410 -14
Partials 626 626
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The tests lock replica selection, request context, and a ban on handlers calling database.DB directly.
… context. Handlers no longer depend on a Gin key captured in middleware.
…in handlers. Primary-only routes call DBWriter so status, probes, and admin SQL stay off the read replica.
Secure Coding Practices Checklist GitHub Link
Secure Coding Checklist
Summary by Sourcery
Centralize request-aware database connection handling across API and administrative handlers.
Enhancements:
Tests:
Chores: