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

Support for DATABASE_URL #179

Open
gebi84 opened this issue Jan 6, 2022 · 8 comments
Open

Support for DATABASE_URL #179

gebi84 opened this issue Jan 6, 2022 · 8 comments

Comments

@gebi84
Copy link

gebi84 commented Jan 6, 2022

first thx for the great tool.

I'm using the latest version 1.9.0

When i only set the URL in the .env:
DATABASE_URL=mysql://root:[email protected]/fwgshopmanager_test

I get following error:
[ErrorException] Notice: Undefined index: master

the params array which comes into ConnectionFactory::createConnection looks like so
array:9 [ "url" => "mysql://root:[email protected]/fwgshopmanager_test" "charset" => "UTF8" "host" => "localhost" "port" => null "user" => "root" "password" => null "driver" => "pdo_mysql" "driverOptions" => [] "defaultTableOptions" => [] ]

My suggestion to fix this would be:

if (isset($params['url']) && !isset($params['dbname'])) {
    $connection = DriverManager::getConnection($params, $config, $eventManager);
    $params = $connection->getParams();
    unset($params['url']);
}

if (isset($params['dbname'])) {
    $dbName = $this->getDbNameFromEnv($params['dbname']);
} else {
    $dbName = $this->getDbNameFromEnv($params['master']['dbname']);
}
@DonCallisto
Copy link
Collaborator

Could this have something to do with #159?

@gebi84
Copy link
Author

gebi84 commented Jan 12, 2022

no I don't think so, this also does not handle the url parameter

@mr120
Copy link

mr120 commented Jan 12, 2022

EDIT: Actually this doesn't work.
This could potentially work with a config/packages/dev/doctrine.yaml, and a .env.dev but that is a lot of extra config.
If the global doctrine.yaml uses the 'url', then the test one will use it too which will result in the wrong database being used.
All said, I think the suggestion from gebi84 looks good.

I believe this is the same issue as #101

Original message below:
An alternate solution that uses the connection string would be helpful, however this can be easily overcome with test specific configs.

#config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
#config/packages/test/doctrine.yaml
doctrine:
    dbal:
        driver: '%env(DATABASE_DRIVER)%'
        host: '%env(DATABASE_HOST)%'
        port: '%env(DATABASE_PORT)%'
        dbname: '%env(DATABASE_NAME)%'
        user: '%env(DATABASE_USER)%'
        password: '%env(DATABASE_PASSWORD)%'
        charset: UTF8

Being sure your .env has the required properties

@soulcodex
Copy link

soulcodex commented Jul 4, 2022

Hi guys, is absolutly neccesary support DATABASE_URL param for connection with database, i am not able to establish connection with my database using the params listing like commented us @mr120 :(

@DonCallisto
Copy link
Collaborator

Hi @soulcodex, at the moment I won't be able to tackle this quickly. If you find a solution for this issue, you're welcome to open a PR.

@soulcodex
Copy link

I will start to work with a PR with the Connection Factory based on regex capture for URL approach at least for relational databases like MySQL, PG and SQLITE

See you soon

@soulcodex
Copy link

@DonCallisto take a look at the changes I have for the package using DSN instead of a list of parameters, it only remains to test it and add the approach for SQLite.

https://github.com/soulcodex/fastest/tree/feature/1-add-support-for-database-url/adapters/Doctrine/DBAL/Factory

@DonCallisto
Copy link
Collaborator

Please, produce a diff (for example with a draft PR) and when I'll have time, I'll try to take a look.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants