Fix DATABASE_URL corruption from special characters in POSTGRES_PASSWORD
This commit is contained in:
+22
-2
@@ -1,10 +1,30 @@
|
||||
import os
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import URL
|
||||
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
||||
|
||||
DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite:///./time_track.db")
|
||||
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
|
||||
|
||||
def build_database_url() -> URL | str:
|
||||
"""Prefer DATABASE_URL; otherwise build a safely escaped URL from parts.
|
||||
|
||||
Passwords are passed as URL components (not string interpolation), so
|
||||
characters like '@', ':', '/', '#' and '%' cannot corrupt the hostname.
|
||||
"""
|
||||
database_url = os.environ.get("DATABASE_URL")
|
||||
if database_url:
|
||||
return database_url
|
||||
return URL.create(
|
||||
"postgresql+psycopg",
|
||||
username=os.environ.get("POSTGRES_USER", "time_track"),
|
||||
password=os.environ.get("POSTGRES_PASSWORD", ""),
|
||||
host=os.environ.get("POSTGRES_HOST", "localhost"),
|
||||
port=int(os.environ.get("POSTGRES_PORT", "5432")),
|
||||
database=os.environ.get("POSTGRES_DB", "time_track"),
|
||||
)
|
||||
|
||||
|
||||
engine = create_engine(build_database_url(), pool_pre_ping=True)
|
||||
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
|
||||
|
||||
|
||||
|
||||
+15
-1
@@ -1,6 +1,8 @@
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import shutil
|
||||
import time
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
@@ -12,6 +14,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from pypdf import PdfReader
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import OperationalError
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
|
||||
@@ -34,7 +37,18 @@ templates = Jinja2Templates(directory=str(Path(__file__).parent / "templates"))
|
||||
def startup() -> None:
|
||||
if not ADMIN_PASSWORD or not SYNC_API_TOKEN:
|
||||
raise RuntimeError("ADMIN_PASSWORD and SYNC_API_TOKEN must be configured")
|
||||
Base.metadata.create_all(bind=engine)
|
||||
# Postgres may briefly restart or lag behind its health check at deploy time.
|
||||
for attempt in range(1, 11):
|
||||
try:
|
||||
Base.metadata.create_all(bind=engine)
|
||||
break
|
||||
except OperationalError as exc:
|
||||
if attempt == 10:
|
||||
raise
|
||||
logging.getLogger("uvicorn.error").warning(
|
||||
"Database not ready (attempt %s/10): %s", attempt, exc.orig
|
||||
)
|
||||
time.sleep(2)
|
||||
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user