oedb-backend/test_permissions.sh
2025-09-15 23:25:11 +02:00

45 lines
No EOL
1.5 KiB
Bash
Executable file

#!/bin/bash
# Script to test database permissions for the events table
# 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"}
# Set PGPASSWORD for authentication
export PGPASSWORD="${POSTGRES_PASSWORD}"
echo "Testing database permissions for user '$DB_USER'..."
echo "Attempting to select from events table..."
# Try to select from the events table
result=$(psql -h $DB_HOST -U $DB_USER -d $DB_NAME -t -c "SELECT COUNT(*) FROM events;")
if [ $? -eq 0 ]; then
echo "✅ Success! User '$DB_USER' has permission to access the events table."
echo "Count of events: $result"
else
echo "❌ Error: User '$DB_USER' does not have permission to access the events table."
echo "Running setup_db.sh to fix permissions..."
./setup_db.sh
# Test again after running setup_db.sh
echo "Testing permissions again..."
result=$(psql -h $DB_HOST -U $DB_USER -d $DB_NAME -t -c "SELECT COUNT(*) FROM events;")
if [ $? -eq 0 ]; then
echo "✅ Success! Permissions fixed. User '$DB_USER' now has access to the events table."
echo "Count of events: $result"
else
echo "❌ Error: Still unable to access the events table. Please check database configuration."
fi
fi
echo "Test completed."
exit 0