name: DB Migration Test

on:
  workflow_call:

concurrency:
  group: db-migration-test-${{ github.ref }}
  cancel-in-progress: true

jobs:
  db-migration-test-postgres:
    runs-on: depot-ubuntu-24.04

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup UV and Python
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
        with:
          enable-cache: true
          python-version: "3.12"
          cache-dependency-glob: api/uv.lock

      - name: Install dependencies
        run: uv sync --project api
      - name: Ensure Offline migration are supported
        run: |
          # upgrade
          uv run --directory api flask db upgrade 'base:head' --sql
          # downgrade
          uv run --directory api flask db downgrade 'head:base' --sql

      - name: Prepare middleware env
        run: |
          cd docker
          cp envs/middleware.env.example middleware.env

      - name: Set up Middlewares
        uses: hoverkraft-tech/compose-action@d2bee4f07e8ca410d6b196d00f90c12e7d48c33a # v2.6.0
        with:
          compose-file: |
            docker/docker-compose.middleware.yaml
          services: |
            db_postgres
            redis

      - name: Prepare configs
        run: |
          cd api
          cp .env.example .env

      - name: Run DB Migration
        env:
          DEBUG: true
        run: uv run --directory api flask upgrade-db

  db-migration-test-mysql:
    runs-on: depot-ubuntu-24.04

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup UV and Python
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
        with:
          enable-cache: true
          python-version: "3.12"
          cache-dependency-glob: api/uv.lock

      - name: Install dependencies
        run: uv sync --project api
      - name: Ensure Offline migration are supported
        run: |
          # upgrade
          uv run --directory api flask db upgrade 'base:head' --sql
          # downgrade
          uv run --directory api flask db downgrade 'head:base' --sql

      - name: Prepare middleware env for MySQL
        run: |
          cd docker
          cp envs/middleware.env.example middleware.env
          sed -i 's/DB_TYPE=postgresql/DB_TYPE=mysql/' middleware.env
          sed -i 's/DB_HOST=db_postgres/DB_HOST=db_mysql/' middleware.env
          sed -i 's/DB_PORT=5432/DB_PORT=3306/' middleware.env
          sed -i 's/DB_USERNAME=postgres/DB_USERNAME=mysql/' middleware.env

      - name: Set up Middlewares
        uses: hoverkraft-tech/compose-action@d2bee4f07e8ca410d6b196d00f90c12e7d48c33a # v2.6.0
        with:
          compose-file: |
            docker/docker-compose.middleware.yaml
          services: |
            db_mysql
            redis

      - name: Prepare configs for MySQL
        run: |
          cd api
          cp .env.example .env
          sed -i 's/DB_TYPE=postgresql/DB_TYPE=mysql/' .env
          sed -i 's/DB_PORT=5432/DB_PORT=3306/' .env
          sed -i 's/DB_USERNAME=postgres/DB_USERNAME=root/' .env

      # hoverkraft-tech/compose-action@v2.6.0 only waits for `docker compose up -d`
      # to return (container processes started); it does not wait on healthcheck
      # status. mysql:8.0's first-time init takes 15-30s, so without an explicit
      # wait the migration runs while InnoDB is still initialising and gets
      # killed with "Lost connection during query". Poll a real SELECT until it
      # succeeds.
      - name: Wait for MySQL to accept queries
        run: |
          set +e
          for i in $(seq 1 60); do
            if docker run --rm --network host mysql:8.0 \
                mysql -h 127.0.0.1 -P 3306 -uroot -pdifyai123456 \
                -e 'SELECT 1' >/dev/null 2>&1; then
              echo "MySQL ready after ${i}s"
              exit 0
            fi
            sleep 1
          done
          echo "MySQL not ready after 60s; dumping container logs:"
          docker compose -f docker/docker-compose.middleware.yaml --profile mysql logs --tail=200 db_mysql
          exit 1

      - name: Run DB Migration
        env:
          DEBUG: true
        run: uv run --directory api flask upgrade-db
