Phinx: Support dotenv files

Created on 20 May 2015  路  10Comments  路  Source: cakephp/phinx

Support for a dotenv file would be quite nice.

Instead of having the database username password and name put into both .env and phinx.yml, having the phinx.yml file allow lines such as

user: {~/path/to/.env:mysql_user} where the first half is the path to the env file, and the second half is the name of the environment variable.

Either that, or supporting the dotenv project itself and having the .env file loaded into the environment variables, and then allowing {mysql_user} to recognise environment variables would be good.

feature

Most helpful comment

How I implemented what @DerMika is saying. I used -c phinx.php and loaded dotenv in that file, like so:

<?php

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

return [
    'paths' => [
        'migrations' => __DIR__ . '/migrations',
    ],
    'environments' => [
        'default_migration_table' => 'phinxlog',
        'default_database' => getenv('APP_ENV'),
        'develop' => [
            'adapter' => getenv('DB_ADAPTER'),
            'host' => getenv('DB_HOST'),
            'name' => getenv('DB_NAME'),
            'user' => getenv('DB_USER'),
            'pass' => getenv('DB_PASS'),
            'port' => getenv('DB_PORT'),
        ],
    ],
];

That was probably obvious, but if it saves someone some time...

All 10 comments

Here's the library that makes it trivial to support .env files:

https://github.com/vlucas/phpdotenv

Yes, that is the one that I was meaning :)

:+1: Please do!

As many people already use .env files on their projects, and it is something that is becoming increasingly popular (ex: the Laravel framework now also has it), adding support for .env in phinx would simplify integrating phinx into a Laravel application or any other app that uses .env, reducing the _barrier to adoption_ that always happens when a developer needs to summon the courage to try out a new tool.

:+1:

To be fair, there is a workaround. You could use the (badly documented) phinx.php config file and load settings using phpdotenv there.

See https://github.com/robmorgan/phinx/pull/60

How I implemented what @DerMika is saying. I used -c phinx.php and loaded dotenv in that file, like so:

<?php

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

return [
    'paths' => [
        'migrations' => __DIR__ . '/migrations',
    ],
    'environments' => [
        'default_migration_table' => 'phinxlog',
        'default_database' => getenv('APP_ENV'),
        'develop' => [
            'adapter' => getenv('DB_ADAPTER'),
            'host' => getenv('DB_HOST'),
            'name' => getenv('DB_NAME'),
            'user' => getenv('DB_USER'),
            'pass' => getenv('DB_PASS'),
            'port' => getenv('DB_PORT'),
        ],
    ],
];

That was probably obvious, but if it saves someone some time...

Are you able to provide a pull request with your suggested changes?

@dereuromark Sure. I made a quick pass at it. Any particular branch you want me to make a pull request against? Master?

if it is bc master should be fine.

ping

The PR I submitted is here: https://github.com/cakephp/phinx/pull/1236

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robmorgan picture robmorgan  路  20Comments

aimfeld picture aimfeld  路  23Comments

orderbynull picture orderbynull  路  19Comments

jeremylivingston picture jeremylivingston  路  20Comments

alex-barylski picture alex-barylski  路  14Comments