Phinx: Support .env environment variables...

Created on 26 May 2016  路  3Comments  路  Source: cakephp/phinx

It would be nice if this would support using .env files for setting database passwords and such.

I worked around this limitation by creating a wrapper in my db directory using vlucas/dotenv to call phinx like so...

require_once __DIR__.'/../vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
$dotenv->load();

require __DIR__ . '/../vendor/bin/phinx';

Most helpful comment

If you use phinx.php as your config file, rather than phinx.yml, you really don't need a wrapper.

<?php

// load our environment files - used to store credentials & configuration
(new Dotenv\Dotenv(__DIR__))->load();

return
    [
        'paths' => [
            'migrations' => '%%PHINX_CONFIG_DIR%%/migrations',
        ],
        'environments' =>
            [
                'default_database' => '*** CHOOSE AN ENVIRONMENT ***',
                'default_migration_table' => 'phinxlog',
                'development'      =>
                    [
                        'adapter' => 'mysql',
                        'host' => $_ENV['DB_DEVELOPMENT_HOST'],
                        'name' => $_ENV['DB_DEVELOPMENT_DATABASE'],
                        'user' => $_ENV['DB_DEVELOPMENT_USERNAME'],
                        'pass' => $_ENV['DB_DEVELOPMENT_PASSWORD'],
                        'port' => 3306,
                        'charset' => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                    ],
                'staging' =>
                    [
                        'adapter' => 'mysql',
                        'host' => $_ENV['DB_STAGING_HOST'],
                        'name' => $_ENV['DB_STAGING_DATABASE'],
                        'user' => $_ENV['DB_STAGING_USERNAME'],
                        'pass' => $_ENV['DB_STAGING_PASSWORD'],
                        'port' => 3306,
                        'charset' => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                    ],
                'production' =>
                    [
                        'adapter' => 'mysql',
                        'host' => $_ENV['DB_PRODUCTION_HOST'],
                        'name' => $_ENV['DB_PRODUCTION_DATABASE'],
                        'user' => $_ENV['DB_PRODUCTION_USERNAME'],
                        'pass' => $_ENV['DB_PRODUCTION_PASSWORD'],
                        'port' => 3306,
                        'charset' => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                    ],
            ],
    ];

All 3 comments

If you use phinx.php as your config file, rather than phinx.yml, you really don't need a wrapper.

<?php

// load our environment files - used to store credentials & configuration
(new Dotenv\Dotenv(__DIR__))->load();

return
    [
        'paths' => [
            'migrations' => '%%PHINX_CONFIG_DIR%%/migrations',
        ],
        'environments' =>
            [
                'default_database' => '*** CHOOSE AN ENVIRONMENT ***',
                'default_migration_table' => 'phinxlog',
                'development'      =>
                    [
                        'adapter' => 'mysql',
                        'host' => $_ENV['DB_DEVELOPMENT_HOST'],
                        'name' => $_ENV['DB_DEVELOPMENT_DATABASE'],
                        'user' => $_ENV['DB_DEVELOPMENT_USERNAME'],
                        'pass' => $_ENV['DB_DEVELOPMENT_PASSWORD'],
                        'port' => 3306,
                        'charset' => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                    ],
                'staging' =>
                    [
                        'adapter' => 'mysql',
                        'host' => $_ENV['DB_STAGING_HOST'],
                        'name' => $_ENV['DB_STAGING_DATABASE'],
                        'user' => $_ENV['DB_STAGING_USERNAME'],
                        'pass' => $_ENV['DB_STAGING_PASSWORD'],
                        'port' => 3306,
                        'charset' => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                    ],
                'production' =>
                    [
                        'adapter' => 'mysql',
                        'host' => $_ENV['DB_PRODUCTION_HOST'],
                        'name' => $_ENV['DB_PRODUCTION_DATABASE'],
                        'user' => $_ENV['DB_PRODUCTION_USERNAME'],
                        'pass' => $_ENV['DB_PRODUCTION_PASSWORD'],
                        'port' => 3306,
                        'charset' => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                    ],
            ],
    ];

@rquadling is right. According to the docs, you can use either an YML or an PHP file as a configuration file.

Was this page helpful?
0 / 5 - 0 ratings