Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions py_backend/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,31 @@
def _get_db_url() -> str:
url = os.getenv("ALEMBIC_DATABASE_URL") or os.getenv("DATABASE_URL")
if not url:
raise RuntimeError("Set ALEMBIC_DATABASE_URL or DATABASE_URL for Alembic migrations.")
# Try to get it from app.config.settings as fallback
try:
from app.config import settings
url = settings.DATABASE_URL
except Exception:
pass

if not url:
raise RuntimeError(
"Set ALEMBIC_DATABASE_URL or DATABASE_URL for Alembic migrations. "
"Neither environment variable nor app.config.settings.DATABASE_URL is set."
)

# Strip psql wrapper if present (e.g., "psql 'postgresql://...'")
if url.startswith("psql '") and url.endswith("'"):
url = url[6:-1]

# Replace postgresql:// with postgresql+psycopg:// for psycopg3
if url.startswith("postgresql://"):
if url.startswith("postgresql://") and not url.startswith("postgresql+psycopg://"):
url = url.replace("postgresql://", "postgresql+psycopg://", 1)

# Add sslmode=require for non-localhost connections (production)
if "sslmode=" not in url and "localhost" not in url and "127.0.0.1" not in url:
url = f"{url}{'&' if '?' in url else '?'}sslmode=require"
Comment on lines +51 to +53

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid unconditionally appending sslmode=require

The new _get_db_url appends sslmode=require whenever the URL host is not literally localhost or 127.0.0.1. Many existing local/docker deployments connect to Postgres over service names such as db or postgres without TLS. Those URLs will now always receive sslmode=require, causing Alembic to fail to connect because the server is not configured for SSL. This change will break migrations in common non‑TLS environments and should be guarded by an explicit setting rather than inferring from the hostname.

Useful? React with 👍 / 👎.


return url

def run_migrations_offline() -> None:
Expand Down
11 changes: 10 additions & 1 deletion py_backend/app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,19 @@ def create_caption(db: Session, image_id, title, prompt, model_code, raw_json, t
if img.image_type == "drone_image":
schema_id = "drone_caption@1.0.0"

# Handle "manual" model: if it doesn't exist in the database, set model to NULL
# This can happen if migrations didn't run (e.g., in production with fallback table creation)
model_value = model_code
if model_code == "manual":
manual_model = db.query(models.Models).filter(models.Models.m_code == 'manual').first()
if not manual_model:
logger.warning("'manual' model not found in database, setting caption.model to NULL")
model_value = None

caption = models.Captions(
title=title,
prompt=prompt,
model=model_code,
model=model_value,
schema_id=schema_id,
raw_json=raw_json,
generated=text,
Expand Down
Loading