Feature Description
Enhance SqlAlchemyConfig to automatically generate and store database connection URLs from individual connection parameters.
Problem Statement
Currently, developers must manually construct database connection URLs throughout the codebase using individual parameters:
db_url = f"postgresql://{sqlalchemy_config.USERNAME}:{sqlalchemy_config.PASSWORD}@{sqlalchemy_config.HOST}:{sqlalchemy_config.PORT}/{sqlalchemy_config.DATABASE}"
This leads to code duplication, potential inconsistencies, and requires extra boilerplate code whenever database connections are needed.
Proposed Solution
Add a DB_URL field to SqlAlchemyConfig that is automatically populated during model initialization if all required connection parameters are present. This approach uses Pydantic's model_validator to ensure the URL is correctly generated whenever the model is instantiated.
Use Cases
- When creating database connections in various parts of the application
- When initializing ORM adapters that require connection strings
- When spinning up database-dependent services in testing environments
- When configuring database migrations and schema management tools
Implementation Ideas
The implementation adds:
- A new
DB_URL field that will store the connection string
- A Pydantic
model_validator that automatically builds the connection URL if:
DB_URL is not explicitly provided
- All required connection parameters (username, host, port, database) are present
@model_validator(mode='after')
def build_connection_url(self) -> "SqlAlchemyConfig":
"""Build and populate DB_URL if not provided but all required connection parameters are present."""
if self.DB_URL is not None:
return self
if all([self.USERNAME, self.HOST, self.PORT, self.DATABASE]):
password_part = f":{self.PASSWORD}" if self.PASSWORD else ""
self.DB_URL = f"{self.DRIVER_NAME}://{self.USERNAME}{password_part}@{self.HOST}:{self.PORT}/{self.DATABASE}"
return self
Benefits
- Reduces code duplication
- Ensures consistent URL format across the application
- Simplifies code that needs database connections
- Leverages Pydantic's validation system rather than custom properties
- Maintains backward compatibility
Would you be willing to help implement this feature?
Feature Description
Enhance SqlAlchemyConfig to automatically generate and store database connection URLs from individual connection parameters.
Problem Statement
Currently, developers must manually construct database connection URLs throughout the codebase using individual parameters:
This leads to code duplication, potential inconsistencies, and requires extra boilerplate code whenever database connections are needed.
Proposed Solution
Add a
DB_URLfield to SqlAlchemyConfig that is automatically populated during model initialization if all required connection parameters are present. This approach uses Pydantic'smodel_validatorto ensure the URL is correctly generated whenever the model is instantiated.Use Cases
Implementation Ideas
The implementation adds:
DB_URLfield that will store the connection stringmodel_validatorthat automatically builds the connection URL if:DB_URLis not explicitly providedBenefits
Would you be willing to help implement this feature?