Skip to content

Commit a283041

Browse files
committed
Simplify database configuration - fallback to SQLite
1 parent 38ef891 commit a283041

2 files changed

Lines changed: 9 additions & 85 deletions

File tree

backend/app/database.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@
1111
)
1212
sys.stderr.write(f"[DB DEBUG] DATA_DIR: {os.environ.get('DATA_DIR', 'Not set')}\n")
1313

14-
RAILWAY_TEST_VARIABLE = "THIS_SHOULD_APPEAR_IN_LOGS_IF_CODE_IS_LOADED"
15-
1614
if DATABASE_URL:
1715
if DATABASE_URL.startswith("postgresql://"):
18-
sys.stderr.write("[DB DEBUG] Using PostgreSQL with NullPool and SSL\n")
16+
sys.stderr.write("[DB DEBUG] Using PostgreSQL\n")
17+
18+
from urllib.parse import urlparse
19+
20+
parsed = urlparse(DATABASE_URL)
21+
1922
engine = create_engine(
2023
DATABASE_URL,
2124
poolclass=NullPool,
2225
connect_args={
23-
"connect_timeout": 10,
26+
"connect_timeout": 30,
2427
"sslmode": "require",
25-
"application_name": "proproo-backend",
28+
"keepalives": 1,
29+
"keepalives_idle": 60,
2630
},
2731
)
2832
else:

backend/app/main.py

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121

2222
@app.on_event("startup")
2323
async def startup_event():
24-
"""Initialize database on startup"""
2524
try:
26-
# Create tables if they don't exist
2725
Base.metadata.create_all(bind=engine)
2826
print("[STARTUP] Database tables created/verified")
2927
except Exception as e:
@@ -33,81 +31,3 @@ async def startup_event():
3331
@app.get("/health")
3432
def health_check():
3533
return {"status": "ok"}
36-
37-
38-
@app.get("/test-db")
39-
def test_db():
40-
"""Test database connection"""
41-
import os
42-
import ssl
43-
import socket
44-
import subprocess
45-
46-
DATABASE_URL = os.environ.get("DATABASE_URL", "")
47-
48-
results = {"tests": []}
49-
50-
try:
51-
from urllib.parse import urlparse
52-
53-
parsed = urlparse(DATABASE_URL)
54-
hostname = parsed.hostname
55-
port = parsed.port or 5432
56-
57-
results["tests"].append({"host": hostname, "port": port})
58-
59-
try:
60-
proc = subprocess.run(
61-
["psql", "--version"], capture_output=True, text=True, timeout=5
62-
)
63-
results["tests"].append({"psql": proc.stdout.strip()})
64-
except Exception as e:
65-
results["tests"].append({"psql_error": str(e)[:100]})
66-
67-
try:
68-
result = subprocess.run(
69-
[
70-
"psql",
71-
f"postgresql://{parsed.username}:{parsed.password}@{hostname}:{port}{parsed.path}",
72-
"-c",
73-
"SELECT 1;",
74-
],
75-
capture_output=True,
76-
text=True,
77-
timeout=30,
78-
env={**os.environ, "PGSSLMODE": "require"},
79-
)
80-
results["tests"].append(
81-
{"psql_require": result.stdout[:200] + result.stderr[:200]}
82-
)
83-
except Exception as e:
84-
results["tests"].append({"psql_require_error": str(e)[:200]})
85-
86-
try:
87-
import psycopg2
88-
89-
for mode in ["require"]:
90-
try:
91-
conn = psycopg2.connect(
92-
host=hostname,
93-
port=port,
94-
dbname=parsed.path[1:],
95-
user=parsed.username,
96-
password=parsed.password,
97-
sslmode=mode,
98-
sslcompression=0,
99-
)
100-
cur = conn.cursor()
101-
cur.execute("SELECT 1;")
102-
cur.close()
103-
conn.close()
104-
results["tests"].append({f"psycopg2_{mode}_nocomp": "success"})
105-
except Exception as e:
106-
results["tests"].append({f"psycopg2_{mode}_nocomp": str(e)[:200]})
107-
except ImportError:
108-
results["tests"].append({"psycopg2": "not installed"})
109-
110-
except Exception as e:
111-
results["error"] = str(e)
112-
113-
return results

0 commit comments

Comments
 (0)