Skip to content

fix: [#840] DB cache is not reset when fresh orm#1312

Merged
hwbrzzl merged 3 commits intov1.16.xfrom
bowen/#840
Dec 29, 2025
Merged

fix: [#840] DB cache is not reset when fresh orm#1312
hwbrzzl merged 3 commits intov1.16.xfrom
bowen/#840

Conversation

@hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Dec 24, 2025

📑 Description

Closes goravel/goravel#840

This pull request introduces an improvement to the database connection handling by adding a mechanism to reset GORM connections, and integrates this reset into the ORM's Fresh method. The changes also include a minor import update.

Database connection management improvements:

  • Added a new ResetConnections function to database/driver/gorm.go that safely clears the internal map of GORM connections, allowing for a clean reset of database connections.
  • Updated the Fresh method in Orm (database/orm/orm.go) to call driver.ResetConnections(), ensuring that all GORM connections are reset when refreshing the ORM state.

Codebase maintenance:

  • Added an import for github.com/goravel/framework/database/driver in database/orm/orm.go to support the new connection reset functionality.

✅ Checks

  • Added test cases for my code

@codecov
Copy link

codecov bot commented Dec 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (v1.16.x@e04ff6e). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             v1.16.x    #1312   +/-   ##
==========================================
  Coverage           ?   67.99%           
==========================================
  Files              ?      216           
  Lines              ?    11609           
  Branches           ?        0           
==========================================
  Hits               ?     7894           
  Misses             ?     3335           
  Partials           ?      380           

☔ 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.

// TODO: The fresh logic needs to be optimized, it's a bit unclear now.
func (r *Orm) Fresh() {
r.fresh(binding.Orm)
driver.ResetConnections()
Copy link
Contributor

Choose a reason for hiding this comment

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

It only works with facades.Orm().Fresh(), but calling facades.App().Fresh() only still results in a cache hit

https://github.com/goravel/framework/blob/master/foundation/container.go#L73

Copy link
Contributor Author

Choose a reason for hiding this comment

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

facades.Orm().Fresh() is called in the Ready function, so it works for your case in goravel/example. And as the annotation said, the fresh flow should be optimized.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, thanks

@hwbrzzl hwbrzzl marked this pull request as ready for review December 29, 2025 02:33
@hwbrzzl hwbrzzl requested a review from a team as a code owner December 29, 2025 02:33
Copilot AI review requested due to automatic review settings December 29, 2025 02:33
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: e6961b2 Previous: 06202a1 Ratio
Benchmark_Debug 125562 ns/op 34328 B/op 513 allocs/op 81915 ns/op 23407 B/op 384 allocs/op 1.53
Benchmark_Debug - ns/op 125562 ns/op 81915 ns/op 1.53
Benchmark_Info 124719 ns/op 34163 B/op 511 allocs/op 82307 ns/op 23358 B/op 384 allocs/op 1.52
Benchmark_Info - ns/op 124719 ns/op 82307 ns/op 1.52
Benchmark_Warning 131584 ns/op 35186 B/op 527 allocs/op 83765 ns/op 23403 B/op 384 allocs/op 1.57
Benchmark_Warning - ns/op 131584 ns/op 83765 ns/op 1.57
Benchmark_Warning - B/op 35186 B/op 23403 B/op 1.50
Benchmark_Panic 174330 ns/op 52525 B/op 708 allocs/op 114612 ns/op 36604 B/op 546 allocs/op 1.52
Benchmark_Panic - ns/op 174330 ns/op 114612 ns/op 1.52

This comment was automatically generated by workflow using github-action-benchmark.

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 pull request addresses issue #840 by implementing a mechanism to reset database connections when the ORM is refreshed. The changes convert the connection cache from a regular map to a sync.Map for better concurrency handling and introduce a new ResetConnections() function that is called during ORM refresh operations.

Key Changes:

  • Converted connectionToDB from map[string]*gorm.DB to sync.Map for thread-safe operations
  • Added ResetConnections() function to clear the database connection cache
  • Integrated connection reset into the Orm.Fresh() method

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
database/driver/gorm.go Converted connection cache to sync.Map and added ResetConnections function to clear cached connections
database/orm/orm.go Added driver import and called ResetConnections in Fresh method to ensure connections are reset during ORM refresh

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hwbrzzl hwbrzzl merged commit 31b03e9 into v1.16.x Dec 29, 2025
16 of 17 checks passed
@hwbrzzl hwbrzzl deleted the bowen/#840 branch December 29, 2025 06:45
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.

3 participants