#!/bin/bash # DEV-ONLY: MongoDB setup script for local development environments. # DO NOT run in production. # Define colors for readable output GREEN="\033[0;32m" RED="\033[0;31m" YELLOW="\033[0;33m" BLUE="\033[0;34m" NC="\033[0m" # No Color # S-C2: Block production environments MONGO_HOST="${MONGO_HOST:-localhost}" APP_ENV="${APP_ENV:-development}" echo -e "${BLUE}===== MongoDB Setup Script (DEV ONLY) =====${NC}" echo -e "This script sets up MongoDB for LOCAL DEVELOPMENT only." echo "" if [ "$APP_ENV" = "production" ]; then echo -e "${RED}ERROR: This script must not be run in production (APP_ENV=production).${NC}" exit 1 fi if [ "$MONGO_HOST" != "localhost" ] && [ "$MONGO_HOST" != "127.0.0.1" ]; then echo -e "${RED}ERROR: MONGO_HOST is set to '$MONGO_HOST'. This script only runs against localhost.${NC}" exit 1 fi echo -e "${YELLOW}WARNING: This script configures MongoDB for development (no authentication).${NC}" echo -e "This is INSECURE and should NEVER be done on a production server." echo "" read -r -p "Continue? [y/N] " confirm if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo "Aborted." exit 0 fi # Check if MongoDB is running if ! pgrep -x "mongod" > /dev/null; then echo -e "${YELLOW}MongoDB is not running. Attempting to start MongoDB...${NC}" if [[ "$OSTYPE" == "darwin"* ]]; then # macOS brew services start mongodb-community || mongod --config /usr/local/etc/mongod.conf --fork else # Linux sudo systemctl start mongod || sudo service mongod start fi # Wait for MongoDB to start sleep 3 # Check again if ! pgrep -x "mongod" > /dev/null; then echo -e "${RED}Failed to start MongoDB. Please start it manually before running this script.${NC}" exit 1 else echo -e "${GREEN}MongoDB started successfully.${NC}" fi else echo -e "${GREEN}MongoDB is already running.${NC}" fi echo -e "${YELLOW}Creating semblance_db database and collections...${NC}" # S-M3: Use mongosh instead of deprecated mongo CLI mongosh --eval ' db = db.getSiblingDB("semblance_db"); db.createCollection("users"); db.createCollection("personas"); db.createCollection("focus_groups"); print("Collections created."); ' echo -e "${GREEN}MongoDB setup completed. The database is now ready for development.${NC}" echo -e "${YELLOW}Note: You may need to restart MongoDB for all changes to take effect:${NC}" echo -e " - On macOS: brew services restart mongodb-community" echo -e " - On Linux: sudo systemctl restart mongod"