#!/usr/bin/env bash
# Run Alembic migrations in a one-off container
set -euo pipefail

REGION="${AWS_REGION:-ap-southeast-1}"
IMAGE_TAG="${1:-latest}"

ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
ECR_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com"
IMAGE="${ECR_REGISTRY}/helix-server:${IMAGE_TAG}"

echo "Running Alembic migrations (image: ${IMAGE})..."
docker run --rm \
  --env-file /opt/helix/env/.env.production \
  --network host \
  "$IMAGE" \
  /app/.venv/bin/alembic upgrade head

echo "Migrations complete."
