phpherokudrush

Drush does not recognize an env variable with Heroku


In a PHP / Drupal project I'm encountering a problem when using Drush in a Heroku instance. The environment variable DATABASE_URL in the settings.php file is not recognized by Drush.

However, when I use the command heroku config -a my-app -s the DATABASE_URL is visible. Same with phpinfo().

I try to use Procfile like:

web: export DATABASE_URL=$DATABASE_URL && heroku-php-apache2 web/

but that doesn't work, either.

The only way to get Drush to recognize the variable is to execute export DATABASE_URL="mysql://..." when I am on the instance, but it's not persistent:

heroku ps:exec -a my-app
export DATABASE_URL="mysql://..."
drush status

Any suggestions?

settings.php:

$db_cred = parse_url(getenv('DATABASE_URL'));
$databases['default']['default'] = array (
  'database' => ltrim($db_cred['path'], '/'),
  'username' => $db_cred['user'],
  'password' => $db_cred['pass'],
  'prefix' => '',
  'host' => $db_cred['host'],
  'port' => $db_cred['port'],
  'isolation_level' => 'READ COMMITTED',
  'namespace' => 'Drupal\\mysql\\Driver\\Database\\mysql',
  'driver' => $db_cred['scheme'],
  'autoload' => 'core/modules/mysql/src/Driver/Database/mysql/',
);

Solution

  • This is by design:

    The SSH session created by Heroku Exec doesn’t have the config vars set as environment variables (i.e., env in a session doesn’t list config vars set by heroku config:set).

    I suggest you connect using heroku run instead:

    heroku run bash -a my-app
    drush status
    

    The main difference between heroku ps:exec and heroku run is that the former connects to a running dyno and the latter spins up a one-off dyno.

    You should also roll back that change to your Procfile. There's no point in re-exporting an environment variable like that, and it may be confusing later.

    Finally, note that the value of DATABASE_URL can change at any time. Even if you were able to export its current value in a persistent way, that connection string might not work tomorrow. It's better to read the variable every time.


    Side note: It doesn't look like drush status is a valid command. I'm not a Drupal guy so can't help much beyond pointing that out. Maybe you're looking for core:status, config:status, or another similarly-named command?