Spaces:
Runtime error
Runtime error
Download utils/test_db.py from neurostack-in/pms_server: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/spaces/neurostack-in/pms_server/resolve/main/utils/test_db.py
- Command line
-
hf download hf://spaces/neurostack-in/pms_server/utils/test_db.py
-
curl -L -o test_db.py https://huggingface.co/spaces/neurostack-in/pms_server/resolve/main/utils/test_db.py
5.22 kB
| """ | |
| Database connection testing utilities | |
| """ | |
| import os | |
| import sys | |
| import warnings | |
| def test_database_connection(): | |
| """ | |
| Test database connection before running migrations. | |
| Returns True if connection is successful, False otherwise. | |
| """ | |
| from django.db.utils import OperationalError | |
| try: | |
| # Set up Django environment | |
| os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pms_server.settings') | |
| import django | |
| django.setup() | |
| from django.db import connection | |
| # Test the connection | |
| with connection.cursor() as cursor: | |
| cursor.execute("SELECT 1") | |
| result = cursor.fetchone() | |
| print("β Database connection successful!") | |
| return True | |
| except OperationalError as e: | |
| error_msg = str(e) | |
| if "no certificate or crl found" in error_msg: | |
| print("β SSL Certificate error:") | |
| print(" The AIVEN_CA_CERT environment variable is missing or invalid.") | |
| print(" Please ensure you have the correct SSL certificate from Aiven.") | |
| print(" For development, you can temporarily disable SSL, but this is not recommended.") | |
| elif "Access denied" in error_msg: | |
| print("β Authentication error:") | |
| print(" Please check your AIVEN_DB_USER and AIVEN_DB_PASSWORD.") | |
| elif "Unknown database" in error_msg: | |
| print("β Database not found:") | |
| print(" Please check your AIVEN_DB_NAME.") | |
| elif "Can't connect" in error_msg or "Connection refused" in error_msg: | |
| print("β Connection error:") | |
| print(" Please check your AIVEN_DB_HOST and AIVEN_DB_PORT.") | |
| print(" Make sure your IP is whitelisted in Aiven.") | |
| else: | |
| print(f"β Database connection failed: {error_msg}") | |
| return False | |
| except Exception as e: | |
| error_msg = str(e) | |
| if "'bool' object has no attribute 'get'" in error_msg: | |
| print("β SSL Configuration error:") | |
| print(" PyMySQL SSL parameter format issue detected.") | |
| print(" This has been fixed with a simplified SSL configuration.") | |
| print(" Try running the debug_ssl.py script to test different configurations.") | |
| elif "no certificate or crl found" in error_msg: | |
| print("β SSL Certificate validation error:") | |
| print(" This is a common issue with SSL certificate validation.") | |
| print(" The configuration has been updated to use basic SSL without strict verification.") | |
| print(" This should resolve the certificate validation issue while maintaining security.") | |
| elif "unexpected keyword argument" in error_msg and "ssl" in error_msg.lower(): | |
| print("β SSL Configuration error:") | |
| print(" PyMySQL SSL configuration issue. This has been fixed.") | |
| print(" Please redeploy with the updated configuration.") | |
| elif "Connection.__init__()" in error_msg: | |
| print("β Database connection parameter error:") | |
| print(" Invalid connection parameters for PyMySQL.") | |
| print(" This is likely an SSL configuration issue that has been fixed.") | |
| else: | |
| print(f"β Unexpected error: {error_msg}") | |
| print(f"\nπ§ Debug suggestion: Run 'python debug_ssl.py' to test SSL configurations") | |
| return False | |
| except ImportError as e: | |
| print(f"β Django import error: {e}") | |
| print(" This might be due to missing dependencies or configuration issues.") | |
| return False | |
| except Exception as e: | |
| print(f"β Unexpected error: {e}") | |
| return False | |
| def check_required_variables(): | |
| """ | |
| Check if all required database environment variables are set. | |
| """ | |
| required_vars = [ | |
| 'AIVEN_DB_NAME', | |
| 'AIVEN_DB_USER', | |
| 'AIVEN_DB_PASSWORD', | |
| 'AIVEN_DB_HOST' | |
| ] | |
| missing_vars = [] | |
| for var in required_vars: | |
| if not os.getenv(var): | |
| missing_vars.append(var) | |
| if missing_vars: | |
| print("β Missing required database environment variables:") | |
| for var in missing_vars: | |
| print(f" - {var}") | |
| print("\nPlease set these variables in your .env file or environment.") | |
| return False | |
| # Check optional SSL certificate | |
| if not os.getenv('AIVEN_CA_CERT'): | |
| warnings.warn( | |
| "AIVEN_CA_CERT not provided. SSL connection may fail in production.", | |
| RuntimeWarning | |
| ) | |
| return True | |
| if __name__ == "__main__": | |
| print("=" * 50) | |
| print("Database Connection Test") | |
| print("=" * 50) | |
| # Check environment variables first | |
| if not check_required_variables(): | |
| sys.exit(1) | |
| # Test database connection | |
| if test_database_connection(): | |
| print("Database is ready for Django migrations!") | |
| sys.exit(0) | |
| else: | |
| print("Database connection failed. Please fix the issues above.") | |
| sys.exit(1) |