91 lines
No EOL
3 KiB
Bash
Executable file
91 lines
No EOL
3 KiB
Bash
Executable file
#!/bin/bash
|
|
# Script to check if database tables are properly created
|
|
# Uses environment variables from .env file
|
|
|
|
# Load environment variables from .env file
|
|
if [ -f .env ]; then
|
|
echo "Loading environment variables from .env file..."
|
|
export $(grep -v '^#' .env | xargs)
|
|
fi
|
|
|
|
# Default values if not set in .env
|
|
DB_NAME=${DB_NAME:-"oedb"}
|
|
DB_USER=${DB_USER:-"postgres"}
|
|
DB_HOST=${DB_HOST:-"localhost"}
|
|
|
|
# Function to check if a table exists
|
|
check_table() {
|
|
local table_name=$1
|
|
echo "Checking if table '$table_name' exists..."
|
|
|
|
# Query to check if table exists
|
|
result=$(PGPASSWORD="$POSTGRES_PASSWORD" psql -h $DB_HOST -U $DB_USER -d $DB_NAME -t -c "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '$table_name');")
|
|
|
|
# Trim whitespace
|
|
result=$(echo $result | xargs)
|
|
|
|
if [ "$result" = "t" ]; then
|
|
echo "✅ Table '$table_name' exists."
|
|
return 0
|
|
else
|
|
echo "❌ Table '$table_name' does not exist!"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to check table structure
|
|
check_table_columns() {
|
|
local table_name=$1
|
|
echo "Checking structure of table '$table_name'..."
|
|
|
|
# Get column information
|
|
PGPASSWORD="$POSTGRES_PASSWORD" psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = '$table_name';"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo "✅ Successfully retrieved structure for table '$table_name'."
|
|
return 0
|
|
else
|
|
echo "❌ Failed to retrieve structure for table '$table_name'!"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Check database connection
|
|
echo "Connecting to PostgreSQL database '$DB_NAME' as user '$DB_USER'..."
|
|
export PGPASSWORD="$POSTGRES_PASSWORD"
|
|
if ! psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT 1" > /dev/null 2>&1; then
|
|
echo "❌ Failed to connect to database. Please check your credentials and database status."
|
|
exit 1
|
|
fi
|
|
echo "✅ Successfully connected to database."
|
|
|
|
# Check required tables
|
|
tables=("events" "events_deleted" "geo")
|
|
all_tables_exist=true
|
|
|
|
for table in "${tables[@]}"; do
|
|
if ! check_table "$table"; then
|
|
all_tables_exist=false
|
|
fi
|
|
done
|
|
|
|
# If all tables exist, check their structure
|
|
if [ "$all_tables_exist" = true ]; then
|
|
echo -e "\n--- Detailed Table Structure ---"
|
|
for table in "${tables[@]}"; do
|
|
echo -e "\n"
|
|
check_table_columns "$table"
|
|
done
|
|
|
|
echo -e "\n✅ All required tables exist in the database."
|
|
else
|
|
echo -e "\n❌ Some required tables are missing. Please run setup_db.sh to initialize the database."
|
|
exit 1
|
|
fi
|
|
|
|
# Check for indexes (optional but recommended)
|
|
echo -e "\n--- Checking Indexes ---"
|
|
PGPASSWORD="$POSTGRES_PASSWORD" psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT tablename, indexname FROM pg_indexes WHERE schemaname = 'public' AND tablename IN ('events', 'events_deleted', 'geo');"
|
|
|
|
echo -e "\n✅ Database tables check completed successfully."
|
|
exit 0 |