Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-01-05 13:56:25 +07:00
parent a86b0be26e
commit 8c38357c28
2 changed files with 68 additions and 45 deletions
+7 -1
View File
@@ -1,5 +1,6 @@
import os
from typing import Optional
from urllib.parse import quote_plus
from dotenv import load_dotenv
from sqlalchemy.ext.asyncio import (
@@ -44,7 +45,12 @@ class PostgresConnection:
user = os.getenv("POSTGRES_USER", "postgres")
password = os.getenv("POSTGRES_PASSWORD", "")
database = os.getenv("POSTGRES_DB", "postgres")
auth = f"{user}:{password}@" if password else f"{user}@"
# Encode password để handle ký tự đặc biệt như @, /, etc.
if password:
auth = f"{user}:{quote_plus(password)}@"
else:
auth = f"{user}@"
raw_url = f"postgresql+asyncpg://{auth}{host}:{port}/{database}"
url = _normalize_asyncpg_url(raw_url)