Adding new migrations script supporting goose

This commit is contained in:
Finley Ghosh 2025-12-07 15:53:41 +11:00
parent b0a09c159d
commit f6bbfb3c83
4 changed files with 81 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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."