LoginSignup
0
1

More than 5 years have passed since last update.

Herokuのデータベースを本番とテストで切り替える

Posted at

データベースの接続を自動化

getenvでHerokuのアプリと連携しているデータベースの接続情報が取得できるため、もし環境変数がない場合は、実行環境がローカルになる。


if (getenv('DATABASE_URL')) {

    // heroku
    $dbUrl = parse_url(getenv('DATABASE_URL'));
    $dbName = ltrim($dbUrl['path'], '/');
    $dbHost = $dbUrl['host'];
    $dbUser = $dbUrl['user'];
    $dbPass = $dbUrl['pass'];
} else {

    // macbook
    $dbHost = "localhost";
    $dbName = "test";
    $dbUser = "user";
    $dbPass = "password";
}

ORM::configure("pgsql:host={$dbHost};dbname={$dbName}");
ORM::configure('username', $dbUser);
ORM::configure('password', $dbPass);

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1