创建测试数据库以在工作流程中运行 PHPunit 时 github 操作出现问题

问题描述 投票:0回答:1

我在symfony中有一个项目,我想通过github actions来实现CI,但是当运行数据库时告诉我一个错误,表已经创建,因此无法进行迁移,但之前我已经强制删除它,我不明白这个错误。

我尝试这个.yml

name: Symfony Tests

on:
  push:
    branches: [ "master" ] 
  pull_request:
    branches: [ "master" ]

permissions:
  contents: read

jobs:
  symfony-tests:
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: nurses
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3
    
    steps:
      # Checkout the repository
      - uses: actions/checkout@v4
      
      # Setup PHP environment
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'  # Ajusta la versión según tus necesidades
          extensions: mbstring, intl, pdo_mysql

      # Copy the .env.test.local file if not already present
      - name: Copy .env.test
        run: php -r "file_exists('.env.test.local') || copy('.env.test', '.env.test.local');"

      # Cache Composer packages
      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-

      # Install dependencies
      - name: Install Dependencies
        run: composer install --no-progress --prefer-dist

      # Wait for MySQL to be ready
      - name: Wait for MySQL to be ready
        run: |
          until mysqladmin ping -h"127.0.0.1" --silent; do
            echo "Waiting for database connection..."
            sleep 1
          done
          
      - name: Drop Database if Exists
        run: php bin/console doctrine:database:drop --force --if-exists
        env:
          DATABASE_URL: "mysql://root:[email protected]:3306/nurses"
      
      - name: Create Database
        run: php bin/console doctrine:database:create
        env:
          DATABASE_URL: "mysql://root:[email protected]:3306/nurses"

      - name: Drop Existing Database Schema (force drop all tables)
        run: php bin/console doctrine:schema:drop --force
        env:
          DATABASE_URL: "mysql://root:[email protected]:3306/nurses"
      
      - name: Run Database Migrations
        run: php bin/console doctrine:migrations:migrate
        env:
          DATABASE_URL: "mysql://root:[email protected]:3306/nurses"

      # Run PHPUnit tests
      - name: Execute tests (Unit and Feature tests) via PHPUnit
        env:
          DATABASE_URL: "mysql://root:[email protected]:3306/nurses"
        run: vendor/bin/phpunit

我明白了 Actions image

php mysql symfony github-actions
1个回答
0
投票

错误消息清楚地表明表“护士”已经存在,我看到您已尝试使用:

doctrine:database:drop --force --if-exists

因此,您可以通过运行 SQL 查询来使用更有效的方法来删除表,我建议在执行此操作之前先备份此数据库。

DROP TABLE IF EXISTS nurses;

如果这不起作用,也许您在数据库身份验证中犯了错误,请仔细检查您的凭据和您正在连接的数据库变量。

© www.soinside.com 2019 - 2024. All rights reserved.