Vojta Hejda Vojta Hejda - 6 months ago 32
SQL Question

Symfony 2.7 application without created database

I am experiencing some very odd behavior with my Symfony2 application using Doctrine with PDO_mysql:

After cloning it from repository, I need to install it via composer, without the database being already created. I want to create the database later with

app/console doctrine:database:create
command, with parameters specified in parameters.yml during installation.

But when I run
composer install
, I get this error on composer post-install-cmd "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache":

[PDOException] SQLSTATE[42000] [1049] Unknown database 'test'


First I thought this is a duplicate of this issue. But specifying
server_version
in doctrine connection config in app/config/config.yml doesn't help. Also, other solutions I have found advised to revert to earlier version of
doctrine/dbal
, like 2.4.4 or 2.4.3. I tried any combination of
doctrine/dbal
,
doctrine/orm
and
doctrine/doctrine-bundle
versions, but the behavior didn't change.

After that, I noticed that running commands that throw this error repeatedly work. E.g. when I run
app/console doctrine:database:create
on a freshly installed copy (I temporarily deleted those post-install scripts, that were throwing said error) first time, it throws this error. When I run it immediately again, it creates it succesfully:

Created database `test` for connection named default


It happens also with completely unrelated console commands. When I run
app/console
to show me available commands for the first time, I get unknown database 'test'. When I run it immediately again, everything is OK and after that it even lets me create the database with
database:create
command.

To me this looks like something needs to be initialized after installation first, in order for the pdo_mysql connection to work properly. But what is it? I am using composer.json setup similar to the Symfony Standard Edition. But no matter what I try, the result is the same... After 2 days, I am really desperate now.

Note 1: If I create the database manually before specifying it's name in parameters.yml, everything works as expected. But this is not an option for me. I need to be able to run
composer install
with all post-install-cmds before I create the DB.


Note 2: I am trying all this on localhost Linux machine with manually installed MySQL AND at the same time on Windows machine with xampp stack using MariaDB. The behavior is the same on both...

Answer

I called some of the problematic commands with verbose and I came up with an exception trace, containing this:

Doctrine\DBAL\Schema\AbstractSchemaManager->tablesExist() 
    at vendor/lexik/translation-bundle/Lexik/Bundle/TranslationBundle/Storage/DoctrineORMStorage.php:28

Lexik\Bundle\TranslationBundle\Storage\DoctrineORMStorage->translationsTablesExist() 
    at vendor/lexik/translation-bundle/Lexik/Bundle/TranslationBundle/EventDispatcher/GetDatabaseResourcesListener.php:41

So I checked LexikTranslationBundle and found this issue, fixed by this commit. Unfortunately for me, the fix is only merged into version 4, which requires Symfony 2.8. I proposed merging into other versions too, but in a meantime I ended up forking the bundle and fixing it for myself. Now everything works as expected.