diff --git a/scripts/deploy/scripts/run_all_migrations.sh b/scripts/archive/run_all_migrations.sh similarity index 100% rename from scripts/deploy/scripts/run_all_migrations.sh rename to scripts/archive/run_all_migrations.sh diff --git a/scripts/deploy/deploy-prod.sh b/scripts/deploy/deploy-prod.sh index a208c82e..13589f14 100755 --- a/scripts/deploy/deploy-prod.sh +++ b/scripts/deploy/deploy-prod.sh @@ -1,13 +1,17 @@ #!/bin/bash # Deploy production environment for cmc-sales -# Usage: ./deploy-prod.sh [--no-cache] +# Usage: ./deploy-prod.sh [--no-cache] [-m|--migrate] USE_CACHE=true +RUN_MIGRATIONS=false for arg in "$@"; do if [[ "$arg" == "--no-cache" ]]; then USE_CACHE=false echo "No cache flag detected: will rebuild images without cache." + elif [[ "$arg" == "-m" || "$arg" == "--migrate" ]]; then + RUN_MIGRATIONS=true + echo "Migration flag detected: will run database migrations." fi done if [[ "$USE_CACHE" == "true" ]]; then @@ -85,3 +89,13 @@ ENVEOF echo "Checking running containers..." echo "Production deployment complete." ENDSSH + +if [[ "$RUN_MIGRATIONS" == "true" ]]; then + echo "Running database migrations..." + bash scripts/deploy/scripts/run_migrations.sh prod + if [ $? -eq 0 ]; then + echo "Migrations completed successfully." + else + echo "WARNING: Migration failed. Check logs for details." + fi +fi diff --git a/scripts/deploy/deploy-stg.sh b/scripts/deploy/deploy-stg.sh index a889e695..7a602138 100755 --- a/scripts/deploy/deploy-stg.sh +++ b/scripts/deploy/deploy-stg.sh @@ -1,13 +1,17 @@ #!/bin/bash # Deploy staging environment for cmc-sales -# Usage: ./deploy-stg.sh [--no-cache] +# Usage: ./deploy-stg.sh [--no-cache] [-m|--migrate] USE_CACHE=true +RUN_MIGRATIONS=false for arg in "$@"; do if [[ "$arg" == "--no-cache" ]]; then USE_CACHE=false echo "No cache flag detected: will rebuild images without cache." + elif [[ "$arg" == "-m" || "$arg" == "--migrate" ]]; then + RUN_MIGRATIONS=true + echo "Migration flag detected: will run database migrations." fi done if [[ "$USE_CACHE" == "true" ]]; then @@ -76,3 +80,13 @@ ENVEOF echo "Checking running containers..." echo "Deployment complete." ENDSSH + +if [[ "$RUN_MIGRATIONS" == "true" ]]; then + echo "Running database migrations..." + bash scripts/deploy/scripts/run_migrations.sh stg + if [ $? -eq 0 ]; then + echo "Migrations completed successfully." + else + echo "WARNING: Migration failed. Check logs for details." + fi +fi diff --git a/scripts/deploy/scripts/run_migrations.sh b/scripts/deploy/scripts/run_migrations.sh new file mode 100755 index 00000000..f9a28ae0 --- /dev/null +++ b/scripts/deploy/scripts/run_migrations.sh @@ -0,0 +1,51 @@ +#!/bin/bash +set -e + +# Usage: ./run_migrations.sh [stg|prod] +# Default to staging +TARGET="${1:-stg}" + +if [[ "$TARGET" != "stg" && "$TARGET" != "prod" ]]; then + echo "ERROR: Target must be 'stg' or 'prod'" + echo "Usage: ./run_migrations.sh [stg|prod]" + exit 1 +fi + +if [[ "$TARGET" == "prod" ]]; then + COMPOSE_FILE="docker-compose.prod.yml" + ENV_FILE="--env-file .env.prod" + WORK_DIR="cmc-sales-prod" +else + COMPOSE_FILE="docker-compose.stg.yml" + ENV_FILE="" + WORK_DIR="cmc-sales-staging" +fi + +echo "Running goose migrations for $TARGET environment..." + +SERVER="cmc" +ssh $SERVER \ + "COMPOSE_FILE='$COMPOSE_FILE' ENV_FILE='$ENV_FILE' WORK_DIR='$WORK_DIR' bash -s" << 'ENDSSH' + set -e + cd /home/cmc/$WORK_DIR + echo "Running migrations in $WORK_DIR..." + + # Wait for Go service to be ready + echo "Waiting for Go service to be ready..." + for i in {1..30}; do + if docker compose $ENV_FILE -f "$COMPOSE_FILE" exec -T go test -f /app/go.mod 2>/dev/null; then + echo "Go service is ready." + break + fi + if [ $i -eq 30 ]; then + echo "ERROR: Go service is not running or not ready." + exit 1 + fi + sleep 2 + done + + docker compose $ENV_FILE -f "$COMPOSE_FILE" exec -T go sh -c "cd /app && make migrate" + echo "Migrations completed successfully." +ENDSSH + +echo "Done."