Skip to content

Commit 59a9c64

Browse files
authored
fix(orm): build correct SQLite URLs in ConnectionFactory.build_url() (#191)
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.
1 parent a04ee60 commit 59a9c64

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

fastapi_startkit/src/fastapi_startkit/masoniteorm/connections/factory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ def build_url(cls, config: dict) -> str:
2626

2727
driver = config["driver"]
2828
scheme = cls.DRIVER_URLS[driver]
29+
db = config.get("database", "")
30+
31+
if driver == "sqlite":
32+
return f"{scheme}:///{db}"
33+
2934
user = config.get("username", "")
3035
pwd = config.get("password", "")
3136
host = config.get("host", "localhost")
3237
port = config.get("port", "")
33-
db = config.get("database", "")
3438
return f"{scheme}://{user}:{pwd}@{host}:{port}/{db}"
3539

3640
@classmethod

fastapi_startkit/tests/masoniteorm/config/test_db_url.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ def test_postgres_config(self):
3030
url = ConnectionFactory.build_url(config)
3131
self.assertEqual(url, "postgresql+asyncpg://user:pass@db.example.com:5432/mydb")
3232

33+
def test_sqlite_config_relative_database(self):
34+
config = {
35+
"driver": "sqlite",
36+
"database": "database.sqlite",
37+
}
38+
url = ConnectionFactory.build_url(config)
39+
self.assertEqual(url, "sqlite+aiosqlite:///database.sqlite")
40+
41+
def test_sqlite_config_absolute_database(self):
42+
config = {
43+
"driver": "sqlite",
44+
"database": "/var/data/database.sqlite",
45+
}
46+
url = ConnectionFactory.build_url(config)
47+
self.assertEqual(url, "sqlite+aiosqlite:////var/data/database.sqlite")
48+
49+
def test_sqlite_config_ignores_host_user_password_port(self):
50+
"""SQLite has no host/user/password/port -- extra keys must not leak into the URL."""
51+
config = {
52+
"driver": "sqlite",
53+
"database": "database.sqlite",
54+
"host": "localhost",
55+
"username": "root",
56+
"password": "secret",
57+
"port": "",
58+
}
59+
url = ConnectionFactory.build_url(config)
60+
self.assertEqual(url, "sqlite+aiosqlite:///database.sqlite")
61+
3362
def test_sqlite_config_via_url_passthrough(self):
3463
config = {
3564
"driver": "sqlite",

0 commit comments

Comments
 (0)