Raja's Exocortex

Laravel Postgres DB Migration Test using Pest

Laravel code testing tools like pint and larastan do not cover database migrations. This workflow sets up a PostgreSQL service and uses Pest to verify that all DB migrations run successfully.

Filename: laravel-pg-migration-test.yaml

# Filename: .github/workflows/laravel-pg-migration-test.yaml

name: Test Laravel Postgres DB Migrations using Pest

on:
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  pest-db-migration-test:
    runs-on: ubuntu-latest
    name: Pest DB Migration Test

    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_DB: test
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
        ports:
          - 5432:5432
        healthcheck:
          test: ["CMD-SHELL", "pg_isready -U test -d test"]
          start-period: 10s
          interval: 30s
          timeout: 5s
          retries: 3

    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: "8.3"
          extensions: pgsql
          coverage: none

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

      - name: Cache Composer dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Install Composer dependencies
        run: composer install --prefer-dist --no-interaction --quiet

      - name: Generate .env and configure for Postgres
        run: |
          cp .env.example .env
          php artisan key:generate
          sed -i "s/DB_CONNECTION=.*/DB_CONNECTION=pgsql/" .env
          sed -i "s/DB_HOST=.*/DB_HOST=postgres/" .env
          sed -i "s/DB_PORT=.*/DB_PORT=5432/" .env
          sed -i "s/DB_DATABASE=.*/DB_DATABASE=test/" .env
          sed -i "s/DB_USERNAME=.*/DB_USERNAME=test/" .env
          sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=test/" .env

      - name: Run Pest for migrations
        run: ./vendor/bin/pest --group=migrations --colors

Depends On

  1. Self hosted Gitea gitea
  2. Self hosted ACT Runners gitea-act-runner