stones/check-db.sh

78 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Script to check database connectivity for the Stones Database application
set -e # Exit immediately if a command exits with a non-zero status
# Colors for pretty output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${YELLOW}Checking database connectivity...${NC}"
# Check if Prisma is installed
if [ ! -d "node_modules/.prisma" ]; then
echo -e "${YELLOW}Installing Prisma dependencies...${NC}"
npm install @prisma/client
npx prisma generate
fi
# Create temporary script to check DB connection
cat > db-check.js << EOF
const { PrismaClient } = require('@prisma/client');
async function main() {
console.log('Attempting to connect to database...');
const prisma = new PrismaClient();
try {
// Test the connection
await prisma.\$connect();
console.log('Successfully connected to the database.');
// Get some database statistics
const contactCount = await prisma.contact.count();
const nftHoldingCount = await prisma.nftHolding.count();
const daoMembershipCount = await prisma.daoMembership.count();
const tokenHoldingCount = await prisma.tokenHolding.count();
console.log('Database statistics:');
console.log(\`Contacts: \${contactCount}\`);
console.log(\`NFT Holdings: \${nftHoldingCount}\`);
console.log(\`DAO Memberships: \${daoMembershipCount}\`);
console.log(\`Token Holdings: \${tokenHoldingCount}\`);
await prisma.\$disconnect();
process.exit(0);
} catch (error) {
console.error('Failed to connect to the database:', error.message);
if (error.message.includes('database does not exist')) {
console.error('The database specified in your DATABASE_URL does not exist.');
console.error('You may need to create it manually:');
console.error(' 1. Connect to PostgreSQL using: psql -U postgres');
console.error(' 2. Create the database: CREATE DATABASE stones;');
console.error(' 3. Update your .env or .env.local file with the correct DATABASE_URL');
} else if (error.message.includes('authentication failed')) {
console.error('Authentication failed. Check your username and password in DATABASE_URL.');
} else if (error.message.includes('connect ECONNREFUSED')) {
console.error('Could not connect to PostgreSQL server. Make sure it is running.');
}
await prisma.\$disconnect();
process.exit(1);
}
}
main();
EOF
# Run the temporary script
echo -e "${YELLOW}Running database connection test...${NC}"
node db-check.js
# Clean up
rm db-check.js
echo -e "${GREEN}Database check completed.${NC}"