'Problem to create two databases with doctrine in Symfony

I'm working on a Symfony project, using doctrine as ORM with MariaDB as a driver.

To test my different job, I'm working with gitlab-runner in local on MacOs.

Problem

I create a job to test the creation of my different databases and the migrations for both.

I'm using the symfony-cli command line to create my databases from the doctrine's file configuration (see in the section Source File)

The command line to create a database with symfony-cli

$ php bin/console doctrine:database:create --env=test --if-not-exists --connection=[CONNECTION_NAME]

The error prompt when the command line from is asked to run

$ php bin/console doctrine:database:create --env=test --if-not-exists --connection=CleanerFuture
Could not create database `cf_CleanerFuture` for connection named CleanerFuture
An exception occurred while executing 'CREATE DATABASE `cf_CleanerFuture`':

SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'myapptest'@'%' to database 'cf_CleanerFuture'

Goal

Well my goal it's to try to create the database base with this command line to run my job and past to the next one

Source File

.gitlab-ci.yml

This is the job I try to execute

doctrine-migrations:
  image: php:7.3
  stage: Migrations
  services:
    - name: mysql:5.7
      alias: mysql
  variables:
    ENV: test
    MYSQL_ROOT_PASSWORD: pass_test
    MYSQL_DATABASE: cf_Central
    MYSQL_USER: myapptest
    MYSQL_PASSWORD: myapptest
    DATABASE_URL: 'mysql://myapptest:myapptest@mysql:3306/'
  before_script:
    - apt-get update
    - apt-get install -y git libzip-dev
    - curl -sSk https://getcomposer.org/installer | php -- --disable-tls && mv composer.phar /usr/local/bin/composer
    - docker-php-ext-install mysqli pdo pdo_mysql zip
    - curl -sS https://get.symfony.com/cli/installer | bash
    - mv /root/.symfony/bin/symfony /usr/local/bin/symfony
    - composer remove ext-xdebug
    - composer install
  script:
    - php bin/console doctrine:database:drop --force --if-exists --env=test --connection=default
    - php bin/console doctrine:database:drop --force --if-exists --env=test --connection=CleanerFuture
    - php bin/console doctrine:database:create --env=test --if-not-exists --connection=CleanerFuture
    - php bin/console doctrine:migrations:migrate --env=test
  allow_failure: false

config/test/doctrine.yaml

doctrine:
  dbal:
    default_connection: default
    connections:
      default:
        url: '%env(resolve:DATABASE_URL)%cf_central'
        server_version: "mariadb-10.4.11"
        driver: 'pdo_mysql'
        charset: utf8
      CleanerFuture:
        url: '%env(resolve:DATABASE_URL)%cf_CleanerFuture'
        server_version: "mariadb-10.4.11"
        driver: 'pdo_mysql'
        charset: utf8
  orm:
    auto_generate_proxy_classes: true
    default_entity_manager: default
    entity_managers:
      default:
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        connection: default
        mappings:
          Central:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Entity/Central'
            prefix: 'App\Entity\Central'
      CleanerFuture:
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        connection: CleanerFuture
        mappings:
          Client:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Entity/Client'
            prefix: 'App\Entity\Client'


Solution 1:[1]

url: '%env(resolve:DATABASE_URL)%cf_CleanerFuture' in your config/test/doctrine.yaml file:

The part cf_CleanerFuture will be added to the string value of the variable DATABASE_URL which is defined in your .env file.

If your .env file value for the datbase connection contains something like

DATABASE_URL="mysql://symfony:symfony@database:3306/symfony?serverVersion=8.0"

the processed content of your config will be

doctrine:
  dbal:
    default_connection: default
    connections:
      default:
        url: 'mysql://symfony:symfony@database:3306/symfony?serverVersion=8.0cf_central'

'mysql://symfony:symfony@database:3306/symfony?serverVersion=8.0cf_central' seems to be valid (doctrine can connect and the parameter serverVersion will be ignored if not usable), but the value of the parameter serverVersion is now 8.0cf_central. That makes no sense, is not a valid version number and you haven't configured a different database connection.

It is more recommended to use two different environment variables and store the whole connection URL in these variables:

# config/packages/doctrine.yaml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                # configure these for your database server
                url: '%env(resolve:DATABASE_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
            customer:
                # configure these for your database server
                url: '%env(resolve:DATABASE_CUSTOMER_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4

See also the Symfony documentation which describes how to configure multiple database connections: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anton