fix: prevent Orm.Connection() cached path from poisoning dbConfig - #1540
Merged
Conversation
Fixes goravel issue #987. Cache each connection's database.Config alongside its Query so Orm.Connection(name) returns an Orm whose Config/DatabaseName/ Name match the requested connection, and Name() reports the connection it was constructed for. This stops consecutive migrations on a non-default connection from writing the second ledger row to the wrong database.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1540 +/- ##
==========================================
- Coverage 72.41% 72.37% -0.04%
==========================================
Files 409 409
Lines 26475 26489 +14
==========================================
Hits 19172 19172
- Misses 7301 7315 +14
Partials 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…-path races Wrap the shared connection cache in a QueriesCache guarded by an RWMutex and use double-checked locking in Connection(), so concurrent lazy builds of the same connection from different goroutines are serialized instead of racing on the maps. Observe() snapshots the cache under RLock before invoking user code. Adds TestOrmConnectionConcurrentSafe, which passes under -race and fails on the unlocked version.
This was referenced Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Orm.Connection()now resolves each connection's own database config from a shared, concurrency-safeQueriesCache, soName()/DatabaseName()and connection-scoped behavior (like where the migration ledger row is written) reflect the requested connection instead of the default's.Connection()calls that lazily build the same connection are now serialized by double-checked locking, eliminating the data race on the connection cache —TestOrmConnectionConcurrentSafepasses under-raceand fails on the unlocked version.database/orm/NewOrmconstructor now takes a*QueriesCachebuilt via the newNewQueriesCache(queries, dbConfigs)— breaking only for direct callers of the concrete constructor;contractsorm.Ormis unchanged, so no mock regeneration is needed.goravel/goravel#987
Why
Orm.Connection()had a cached fast path that ignored the requested connection and reused the receiver'sdbConfig. Once a non-default connection had been resolved, every laterConnection()call for it returned anOrmwhoseName()/DatabaseName()— and therefore derived behavior like where the migration ledger row gets written — still pointed at the default connection. Running two migrations on the same non-defaultConnection()wrote the second ledger row to the wrong database.Before the fix, the second migration's ledger row was written to the sqlite connection, where no
migrationstable exists, failing the run. After the fix, both migrations run on their declaredsqliteconnection while both ledger rows are recorded on the default connection, matching Laravel's behavior.The same fix hardens the connection cache for concurrent use. Lazy connection builds now flow through a shared
QueriesCacheguarded by anRWMutexwith double-checked locking, so concurrent cold-path calls from different goroutines no longer race on the shared maps;Observe()snapshots the cache under a read lock before invoking user code. As a cleanup, the ambiguous internal cache field was renamed frommtoconnections.Before the fix, these concurrent cold-path calls raced on the shared connection maps (a
DATA RACEunder-race). After the fix, double-checked locking serializes the build and every caller gets anOrmbound to the requested connection.