Skip to content

Instantly share code, notes, and snippets.

@delineas
Created December 15, 2015 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delineas/2b83a5e76cd0e2fe1920 to your computer and use it in GitHub Desktop.
Save delineas/2b83a5e76cd0e2fe1920 to your computer and use it in GitHub Desktop.
Drupal git branch different database
<?php
$databases = array (
'default' =>
array (
'default' =>
array (
'driver' => 'mysql',
'username' => 'root',
'password' => 'root',
'port' => '',
'host' => 'localhost',
'database' => 'regular_database',
'prefix' => '',
),
),
);
/**
* CHANGE CONFIGURATION BASED ON GIT
* */
$databases_by_branch = array(
'branch_name_1' => array (
'driver' => 'mysql',
'username' => 'root',
'password' => 'root',
'port' => '',
'host' => 'localhost',
'database' => 'branch_name_1_database',
'prefix' => '',
),
'branch_name_2' => array (
'driver' => 'mysql',
'username' => 'root',
'password' => 'root',
'port' => '',
'host' => 'localhost',
'database' => 'branch_name_2_database',
'prefix' => '',
)
);
$branchname_from_HEAD = file(DRUPAL_ROOT . '/.git/HEAD');
$branchname_from_HEAD = $branchname_from_HEAD[0]; //get the string from the array
$a_branchname = explode("/", $branchname_from_HEAD); //seperate out by the "/" in the string
$branchname = preg_replace('/\s/', '', $a_branchname[count($a_branchname) - 1]);
if(isset($databases_by_branch[$branchname])) {
$databases['default']['default'] = $databases_by_branch[$branchname];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment