Hide
on 15/9/06

If you have your website on one server for testing/development (like this laptop) and your live site on another, it's quite likely that once in a while your database.php file from one or the other will find itself referring to the wrong database. Over at With Cake a solution was proposed to allow you to switch database sources in the app model, however it might be easier to manage by switching at the source of the 'problem'.

With a modification to your database.php file like so:

  1. <?php
  2. class DATABASE_CONFIG
  3. {
  4. var $default = NULL;
  5. var $live = array('driver' => 'mysql',
  6. 'connect' =>
  7. 'mysql_pconnect',
  8. 'host' => 'localhost',
  9. 'login' =>
  10. 'liveusername',
  11. 'password' => 'livepassword',
  12. 'database' =>
  13. 'livedatabase',
  14. 'prefix' => '',
  15. 'encoding' => 'UTF8');//
  16. only takes effect in cake 1.2
  17. var $local = array('driver' => 'mysql',
  18. 'connect'
  19. => 'mysql_pconnect',
  20. 'host' => 'localhost',
  21. 'login' =>
  22. 'localmachinelogin',
  23. 'password' => 'localpassword',
  24. 'database'
  25. => 'localdatabase',
  26. 'prefix' => '',
  27. 'encoding' =>
  28. 'UTF8');// only takes effect in cake 1.2
  29. function DATABASE_CONFIG() { // For Php5 use __construct
  30. if (strpos( $_SERVER['SERVER_NAME'], 'dev')!==false) {
  31. $this->default = $this->live;
  32. } else {
  33. $this->default = $this->local;
  34. }
  35. }
  36. }
  37. ?>

The file itself can be moved while syncing the files for your site, without the risk of suddently bringing your live site offline when it can't find the database. Obviously this tip is redundant if you set up your file updates such that the database file is ignored.

This approach can easily be extended to allow switching of database sources for quick testing and the likes, which I might mention in my next post.

5 Responses to One Database file - two servers

  1. 1
    I use a different approach. In my repository I have two trees: a development tree and a "live" tree. On the live server I just export the live tree with the configurations for the live server, so there is no need to "hack" the config files ;-)
  2. 2
    Hi Daniel,
    Kind of amusing that everyone decided to post a note on this topic at the same time ;). As I alluded to in the post, if you are not transferring things manually - the above doesn't apply. I'm not an expert in the use of svn and I don't quite follow what your note means though - do you maintain two trees on your development machine, and periodially syncronize them?
  3. 3
    It is a similar approach as is used for developing cake. The development happens in the trunk, and from time to time there is a release, i.e. the files from trunk (with the exception of configuration files) are copied to the "live" tree. On the live server I only use the files available in that tree.
  4. 4
    Hi Daniel,
    There is something I am clearly missing from your explenation ;). How are you copying from your trunk to your live tree? I've used CASE tools in the past, and avoided such issues by preventing the possibility to publish and overwrite certain resources - but if the copy from one tree to the other is a manual step all that using 2 trees achieves is move the problem (of potentially overwriting) to a different place - I assume I am missing somthing rather obvious (such as making the files read-only??), please could you clarify?
  5. 5
    Well, there is a potential risk of overwriting files, but it is rather low, because it is a two-step process to put a file in the live tree. The first step is to select and copy the files from trunk to the working copy of the live tree. And the second step is to commit those changes. So in my opinion it is almost impossible to overwrite some config files ;-)