63 lines
1.5 KiB
Bash
Executable file
63 lines
1.5 KiB
Bash
Executable file
#!/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
|
|
|
|
# Check if goose is installed
|
|
if ! command -v goose &> /dev/null; then
|
|
echo "ERROR: goose is not installed."
|
|
echo "Install with: go install github.com/pressly/goose/v3/cmd/goose@latest"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Running goose migrations for $TARGET environment..."
|
|
|
|
# Set up SSH tunnel to database
|
|
if [[ "$TARGET" == "prod" ]]; then
|
|
DB_PORT=3307
|
|
SSH_PORT=13307
|
|
else
|
|
DB_PORT=3308
|
|
SSH_PORT=13308
|
|
fi
|
|
|
|
# Create SSH tunnel if not already running
|
|
if ! lsof -ti:$SSH_PORT > /dev/null 2>&1; then
|
|
echo "Creating SSH tunnel to $TARGET database on localhost:$SSH_PORT..."
|
|
ssh -f -N -L $SSH_PORT:localhost:$DB_PORT cmc
|
|
sleep 2
|
|
else
|
|
echo "SSH tunnel already exists on localhost:$SSH_PORT, reusing it..."
|
|
fi
|
|
|
|
# Load goose.env and override the port
|
|
cd go
|
|
if [ -f "goose.env" ]; then
|
|
echo "Loading configuration from goose.env..."
|
|
export $(grep -v '^#' goose.env | xargs)
|
|
# Override the port in GOOSE_DBSTRING
|
|
export GOOSE_DBSTRING=$(echo "$GOOSE_DBSTRING" | sed "s/:3306/:$SSH_PORT/")
|
|
else
|
|
echo "ERROR: goose.env file not found"
|
|
pkill -f "ssh.*-L $SSH_PORT:localhost:$DB_PORT" || true
|
|
exit 1
|
|
fi
|
|
|
|
# Run goose migrations
|
|
echo "Running migrations from $GOOSE_MIGRATION_DIR..."
|
|
goose up
|
|
|
|
# Close SSH tunnel
|
|
echo "Closing SSH tunnel..."
|
|
pkill -f "ssh.*-L $SSH_PORT:localhost:$DB_PORT" || true
|
|
|
|
echo "Done."
|