Files
TimeTracker/docker/test-db-simple.py
Dries Peeters 8a378b7078 feat(clients,license,db): add client management, enhanced DB init, and tests
- Clients: add model, routes, and templates
  - app/models/client.py
  - app/routes/clients.py
  - templates/clients/{create,edit,list,view}.html
  - docs/CLIENT_MANAGEMENT_README.md
- Database: add enhanced init/verify scripts, migrations, and docs
  - docker/{init-database-enhanced.py,start-enhanced.py,verify-database.py}
  - docs/ENHANCED_DATABASE_STARTUP.md
  - migrations/{add_analytics_column.sql,add_analytics_setting.py,migrate_to_client_model.py}
- Scripts: add version manager and docker network test helpers
  - scripts/version-manager.{bat,ps1,py,sh}
  - scripts/test-docker-network.{bat,sh}
  - docs/VERSION_MANAGEMENT.md
- UI: tweak base stylesheet
  - app/static/base.css
- Tests: add client system test
  - test_client_system.py
2025-09-01 11:34:45 +02:00

90 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""
Simple database connection test script
"""
import os
import sys
import psycopg2
def test_database_connection():
"""Test basic database connection"""
print("=== Testing Database Connection ===")
# Get database URL from environment
db_url = os.getenv('DATABASE_URL', 'postgresql+psycopg2://timetracker:timetracker@db:5432/timetracker')
# Parse the URL to get connection details
if db_url.startswith('postgresql+psycopg2://'):
db_url = db_url.replace('postgresql+psycopg2://', '')
# Extract host, port, database, user, password
if '@' in db_url:
auth_part, rest = db_url.split('@', 1)
user, password = auth_part.split(':', 1)
if ':' in rest:
host_port, database = rest.rsplit('/', 1)
if ':' in host_port:
host, port = host_port.split(':', 1)
else:
host, port = host_port, '5432'
else:
host, port, database = rest, '5432', 'timetracker'
else:
host, port, database, user, password = 'db', '5432', 'timetracker', 'timetracker', 'timetracker'
print(f"Connection details:")
print(f" Host: {host}")
print(f" Port: {port}")
print(f" Database: {database}")
print(f" User: {user}")
print(f" Password: {'*' * len(password)}")
try:
print(f"\nAttempting connection...")
conn = psycopg2.connect(
host=host,
port=port,
database=database,
user=user,
password=password,
connect_timeout=10
)
print("✓ Database connection successful!")
# Test a simple query
cursor = conn.cursor()
cursor.execute("SELECT version()")
version = cursor.fetchone()
print(f"✓ Database version: {version[0]}")
# Check if tables exist
cursor.execute("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name
""")
tables = cursor.fetchall()
if tables:
print(f"✓ Found {len(tables)} tables:")
for table in tables:
print(f" - {table[0]}")
else:
print("⚠ No tables found in database")
cursor.close()
conn.close()
print("✓ Connection closed successfully")
return True
except Exception as e:
print(f"✗ Database connection failed: {e}")
return False
if __name__ == '__main__':
success = test_database_connection()
sys.exit(0 if success else 1)