tasko/backend/test_db.py
2025-12-10 15:19:07 +02:00

77 lines
2.1 KiB
Python

"""Test PostgreSQL connection and database setup"""
import sys
# Test 1: Check if psycopg2 is installed
try:
import psycopg2
print("✅ psycopg2 is installed")
except ImportError:
print("❌ psycopg2 is NOT installed")
print(" Run: pip install psycopg2-binary")
sys.exit(1)
# Test 2: Check SQLAlchemy import
try:
from sqlalchemy import create_engine
print("✅ SQLAlchemy is installed")
except ImportError:
print("❌ SQLAlchemy is NOT installed")
sys.exit(1)
# Test 3: Try to connect to PostgreSQL
try:
from database import engine, SessionLocal, User
print("✅ Database module imported successfully")
# Test connection
connection = engine.connect()
print("✅ Connected to PostgreSQL database")
connection.close()
except Exception as e:
print(f"❌ Failed to connect to database: {e}")
print("\nCheck:")
print(" 1. PostgreSQL is running")
print(" 2. Database 'tasko_db' exists")
print(" 3. User 'tasko_user' exists with correct password")
print(" 4. Run schema.sql first: psql -U postgres -f schema.sql")
sys.exit(1)
# Test 4: Check if tables exist
try:
from sqlalchemy import inspect
inspector = inspect(engine)
tables = inspector.get_table_names()
expected_tables = ['users', 'tokens', 'task_lists', 'tasks']
print(f"\n📋 Tables found: {tables}")
for table in expected_tables:
if table in tables:
print(f"✅ Table '{table}' exists")
else:
print(f"❌ Table '{table}' is missing")
if all(table in tables for table in expected_tables):
print("\n✅ All tables are present!")
else:
print("\n❌ Some tables are missing. Run schema.sql")
except Exception as e:
print(f"❌ Error checking tables: {e}")
sys.exit(1)
# Test 5: Try a simple query
try:
db = SessionLocal()
from database import User
user_count = db.query(User).count()
print(f"\n👥 Users in database: {user_count}")
db.close()
print("\n✅ Database is ready to use!")
except Exception as e:
print(f"❌ Error querying database: {e}")
sys.exit(1)