init
This commit is contained in:
91
deploy-db.sh
Normal file
91
deploy-db.sh
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/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_FILE="./db/db.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
|
||||
|
||||
# Check if SQL file exists
|
||||
if [ ! -f "$SQL_FILE" ]; then
|
||||
echo -e "${RED}❌ Error: SQL file '${SQL_FILE}' not found${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}📋 Pre-deployment checks:${NC}"
|
||||
echo "✅ Docker is running"
|
||||
echo "✅ Database container is running"
|
||||
echo "✅ SQL file exists"
|
||||
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 ""
|
||||
|
||||
# Create backup before deployment
|
||||
echo -e "${YELLOW}💾 Creating database backup...${NC}"
|
||||
BACKUP_FILE="backup_$(date +%Y%m%d_%H%M%S).sql"
|
||||
docker exec $DB_CONTAINER pg_dump -U $DB_USER -d $DB_NAME > $BACKUP_FILE
|
||||
echo "✅ Backup created: $BACKUP_FILE"
|
||||
echo ""
|
||||
|
||||
# Deploy the SQL file
|
||||
echo -e "${YELLOW}🔧 Deploying database changes...${NC}"
|
||||
echo "Executing: $SQL_FILE"
|
||||
|
||||
if docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $SQL_FILE; then
|
||||
echo -e "${GREEN}✅ Database deployment completed successfully!${NC}"
|
||||
echo ""
|
||||
|
||||
# Show table information
|
||||
echo -e "${YELLOW}📊 Current database tables:${NC}"
|
||||
docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -c "\dt"
|
||||
|
||||
else
|
||||
echo -e "${RED}❌ Error: Database 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"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo -e "${GREEN}🎉 Deployment completed successfully!${NC}"
|
||||
echo "=================================="
|
Reference in New Issue
Block a user