2121
2222@app .on_event ("startup" )
2323async 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" )
3432def 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