1+ #! /bin/bash
2+ 
3+ #  Python Database Connectivity Check Script
4+ 
5+ #  Check if Python is installed
6+ if  !  command  -v python3 & >  /dev/null;  then 
7+     echo  " Python 3 is not installed. Please install Python 3 first." 
8+     exit  1
9+ fi 
10+ 
11+ #  Create .env file if it doesn't exist
12+ if  [ !  -f  .env ];  then 
13+     cat >  .env << EOF 
14+ # PostgreSQL Configuration 
15+ DB_HOST="localhost" 
16+ DB_PORT="5432" 
17+ DB_USER="user" 
18+ DB_PASS="Password!2024" 
19+ DB_NAME="University" 
20+ 
21+ # MySQL Configuration 
22+ MYSQL_HOST="localhost" 
23+ MYSQL_PORT="3306" 
24+ MYSQL_USER="root" 
25+ MYSQL_PASS="MySQL!2024" 
26+ MYSQL_DB="University" 
27+ EOF 
28+     echo  " Created .env file with database configurations" 
29+ fi 
30+ 
31+ #  Install required Python packages
32+ echo  " Installing required Python packages..." 
33+ pip install psycopg2-binary mysql-connector-python python-dotenv >  /dev/null 2>&1 
34+ 
35+ #  Create PostgreSQL test script
36+ cat >  test_pg_connect.py << EOF 
37+ import os 
38+ import psycopg2 
39+ from dotenv import load_dotenv 
40+ 
41+ # Load environment variables from .env 
42+ load_dotenv() 
43+ 
44+ DB_HOST = os.getenv("DB_HOST") 
45+ DB_PORT = os.getenv("DB_PORT") 
46+ DB_NAME = os.getenv("DB_NAME") 
47+ DB_USER = os.getenv("DB_USER") 
48+ DB_PASS = os.getenv("DB_PASS") 
49+ 
50+ try: 
51+     connection = psycopg2.connect( 
52+         host=DB_HOST, 
53+         port=DB_PORT, 
54+         dbname=DB_NAME, 
55+         user=DB_USER, 
56+         password=DB_PASS 
57+     ) 
58+ 
59+     cursor = connection.cursor() 
60+     cursor.execute("SELECT to_regclass('public.student');") 
61+     table_exists = cursor.fetchone()[0] 
62+ 
63+     if table_exists: 
64+         print("PostgreSQL: Connection successful and student table exists.") 
65+     else: 
66+         print("PostgreSQL: Connection successful but student table does not exist.") 
67+ 
68+ except Exception as e: 
69+     print(f"PostgreSQL: Connection failed - {e}") 
70+ 
71+ finally: 
72+     if 'connection' in locals(): 
73+         connection.close() 
74+ EOF 
75+ 
76+ #  Create MySQL test script
77+ cat >  test_mysql_connect.py << EOF 
78+ import os 
79+ import mysql.connector 
80+ from dotenv import load_dotenv 
81+ 
82+ # Load environment variables from .env 
83+ load_dotenv() 
84+ 
85+ MYSQL_HOST = os.getenv("MYSQL_HOST") 
86+ MYSQL_PORT = os.getenv("MYSQL_PORT") 
87+ MYSQL_USER = os.getenv("MYSQL_USER") 
88+ MYSQL_PASS = os.getenv("MYSQL_PASS") 
89+ MYSQL_DB = os.getenv("MYSQL_DB") 
90+ 
91+ try: 
92+     connection = mysql.connector.connect( 
93+         host=MYSQL_HOST, 
94+         port=MYSQL_PORT, 
95+         user=MYSQL_USER, 
96+         password=MYSQL_PASS, 
97+         database=MYSQL_DB 
98+     ) 
99+ 
100+     cursor = connection.cursor() 
101+     cursor.execute("SHOW TABLES LIKE 'Department';") 
102+     result = cursor.fetchone() 
103+ 
104+     if result: 
105+         print("MySQL: Connection successful and department table exists.") 
106+     else: 
107+         print("MySQL: Connection successful but department table does not exist.") 
108+ 
109+ except mysql.connector.Error as err: 
110+     print(f"MySQL: Connection failed - {err}") 
111+ 
112+ finally: 
113+     if 'connection' in locals() and connection.is_connected(): 
114+         connection.close() 
115+ EOF 
116+ 
117+ #  Run the Python tests
118+ echo  " Running PostgreSQL test..." 
119+ python3 test_pg_connect.py
120+ 
121+ echo  " Running MySQL test..." 
122+ python3 test_mysql_connect.py
0 commit comments