Easy peasy database config

This article is also available in the following languages:
By joelmoss
Like a lot of developers out there, I use Subversion to keep control of my code and projects, and I also use a different database for development and production. But when using Cake this can be a problem when checking out my code from development to production. Unless I edit my database.php with my production config, the production code would have problems, as it would be trying to access data from the development database.

What I needed was an easy-peasy way of being able to check in my code to production without having to edit the database.php config file. So what I did was very simple and can be found below.

For a full write up of this trick and other Cake stuff, please see my Blog at http://joelmoss.info
class DATABASE_CONFIG {

    var $development = array(
        'driver' => 'mysql',
        'connect' => 'mysql_connect',
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'passwd',
        'database' => 'app_devel'
    );
    var $production = array(
        'driver' => 'mysql',
        'connect' => 'mysql_connect',
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'passwd',
        'database' => 'app'
    );
    var $test = array(
        'driver' => 'mysql',
        'connect' => 'mysql_connect',
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'passwd',
        'database' => 'app_test'
    );
    var $default = array();

    function __construct()
    {
        $this->default = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ?
            $this->development : $this->production;
    }
    function DATABASE_CONFIG()
    {
        $this->__construct();
    }
}

Comments

  • Posted 05/02/10 10:57:49 AM
    cake bake did not work after this tweak. It was because $_SERVER was not available from CLI. My workaround was:

    function __construct()
    {
      $this->default = (fread(fopen('/etc/hostname', 'r'), 3) == 'dev') ? $this->development : $this->production;
    }
  • Posted 08/17/09 06:35:39 AM
    Well after a little thinking I came up with a solution for server specific configs. This can be used in conjunction with the above, and
    this way you can move around your app to different servers, as long as you have the appropriate config file, like:

    (file: app/config/localconfigs/localhost.php)

    <?php
        
    /**
         * SQL config
         */
        
    Configure::write'sql_host''localhost');
        
    Configure::write'sql_user''user' );
        
    Configure::write'sql_pwd''passwd');
        
    Configure::write'sql_dbname''mydb' );
    ?>

    My database.php looks like this:

    <?php
    class DATABASE_CONFIG {

        var 
    $default = array();
        protected 
    $defaultConfigVal '';

        function  
    __construct() {

            
    $this->default = array(
                
    'driver'     => $this -> getConfigVal'sql_driver''mysql'),
                
    'persistent' => $this -> getConfigVal'sql_persistent'false ),
                
    'host'       => $this -> getConfigVal'sql_host' ),
                
    'port'       => $this -> getConfigVal'sql_port' ),
                
    'login'      => $this -> getConfigVal'sql_user' ),
                
    'password'   => $this -> getConfigVal'sql_pwd' ),
                
    'database'   => $this -> getConfigVal'sql_dbname' ),
                
    'schema'     => $this -> getConfigVal'sql_schema' ),
                
    'prefix'     => $this -> getConfigVal'sql_prefix' ),
                
    'encoding'   => $this -> getConfigVal'sql_encoding' ),
            );
        }

        protected function 
    getConfigVal$configName$defaultConfigVal NULL ) {
            if ( ! 
    is_null$configVal Configure::read$configName ) ) ) {
                return 
    $configVal;
            } else {
                return 
    is_null$defaultConfigVal ) ? $this->defaultConfigVal $defaultConfigVal;
            }
        }
    }
    ?>

    The trick, as you might have guessed:
    make a php file, and require it at (the end of) core.php:

    <?php
        $configsDir    
    APP 'config' DS 'localconfigs';
        
    $defaultConfig $configsDir DS 'localhost.php';
        
    $configFile    $configsDir DS $_SERVER'HTTP_HOST' ] . '.php';

        if ( 
    file_exists$configFile ) ){
            include_once( 
    $configFile );
        } else {
            include_once( 
    $defaultConfig );
        }
    ?>
  • Posted 05/30/08 09:42:49 AM
    Or maybe match against the http_host like this:

    stristr($_SERVER['HTTP_HOST'],$live_server_url) === false
    Where the $live_server_url is, duh, the live server url :)
  • Posted 05/26/08 03:40:35 PM
    Thanks, I needed an easy way for doing this. Instead of testing using the SERVER_ADDR value, I'm probing the file system for the existence of a file that only resides on the DEV server (outside my app tree so I don't go uploading it accidentally). Within this file, I have DEV-specifig configs.

    Unfortunately, I'm repeating this "DEV server check" in /app/webroot/index.php and /app/webroot/test.php and /app/config/database.php because I couldn't figure out a way to set it up so that my whole application AND cake console receive my configuration. Not very DRY.
  • Posted 01/17/07 05:05:47 AM
    Pretty cool, but ... that's what svn:ignore is for :p

Comments are closed for articles over a year old