Skip to content

Add auto-generated database URL with validation to SqlAlchemyCo…#29

Merged
HosseinNejatiJavaremi merged 2 commits intoSyntaxArc:masterfrom
Mohammadreza-kh94:feature/sqlalchemy-config-connection-url
Apr 15, 2025
Merged

Add auto-generated database URL with validation to SqlAlchemyCo…#29
HosseinNejatiJavaremi merged 2 commits intoSyntaxArc:masterfrom
Mohammadreza-kh94:feature/sqlalchemy-config-connection-url

Conversation

@Mohammadreza-kh94
Copy link
Copy Markdown
Contributor

@Mohammadreza-kh94 Mohammadreza-kh94 commented Apr 14, 2025

Description

This PR adds automatic generation of database URLs with proper validation to the SQLAlchemy configuration. It implements a custom PostgresDsn class that extends Pydantic's AnyUrl, providing validation of PostgreSQL connection strings with support for various PostgreSQL-specific drivers.

The implementation includes two main features:

  1. Auto-generation of connection URLs when component parameters (username, password, host, port, database) are provided
  2. Extraction of component parts from a provided URL to populate missing fields

This makes the configuration more flexible and robust, allowing users to specify either individual components or a complete connection string.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

  • Tested URL generation with all components provided
  • Tested URL validation with valid PostgreSQL schemes
  • Tested URL validation with invalid schemes
  • Tested component extraction from valid URLs
  • Tested with and without password in the connection string

Sample code for testing:

# Test with individual components
db_config = SqlAlchemyConfig(
    USERNAME="postgres", 
    PASSWORD="password123", 
    HOST="localhost", 
    PORT=5432, 
    DATABASE="mydb"
)
assert db_config.DB_URL is not None

# Test with connection string
db_config = SqlAlchemyConfig(
    DB_URL="postgresql+psycopg://user:pass@dbhost:5432/otherdb"
)
assert db_config.USERNAME == "user"
assert db_config.PASSWORD == "pass"
assert db_config.HOST == "dbhost"
assert db_config.PORT == 5432
assert db_config.DATABASE == "otherdb"

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my feature works
  • New and existing unit tests pass locally with my changes

Additional context

This implementation follows Python 3.13 conventions by using Union syntax (|) and proper type hints throughout.
Resolves #28

@HosseinNejatiJavaremi HosseinNejatiJavaremi merged commit 405183b into SyntaxArc:master Apr 15, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Database Connection URL Property to SqlAlchemyConfig

2 participants