Fix ConnectionFactory.build_url() for SQLite (issue #7)#191
Merged
Conversation
SQLite was going through the same {scheme}://{user}:{pwd}@{host}:{port}/{db}
template as postgres/mysql, producing an empty port segment
(sqlite+aiosqlite://:@localhost:/database.sqlite) that crashes
SQLAlchemy's URL._assert_port(). SQLite connections have no
host/user/password/port, so build the URL as {scheme}:///{db} instead,
which also correctly yields a four-slash URL for absolute paths.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ConnectionFactory.build_url()formatted every driver with the same{scheme}://{user}:{pwd}@{host}:{port}/{db}template. For SQLite,portis an empty string, producingsqlite+aiosqlite://:@localhost:/database.sqlite, which crashes SQLAlchemy'sURL._assert_port()withValueError: invalid literal for int() with base 10: ''.build_url()now special-casesdriver == "sqlite"and returnsf"{scheme}:///{db}"— this naturally producessqlite+aiosqlite:///relative.dbfor relative paths andsqlite+aiosqlite:////abs/path.dbfor absolute paths (the leading/in the path supplies the fourth slash).Fixes #7.
Test plan
fastapi_startkit/tests/masoniteorm/config/test_db_url.pycovering relative database path, absolute database path, and that host/user/password/port keys are ignored for sqliteuv run pytest fastapi_startkit/tests/masoniteorm/config/test_db_url.py -v— 7 passeduv run pytest --ignore=tests/masoniteorm/postgres -q(run fromfastapi_startkit/) — 1836 passed, 7 skippeduv run ruff check/uv run ruff format --checkon changed files — clean