#!/bin/bash # OpenCand Database Deployment Script # This script deploys the database schema changes to the PostgreSQL container set -e # Exit on any error # Configuration DB_CONTAINER="opencand_db" DB_NAME="opencand" DB_USER="root" SQL_URL="https://git.ivanch.me/ivanch/opencand/raw/branch/main/db/db.sql" SQL_FILE="./db.sql" MV_URL="https://git.ivanch.me/ivanch/opencand/raw/branch/main/db/mv.sql" MV_FILE="./mv.sql" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${YELLOW}๐Ÿš€ OpenCand Database Deployment Script${NC}" echo "==================================" # Check if Docker is running if ! docker info >/dev/null 2>&1; then echo -e "${RED}โŒ Error: Docker is not running or not accessible${NC}" exit 1 fi # Check if the database container is running if ! docker ps --format "table {{.Names}}" | grep -q "^${DB_CONTAINER}$"; then echo -e "${RED}โŒ Error: Database container '${DB_CONTAINER}' is not running${NC}" echo "Please start the services with: docker-compose up -d" exit 1 fi # Download the SQL files echo -e "${YELLOW}๐Ÿ“ฅ Downloading SQL files...${NC}" if command -v curl &> /dev/null; then if curl -L -o "$SQL_FILE" "$SQL_URL"; then echo "โœ… db.sql downloaded successfully" else echo -e "${RED}โŒ Error: Failed to download db.sql from ${SQL_URL}${NC}" exit 1 fi if curl -L -o "$MV_FILE" "$MV_URL"; then echo "โœ… mv.sql downloaded successfully" else echo -e "${RED}โŒ Error: Failed to download mv.sql from ${MV_URL}${NC}" exit 1 fi elif command -v wget &> /dev/null; then if wget -O "$SQL_FILE" "$SQL_URL"; then echo "โœ… db.sql downloaded successfully" else echo -e "${RED}โŒ Error: Failed to download db.sql from ${SQL_URL}${NC}" exit 1 fi if wget -O "$MV_FILE" "$MV_URL"; then echo "โœ… mv.sql downloaded successfully" else echo -e "${RED}โŒ Error: Failed to download mv.sql from ${MV_URL}${NC}" exit 1 fi else echo -e "${RED}โŒ Error: Neither curl nor wget is available for downloading${NC}" exit 1 fi # Check if SQL files exist (after download) if [ ! -f "$SQL_FILE" ]; then echo -e "${RED}โŒ Error: SQL file '${SQL_FILE}' not found after download${NC}" exit 1 fi if [ ! -f "$MV_FILE" ]; then echo -e "${RED}โŒ Error: SQL file '${MV_FILE}' not found after download${NC}" exit 1 fi echo -e "${YELLOW}๐Ÿ“‹ Pre-deployment checks:${NC}" echo "โœ… Docker is running" echo "โœ… Database container is running" echo "โœ… db.sql downloaded" echo "โœ… mv.sql downloaded" echo "" # Wait for database to be ready echo -e "${YELLOW}โณ Waiting for database to be ready...${NC}" timeout=30 counter=0 while ! docker exec $DB_CONTAINER pg_isready -U $DB_USER -d $DB_NAME >/dev/null 2>&1; do sleep 1 counter=$((counter + 1)) if [ $counter -ge $timeout ]; then echo -e "${RED}โŒ Error: Database failed to become ready within ${timeout} seconds${NC}" exit 1 fi done echo "โœ… Database is ready" echo "" # Deploy the db.sql file echo -e "${YELLOW}๐Ÿ”ง Deploying database changes (db.sql)...${NC}" echo "Executing: $SQL_FILE" if docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $SQL_FILE; then echo -e "${GREEN}โœ… db.sql deployment completed successfully!${NC}" else echo -e "${RED}โŒ Error: db.sql deployment failed${NC}" echo -e "${YELLOW}๐Ÿ’ก You can restore from backup using:${NC}" echo "docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $BACKUP_FILE" # Clean up before exit rm -f "$SQL_FILE" "$MV_FILE" exit 1 fi # Deploy the mv.sql file echo -e "${YELLOW}๏ฟฝ Deploying database changes (mv.sql)...${NC}" echo "Executing: $MV_FILE" if docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $MV_FILE; then echo -e "${GREEN}โœ… mv.sql deployment completed successfully!${NC}" else echo -e "${RED}โŒ Error: mv.sql deployment failed${NC}" echo -e "${YELLOW}๐Ÿ’ก You can restore from backup using:${NC}" echo "docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $BACKUP_FILE" # Clean up before exit rm -f "$SQL_FILE" "$MV_FILE" exit 1 fi # Show table and materialized view information echo -e "${YELLOW}๐Ÿ“Š Current database tables:${NC}" docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -c "\dt" echo -e "${YELLOW}๐Ÿ“„ Current materialized views:${NC}" docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -c "\dm" # Clean up downloaded SQL files echo -e "${YELLOW}๐Ÿงน Cleaning up...${NC}" rm -f "$SQL_FILE" "$MV_FILE" echo "โœ… Temporary SQL files removed" echo "" echo -e "${GREEN}๐ŸŽ‰ Deployment completed successfully!${NC}" echo "=================================="