name: test-migrations

on:
  workflow_dispatch:
  push:
    paths:
      - '**.php'
      - 'composer.*'
  pull_request:
    paths:
      - '**.php'
      - 'composer.*'

jobs:
  build:
    if: ${{ github.ref != 'refs/heads/l10n_development' }}
    runs-on: docker
    container:
      image: docker.io/library/node:24-trixie
    strategy:
      matrix:
        php: ['8.2', '8.3', '8.4', '8.5']
    services:
      mysql:
        image: docker.io/library/mariadb:12.2.2-noble
        options: --tmpfs /var/lib/mysql:rw
        cmd:
          - --innodb-flush-log-at-trx-commit=0
          - --innodb-flush-method=O_DIRECT
          - --innodb-doublewrite=0
          - --innodb-buffer-pool-size=256M
          - --skip-log-bin
          - --sync-binlog=0
        env:
          MARIADB_USER: bookstack-test
          MARIADB_PASSWORD: bookstack-test
          MARIADB_DATABASE: bookstack-test
          MARIADB_ROOT_PASSWORD: password
    steps:
      - uses: https://code.forgejo.org/actions/checkout@v6

      - name: Setup PHP
        uses: https://github.com/shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: gd, mbstring, json, curl, xml, dom, mysql, ldap

      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache composer packages
        uses: https://code.forgejo.org/actions/cache@v5
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ matrix.php }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install composer dependencies
        run: composer install --prefer-dist --no-interaction --ansi
        env:
          COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.GH_TOKEN }}"}}'

      - name: Start migration test
        env:
          TEST_DATABASE_URL: 'mysql://bookstack-test:bookstack-test@mysql/bookstack-test'
        run: |
          php${{ matrix.php }} artisan migrate --force -n --database=mysql_testing

      - name: Start migration:rollback test
        env:
          TEST_DATABASE_URL: 'mysql://bookstack-test:bookstack-test@mysql/bookstack-test'
        run: |
          php${{ matrix.php }} artisan migrate:rollback --force -n --database=mysql_testing

      - name: Start migration rerun test
        env:
          TEST_DATABASE_URL: 'mysql://bookstack-test:bookstack-test@mysql/bookstack-test'
        run: |
          php${{ matrix.php }} artisan migrate --force -n --database=mysql_testing
