-
Notifications
You must be signed in to change notification settings - Fork 0
Fix knex reinitialization after destroy #47
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,7 +106,7 @@ const getHealthStatus = (knex = null) => { | |
| * @returns {Promise<Object>} Maintenance result | ||
| */ | ||
| const performMaintenance = async (knex = null) => { | ||
| const kInstance = knex || knexInstance || global.knex; | ||
| let kInstance = knex || knexInstance || global.knex; | ||
|
|
||
| if (!kInstance) { | ||
| console.error('ERROR: No knex instance available for maintenance'); | ||
|
|
@@ -124,9 +124,16 @@ const performMaintenance = async (knex = null) => { | |
| if (healthCheckStatus.consecutiveFailures >= 3) { | ||
| console.log('Performing database pool reset due to consecutive failures'); | ||
|
|
||
| // Destroy and recreate the pool | ||
| // Destroy existing instance and create a fresh one using the same config | ||
| const config = kInstance.client && kInstance.client.config ? kInstance.client.config : null; | ||
| await kInstance.destroy(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Potential missing reinitialization when config is null If |
||
| await kInstance.initialize(); | ||
|
|
||
| if (config) { | ||
| const newKnex = require('knex')(config); | ||
| kInstance = newKnex; | ||
| knexInstance = newKnex; | ||
| global.knex = newKnex; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Direct manipulation of the global state within Suggested Change: |
||
| } | ||
|
|
||
| // Reset health check status | ||
| healthCheckStatus.consecutiveFailures = 0; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling in
performMaintenancefunction could be improved by throwing an exception instead of just logging and returning an error object. This change would allow the calling function to handle the error more dynamically, which is particularly useful in asynchronous operations where the state and flow of execution can be more complex to manage.Suggested Change: