Update src/database/candidates/client.py
Browse files
src/database/candidates/client.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from sqlalchemy import create_engine
|
| 3 |
from sqlalchemy.orm import sessionmaker
|
| 4 |
from src.database.candidates.models import Base
|
| 5 |
from src.configs import get_database_settings
|
| 6 |
-
import socket
|
| 7 |
|
| 8 |
|
| 9 |
def get_engine():
|
|
@@ -17,25 +17,25 @@ def get_engine():
|
|
| 17 |
"""
|
| 18 |
settings = get_database_settings()
|
| 19 |
|
| 20 |
-
# Allow POSTGRES_HOST override
|
| 21 |
-
|
| 22 |
-
postgres_host =
|
| 23 |
|
| 24 |
# If 'db' (compose) is not resolvable (single-container run), fall back to host.docker.internal
|
| 25 |
try:
|
| 26 |
socket.gethostbyname(postgres_host)
|
| 27 |
except Exception:
|
| 28 |
-
fallback = os.getenv("POSTGRES_HOST_FALLBACK", "host.docker.internal").strip()
|
| 29 |
-
print(f"
|
| 30 |
postgres_host = fallback
|
|
|
|
| 31 |
database_url = (
|
| 32 |
f"postgresql+psycopg2://{settings.user}:{settings.password}"
|
| 33 |
f"@{postgres_host}:{settings.port}/{settings.db}"
|
| 34 |
)
|
| 35 |
|
| 36 |
-
print(f"
|
| 37 |
|
| 38 |
-
# Optional: echo=True for debugging SQL statements
|
| 39 |
return create_engine(database_url, echo=False, future=True)
|
| 40 |
|
| 41 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import socket
|
| 3 |
from sqlalchemy import create_engine
|
| 4 |
from sqlalchemy.orm import sessionmaker
|
| 5 |
from src.database.candidates.models import Base
|
| 6 |
from src.configs import get_database_settings
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def get_engine():
|
|
|
|
| 17 |
"""
|
| 18 |
settings = get_database_settings()
|
| 19 |
|
| 20 |
+
# Allow POSTGRES_HOST override; strip whitespace/quotes to avoid DNS issues
|
| 21 |
+
raw_host = os.getenv("POSTGRES_HOST", settings.host)
|
| 22 |
+
postgres_host = raw_host.strip().strip("\"'")
|
| 23 |
|
| 24 |
# If 'db' (compose) is not resolvable (single-container run), fall back to host.docker.internal
|
| 25 |
try:
|
| 26 |
socket.gethostbyname(postgres_host)
|
| 27 |
except Exception:
|
| 28 |
+
fallback = os.getenv("POSTGRES_HOST_FALLBACK", "host.docker.internal").strip().strip("\"'")
|
| 29 |
+
print(f"[db-client] Host '{postgres_host}' not resolvable; falling back to '{fallback}'")
|
| 30 |
postgres_host = fallback
|
| 31 |
+
|
| 32 |
database_url = (
|
| 33 |
f"postgresql+psycopg2://{settings.user}:{settings.password}"
|
| 34 |
f"@{postgres_host}:{settings.port}/{settings.db}"
|
| 35 |
)
|
| 36 |
|
| 37 |
+
print(f"[db-client] Connecting to database at host={postgres_host} port={settings.port} db={settings.db} user={settings.user}", flush=True)
|
| 38 |
|
|
|
|
| 39 |
return create_engine(database_url, echo=False, future=True)
|
| 40 |
|
| 41 |
|