Skip to content

Fix knex reinitialization after destroy#47

Open
numbpill3d wants to merge 1 commit intomainfrom
codex/recreate-knex-instance-after-destroy
Open

Fix knex reinitialization after destroy#47
numbpill3d wants to merge 1 commit intomainfrom
codex/recreate-knex-instance-after-destroy

Conversation

@numbpill3d
Copy link
Copy Markdown
Collaborator

@numbpill3d numbpill3d commented Jun 8, 2025

Summary

  • allow performMaintenance to update the knex instance
  • when the pool is reset, create a new knex instance and reassign it to knexInstance and global.knex

Testing

  • npm test (fails: Missing required Supabase environment variables)
  • npm run lint

https://chatgpt.com/codex/tasks/task_e_68450e60cbb0832fa86a638e2818e937

Summary by Sourcery

Reinitialize the Knex instance during database pool maintenance to avoid stale connections

Bug Fixes:

  • Recreate and reassign the Knex instance to knexInstance and global.knex after destroying the pool in performMaintenance

Enhancements:

  • Allow performMaintenance to update the active Knex instance reference

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Jun 8, 2025

Reviewer's Guide

This PR updates the performMaintenance workflow to properly destroy and recreate the Knex pool by capturing its existing configuration, instantiating a fresh Knex instance, and reassigning both local and global references.

Sequence Diagram: Knex Instance Replacement in performMaintenance

sequenceDiagram
    participant Func as "performMaintenance()"
    participant OldKnex as "Existing knexInstance (to be replaced)"
    participant KnexFactory as "require('knex')"
    participant ModuleVar as "module.knexInstance"
    participant GlobalVar as "global.knex"

    Func->>OldKnex: Get client.config
    activate OldKnex
    OldKnex-->>Func: config
    deactivate OldKnex

    Func->>OldKnex: destroy()
    activate OldKnex
    OldKnex-->>Func: (destroyed)
    deactivate OldKnex

    alt If config was retrieved
        Func->>KnexFactory: Create new Knex(config)
        activate KnexFactory
        KnexFactory-->>Func: newKnexInstance
        deactivate KnexFactory

        Func->>ModuleVar: Update to newKnexInstance
        Func->>GlobalVar: Update to newKnexInstance
    end
Loading

File-Level Changes

Change Details Files
Enable proper Knex reinitialization on pool reset
  • Changed kInstance declaration from const to let to allow reassignment
  • Captured the existing Knex configuration before destroying the instance
  • Destroyed the old Knex instance and created a new one with the same config
  • Reassigned the new instance to both knexInstance and global.knex
server/utils/db-health.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

let kInstance = knex || knexInstance || global.knex;

if (!kInstance) {
console.error('ERROR: No knex instance available for maintenance');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The error handling in performMaintenance function 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:

if (!kInstance) {
  throw new Error('No knex instance available for maintenance');
}

const newKnex = require('knex')(config);
kInstance = newKnex;
knexInstance = newKnex;
global.knex = newKnex;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Direct manipulation of the global state within performMaintenance function can lead to hard-to-track bugs and makes the code less modular and more difficult to test. Consider using dependency injection for providing the knex instance throughout your application. This approach would make the code more modular, easier to test, and reduce side effects that can arise from altering global state.

Suggested Change:
Remove the line global.knex = newKnex; and ensure that knex instances are passed explicitly where needed.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @numbpill3d - I've reviewed your changes - here's some feedback:

  • Deep clone the original knex configuration instead of reusing client.config directly to ensure all settings (including pool options) are preserved when recreating the instance.
  • Add explicit handling for when kInstance.client.config is null to avoid permanently losing the knex instance after destroy (e.g., throw an error or fallback to the previous instance).
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

// 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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Potential missing reinitialization when config is null

If config is null, destroying the pool leaves knex unusable. Consider throwing an error or adding a fallback initialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant