Skip to content

Refactor Connection Management: Implement CanyonContext Singleton for Improved Configuration and Testability #69

@TheRustifyer

Description

@TheRustifyer

Description:

The current implementation in core/connection/mod.rs relies on a lazy_static initialization alongside numerous functions, resulting in messy code that is hard to maintain and test. We need to streamline this by introducing a general singleton wrapper type (tentatively named Canyon or CanyonContext) that encapsulates the data and functionality currently scattered across the module.

This refactor will:

  • Consolidate initialization logic and connection management into a single, well-structured type.
  • Offer a fluent builder API for constructing a database connection via a datasource. This will empower end users to establish a connection even if a configuration file is absent.
  • Address the current configuration file issue where the absence of the file triggers a panic!(). Instead, CanyonContext should gracefully return an error, with the potential addition of a new DatabaseType variant (e.g., NoDatabase) to support a Result-based error workflow.
  • Improve testability by encapsulating state and exposing a clear, documented public API.

Tasks:

Design the CanyonContext Type:

  • Define a new singleton wrapper type that holds:
    • Connection data
    • Configuration settings
  • Ensure that the type is thread-safe and properly encapsulates all relevant state.

Migrate Lazy Initialization:

  • Refactor the existing lazy_static setup into the new CanyonContext, ensuring a clean and maintainable initialization process.

Implement a Fluent Builder API:

Develop a builder pattern for CanyonContext that allows:

  • Direct data input for runtime configuration.
  • Optional loading of configuration from external sources (e.g., .env files or configuration files).
  • Initialization of a database connection even if no configuration file is provided.

Improve Error Handling:

  • Remove panic!() calls related to missing configuration files.
  • Introduce proper error propagation (using Result) in the query executor branches.
  • Add a new variant (e.g., NoDatabase) to the DatabaseType enum to support error workflows when no database configuration is available.

Refactor Existing Functions:

  • Update functions within core/connection/mod.rs to utilize the new CanyonContext.
  • Ensure that the new architecture integrates seamlessly with the rest of the codebase.

Testing:

Write comprehensive unit tests for:

  • The initialization and lifecycle of CanyonContext.
  • The fluent builder API, ensuring it handles various configuration scenarios.
  • Correct error handling when configuration data is missing or invalid.

Expected Outcome:

A refactored connection module that:

  • Encapsulates connection and configuration management in the CanyonContext singleton, leading to cleaner and more maintainable code.
  • Provides a fluent and user-friendly API for constructing database connections.
  • Handles missing configuration gracefully by returning errors, making the module more robust and testable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions