From 35ea49d6d2cb8f0c9c5df4634549a82de2f3c81e Mon Sep 17 00:00:00 2001 From: Ami Date: Tue, 21 Apr 2026 14:54:18 +0700 Subject: [PATCH] fix: strip sslmode from database URL (asyncpg doesn't support it) --- apps/api/src/config/settings.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/apps/api/src/config/settings.py b/apps/api/src/config/settings.py index 00b8311..2111ab5 100644 --- a/apps/api/src/config/settings.py +++ b/apps/api/src/config/settings.py @@ -126,15 +126,29 @@ class Settings(BaseSettings): ``postgresql://`` and we need the ``+asyncpg`` driver suffix for the async engine. Normalise both cases here so the rest of the codebase can always assume ``postgresql+asyncpg://``. + + Also strips ``sslmode`` from query strings — asyncpg does not + accept this psycopg2 parameter and would raise TypeError. """ - if self.database_url.startswith("postgres://"): - self.database_url = self.database_url.replace( - "postgres://", "postgresql+asyncpg://", 1, - ) - elif self.database_url.startswith("postgresql://"): - self.database_url = self.database_url.replace( - "postgresql://", "postgresql+asyncpg://", 1, - ) + url = self.database_url + + # Fix dialect scheme + if url.startswith("postgres://"): + url = url.replace("postgres://", "postgresql+asyncpg://", 1) + elif url.startswith("postgresql://"): + url = url.replace("postgresql://", "postgresql+asyncpg://", 1) + + # Strip sslmode from query string (asyncpg doesn't support it) + if "?sslmode=" in url or "&sslmode=" in url: + from urllib.parse import urlparse, urlencode, parse_qs + + parsed = urlparse(url) + params = parse_qs(parsed.query, keep_blank_values=True) + params.pop("sslmode", None) + query = urlencode(params, doseq=True) + url = parsed._replace(query=query).geturl() + + self.database_url = url return self @model_validator(mode="after")