Skip to content

Commit 289d2b8

Browse files
committed
added a script to check db connection
1 parent 175dcab commit 289d2b8

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

apps/backend/app/auth/service.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ async def get_or_create_user(
5353
self, email: str, name: str, avatar_url: str = None, google_id: str = None
5454
) -> Optional[Dict[str, Any]]:
5555
"""Get existing user or create new one"""
56+
# Add basic debug print to start
57+
import sys
58+
print(f"DEBUG: Attempting auth for {email}", file=sys.stderr, flush=True)
59+
5660
try:
5761
async with async_session_factory() as db:
5862
result = await db.execute(select(User).where(User.email == email))
5963
user = result.scalar_one_or_none()
6064

6165
if user:
66+
print(f"DEBUG: Found existing user {user.id}", file=sys.stderr, flush=True)
6267
return {
6368
"id": str(user.id),
6469
"email": user.email,
6570
"name": user.name,
6671
"avatar_url": user.avatar_url,
6772
}
6873

74+
print(f"DEBUG: Creating new user for {email}", file=sys.stderr, flush=True)
6975
new_user = User(
7076
email=email,
7177
name=name,
@@ -75,6 +81,7 @@ async def get_or_create_user(
7581
db.add(new_user)
7682
await db.commit()
7783
await db.refresh(new_user)
84+
print(f"DEBUG: Created new user {new_user.id}", file=sys.stderr, flush=True)
7885

7986
return {
8087
"id": str(new_user.id),

apps/backend/check_db.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import asyncio
2+
import os
3+
import sys
4+
from sqlalchemy.ext.asyncio import create_async_engine
5+
from sqlalchemy import text
6+
7+
# Add current directory to path to find 'app'
8+
sys.path.append(os.getcwd())
9+
10+
display_url = "UNKNOWN"
11+
12+
try:
13+
from app.config import settings
14+
url = settings.POSTGRES_URL
15+
# Mask password for display
16+
if "@" in url:
17+
display_url = url.split("@")[1]
18+
user_part = url.split("@")[0]
19+
if ":" in user_part:
20+
display_url = f"{user_part.split(':')[0]}:****@{display_url}"
21+
else:
22+
display_url = f"{user_part}@{display_url}"
23+
else:
24+
display_url = url
25+
26+
print(f"Checking connection to: {display_url}")
27+
except Exception as e:
28+
print(f"Could not load settings: {e}")
29+
url = os.environ.get("POSTGRES_URL")
30+
print(f"Using URL from env: {url}")
31+
32+
if not url:
33+
print("Error: POSTGRES_URL not set!")
34+
sys.exit(1)
35+
36+
async def check():
37+
print("-" * 50)
38+
print("Starting connection test...")
39+
try:
40+
# Create engine
41+
engine = create_async_engine(url, echo=False)
42+
43+
# Try to connect
44+
async with engine.connect() as conn:
45+
result = await conn.execute(text("SELECT 1"))
46+
val = result.scalar()
47+
print(f"SUCCESS: Database returned {val}")
48+
49+
# Check database name
50+
result_db = await conn.execute(text("SELECT current_database()"))
51+
db_name = result_db.scalar()
52+
print(f"Connected to database: {db_name}")
53+
54+
await engine.dispose()
55+
print("-" * 50)
56+
print("✅ DATABASE CONNECTION HEALTHY")
57+
58+
except Exception as e:
59+
print("-" * 50)
60+
print(f"❌ CONNECTION FAILED")
61+
print(f"Error type: {type(e).__name__}")
62+
print(f"Error message: {e}")
63+
print("-" * 50)
64+
65+
if __name__ == "__main__":
66+
asyncio.run(check())

0 commit comments

Comments
 (0)