Switch database by Git branch

Public

This code may be use in your settings.php to be able to switch your database regarding the branch you are working on. This is very useful in Drupal when you work on some features and you may have have to switch back on your master branch to work on hotfixes, for example.
With that little trick, your database always match to your codebase.

Tips: You may use a non-existing database name for some branches (example for 'master' down here) to prevent work on a base on which you should not working (to work on master we have to create a hotfix branch).

</> CopyGet raw version
php
  1. /**
  2.  * If you would like to use different databases by Git branch,
  3.  * fill down your credentials by using the complete branche name as key
  4.  */
  5. $databases_by_branch = array(
  6. 'dev' => array (
  7. 'database' => 'myproject_db',
  8. ),
  9. 'master' => array (
  10. 'database' => 'no-database', /* no-database does not exists = no Drupal usage = for Git management only */
  11. ),
  12. 'my-new-feature' => array(
  13. 'database' => 'my_new_feature_db',
  14. ),
  15. 'another-new-feature' => array(
  16. 'database' => 'another_new_feature_db',
  17. ),
  18. );
  19.  
  20. if (file_exists(DRUPAL_ROOT .'/sites/default/settings.git.inc')) {
  21. include_once DRUPAL_ROOT . '/sites/default/settings.git.inc';
  22. }
  23.  
  24. $current_branch = gi_git_current_branch_name();
  25. $db_default = isset($databases_by_branch[$current_branch]) ? $databases_by_branch[$current_branch] : array();
  26.  
  27. if(!empty($db_default)) {
  28. $db_default['driver'] = 'mysql';
  29. $db_default['username'] = 'drupaluser';
  30. $db_default['password'] = '';
  31. $db_default['host'] = '127.0.0.1';
  32. $db_default['port'] = 33066;
  33. }
  34.  
  35. /**
  36.  * If you are not using databases credentials by branch, complete the
  37.  * database informations down here.
  38.  *
  39.  * Tips: Also it could be your default database if you do not update the above
  40.  * parameters in the $databases_by_branch array.
  41.  */
  42. if (empty($db_default))
  43. $db_default = array (
  44. 'driver' => 'mysql',
  45. 'database' => 'myproject_db',
  46. 'username' => 'drupaluser',
  47. 'password' => '',
  48. 'host' => '127.0.0.1',
  49. 'port' => 33066,
  50. );
  51.  
  52.  
  53. /**
  54.  * Database settings
  55.  */
  56.  
  57. //D6 DB config
  58. $db_url = $db_default['driver'] . '://' . $db_default['username'] . '@' . $db_default['host'] . ':' . $db_default['port'] . '/' . $db_default['database'];
  59.  
  60. //D7 DB config
  61. $databases = array('default' => array('default' => $db_default));

And the content of the settings.git.inc file :

</> CopyGet raw version
php
  1. <?php
  2.  
  3. function gi_git_current_branch_name() {
  4. // here our project tructure is : root/docroot, where root is the root of the Git repo and docroot is the Drupal root.
  5. $branchname_from_HEAD = file(DRUPAL_ROOT . '/../.git/HEAD');
  6. $branchname_from_HEAD = $branchname_from_HEAD[0]; //get the string from the array
  7. $a_branchname = explode("/", $branchname_from_HEAD); //seperate out by the "/" in the string
  8.  
  9. return preg_replace('/\s/', '', $a_branchname[count($a_branchname) - 1]);
  10. }
  11.  
  12. ?>