Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to connect into mysql database when database not created #470

Closed
webmake opened this issue Nov 29, 2018 · 14 comments
Closed

Fails to connect into mysql database when database not created #470

webmake opened this issue Nov 29, 2018 · 14 comments
Assignees
Labels
move-to-fixtures-repository May need to be moved to https://github.com/liip/LiipTestFixturesBundle
Milestone

Comments

@webmake
Copy link

webmake commented Nov 29, 2018

Hello,

This library fails to connect into mysql database when database not created with error An exception occurred in driver: SQLSTATE[HY000] [1049] Unknown database 'test'

Place where error occurs:
https://github.com/liip/LiipFunctionalTestBundle/blob/2.0.0-alpha10/src/Services/DatabaseTools/ORMDatabaseTool.php#L63 or commit to be exact fails because used dbal library performs connect https://github.com/doctrine/dbal/blob/b45ed5e13e4834f64fbc56bfdc0344430cecae63/lib/Doctrine/DBAL/Connection.php#L358 into database despite unset($params['dbname']); in your class ORMDatabaseTool in 61 line

Steps to reproduce

class MyClassTest extends \Liip\FunctionalTestBundle\Test\WebTestCase
{
    protected function setUp(): void
    {
        self::bootKernel();
        $this->loadFixtures([
            LoadEntities::class,
        ]);
        parent::setUp();
    }
}

and

"doctrine/dbal": "^2.8",
"liip/functional-test-bundle": "~2.0@alpha",
@alexislefebvre
Copy link
Collaborator

alexislefebvre commented Nov 29, 2018

As a workaround, you can create the database before running tests:

(app|bin)/console doctrine:database:create --env=test --if-not-exists

@alexislefebvre alexislefebvre added this to the 2.0 milestone Nov 29, 2018
@alexislefebvre
Copy link
Collaborator

Can you try to call self::bootKernel(); after $this->loadFixtures([]);?

@alexislefebvre
Copy link
Collaborator

I can't reproduce this bug on my local environment.

Do you use driver: pdo_mysql in your configuration?

@webmake
Copy link
Author

webmake commented Dec 18, 2018

I tried to your method to switch bootKernel and loadFixtures methods, still same problem. Yes
it is driver: pdo_mysql.

It cannot work at any case, because of such stack trace (please follow):
in test $this->loadFixtures([]);
calls return $dbTool->loadFixtures($classNames, $append); line in Liip\FunctionalTestBundle\Test\WebTestCase:258,
then on $this->createDatabaseIfNotExists(); Liip\FunctionalTestBundle\Services\DatabaseTools\ORMDatabaseTool:96
then $tmpConnection->connect(); Liip\FunctionalTestBundle\Services\DatabaseTools\ORMDatabaseTool:63 here important, you unset $params['dbname']
then $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions); Doctrine\DBAL\Connection:389 But here $this->_params
then $conn = new PDOConnection( $this->constructPdoDsn($params), $username, $password, $driverOptions ); on Doctrine\DBAL\Driver\PDOMySql\Driver:44
and in parent::__construct($dsn, $user, $password, $options); onDoctrine\DBAL\Driver\PDOConnection:46 occurs PDOException, because dsn: mysql:host=127.0.0.1;port=3306;dbname=test;charset=utf8mb4; test database not created, because you create in

        $tmpConnection->connect(); //here FAILS

        if (!in_array($dbName, $tmpConnection->getSchemaManager()->listDatabases())) {
            $tmpConnection->getSchemaManager()->createDatabase($dbName); // here
        }

@alexislefebvre
Copy link
Collaborator

I changed the database name in the configuration used on Travis CI and the tests passed: #477

It looks like a specific configuration leads to the error you have, but I'm still wondering what are these parameters.

@alexislefebvre
Copy link
Collaborator

Could you please share your Doctrine configuration?

@webmake
Copy link
Author

webmake commented Dec 18, 2018

parameters:
    env(DATABASE_URL): ''
    env(DATABASE_SETTINGS_URL): ''

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci

                url: '%env(resolve:DATABASE_URL)%'
            settings:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci

                url: '%env(resolve:DATABASE_SETTINGS_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true
                mappings:
                    App:
                        is_bundle: false
                        type: yml
                        dir: '%kernel.project_dir%/config/doctrine/provider'
                        prefix: 'App\Entity\Provider'
                        alias: App

                    Money:
                        is_bundle: false
                        type: yml
                        dir: '%kernel.project_dir%/config/doctrine-money'
                        prefix: 'Money'
                        alias: Money

            settings:
                connection: settings
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    Settings:
                        is_bundle: false
                        type: yml
                        dir: '%kernel.project_dir%/config/doctrine/settings'
                        prefix: 'App\Entity\Settings'
                        alias: AppSettings

@webmake
Copy link
Author

webmake commented Dec 18, 2018

As I see, your tests are such, that loadfixtures are made in test case, not in setUp, maybe this is why problem not reproducible?

@alexislefebvre
Copy link
Collaborator

Thanks for sharing your configuration.

Yes it may be due to the fact that you load fixtures in setUp, the fact that several databases are configured or the fact that you use url. This is very interesting because the internal tests of this bundle use the simplest configurations, it's probably not enough to cover complex configurations like yours.

@webmake
Copy link
Author

webmake commented Dec 19, 2018

I debugged your WebTestCaseConfigMysqlTest.php, dsn mysql:host=127.0.0.1;charset=UTF8; doesn't contain database name in PDOConnection constructor when performs connect.. That's weird..

@webmake
Copy link
Author

webmake commented Dec 19, 2018

Okay, in my case doesn't work unset as I mentioned, because parseDatabaseUrl again set dbname from url: mysql://admin:admin@127.0.0.1:3306/test

@alexislefebvre
Copy link
Collaborator

Until the bundle supports multiple databases, we can use a workaround to use only one database during tests: https://stackoverflow.com/a/53726916/2257664

@webmake
Copy link
Author

webmake commented Jan 8, 2019

What if unset url and add fragment from doctrine https://github.com/doctrine/dbal/blob/b45ed5e13e4834f64fbc56bfdc0344430cecae63/lib/Doctrine/DBAL/DriverManager.php#L260 which would ignore only dbname like in https://github.com/liip/LiipFunctionalTestBundle/blob/2.0.0-alpha10/src/Services/DatabaseTools/ORMDatabaseTool.php#L61? Or trim path because parseDatabaseUrlPath finds dbname?

@alexislefebvre alexislefebvre added move-to-fixtures-repository May need to be moved to https://github.com/liip/LiipTestFixturesBundle and removed move-to-fixtures-repository May need to be moved to https://github.com/liip/LiipTestFixturesBundle labels May 8, 2019
@alexislefebvre alexislefebvre self-assigned this May 30, 2019
@alexislefebvre
Copy link
Collaborator

See liip/LiipTestFixturesBundle#4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
move-to-fixtures-repository May need to be moved to https://github.com/liip/LiipTestFixturesBundle
Projects
None yet
Development

No branches or pull requests

2 participants