name: Test Database Migrations

on:
  push:
    branches:
      - main
    paths:
      - "server/migrations/**"
      - "server/reflector/db/**"
      - "server/alembic.ini"
      - ".github/workflows/db_migrations.yml"
  pull_request:
    paths:
      - "server/migrations/**"
      - "server/reflector/db/**"
      - "server/alembic.ini"
      - ".github/workflows/db_migrations.yml"

jobs:
  test-migrations:
    runs-on: ubuntu-latest
    concurrency:
      group: db-ubuntu-latest-${{ github.ref }}
      cancel-in-progress: true
    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_USER: reflector
          POSTGRES_PASSWORD: reflector
          POSTGRES_DB: reflector
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready -h 127.0.0.1 -p 5432
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    env:
      DATABASE_URL: postgresql://reflector:reflector@localhost:5432/reflector

    steps:
      - uses: actions/checkout@v4

      - name: Install PostgreSQL client
        run: sudo apt-get update && sudo apt-get install -y postgresql-client | cat

      - name: Wait for Postgres
        run: |
          for i in {1..30}; do
            if pg_isready -h localhost -p 5432; then
              echo "Postgres is ready"
              break
            fi
            echo "Waiting for Postgres... ($i)" && sleep 1
          done

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true
          working-directory: server

      - name: Test migrations from scratch
        working-directory: server
        run: |
          echo "Testing migrations from clean database..."
          uv run alembic upgrade head
          echo "✅ Fresh migration successful"

      - name: Test migration rollback and re-apply
        working-directory: server
        run: |
          echo "Testing rollback to base..."
          uv run alembic downgrade base
          echo "✅ Rollback successful"

          echo "Testing re-apply of all migrations..."
          uv run alembic upgrade head
          echo "✅ Re-apply successful"
