Skip to content

Commit 8be28e8

Browse files
committed
[SQL-428] Checking Database Connectivity with a script
1 parent e2e6e12 commit 8be28e8

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# MySQL Database Connectivity Check Script
4+
5+
# Create .env file if it doesn't exist
6+
if [ ! -f .env ]; then
7+
cat > .env <<EOF
8+
# MySQL Configuration
9+
MYSQL_HOST="localhost"
10+
MYSQL_PORT="3306"
11+
MYSQL_USER="root"
12+
MYSQL_PASS="MySQL!2024"
13+
MYSQL_DB="University"
14+
EOF
15+
echo "Created .env file with MySQL configuration"
16+
fi
17+
18+
# Load environment variables
19+
source .env
20+
21+
echo "Testing MySQL connectivity..."
22+
23+
# Test MySQL connectivity and check for 'Department' table
24+
mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "USE $MYSQL_DB; SHOW TABLES LIKE 'Department';" >/dev/null 2>&1
25+
26+
if [ $? -eq 0 ]; then
27+
echo "MySQL: Connection successful and department table exists."
28+
else
29+
echo "MySQL: Connection failed or department table does not exist."
30+
fi
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# PostgreSQL Database Connectivity Check Script
4+
5+
# Create .env file if it doesn't exist
6+
if [ ! -f .env ]; then
7+
cat > .env <<EOF
8+
DB_HOST="localhost"
9+
DB_PORT="5432"
10+
DB_USER="user"
11+
DB_PASS="Password!2024"
12+
DB_NAME="University"
13+
EOF
14+
echo "Created .env file with PostgreSQL configuration"
15+
fi
16+
17+
# Load environment variables
18+
source .env
19+
20+
echo "Testing PostgreSQL connectivity..."
21+
22+
# Test PostgreSQL connectivity and check for 'student' table
23+
PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c '\dt student' >/dev/null 2>&1
24+
25+
if [ $? -eq 0 ]; then
26+
echo "PostgreSQL: Connection successful and student table exists."
27+
else
28+
echo "PostgreSQL: Connection failed or student table does not exist."
29+
fi
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)