#!/bin/bash # Database setup script for Omada Cloud Integration set -e echo "=== Database Setup Script ===" echo "" # Check if PostgreSQL is running if ! pg_isready -h localhost -p 5432 >/dev/null 2>&1; then echo "⚠️ PostgreSQL is not running on localhost:5432" echo "" echo "Options:" echo "1. Start PostgreSQL with Docker:" echo " docker run -d --name omada-postgres \\" echo " -e POSTGRES_USER=omada_user \\" echo " -e POSTGRES_PASSWORD=omada_password \\" echo " -e POSTGRES_DB=omada_db \\" echo " -p 5433:5432 postgres:15-alpine" echo "" echo " Then update DATABASE_URL in .env to use port 5433" echo "" echo "2. Use existing PostgreSQL (you'll need admin credentials)" echo "" exit 1 fi echo "✅ PostgreSQL is running" echo "" # Try to create database with different methods echo "Attempting to create database..." # Method 1: Try with default postgres user if psql -h localhost -U postgres -lqt 2>/dev/null | cut -d \| -f 1 | grep -qw omada_db; then echo "✅ Database 'omada_db' already exists" else echo "Creating database 'omada_db'..." echo "You may need to enter PostgreSQL admin password" createdb -h localhost -U postgres omada_db 2>/dev/null || \ psql -h localhost -U postgres -c "CREATE DATABASE omada_db;" 2>/dev/null || \ echo "⚠️ Could not create database automatically. Please create it manually:" echo " CREATE DATABASE omada_db;" echo " CREATE USER omada_user WITH PASSWORD 'omada_password';" echo " GRANT ALL PRIVILEGES ON DATABASE omada_db TO omada_user;" fi echo "" echo "✅ Database setup complete!" echo "" echo "Next steps:" echo "1. Run: pnpm run prisma:migrate" echo "2. Verify: pnpm run prisma:studio"