1
0
Fork 0
mirror of https://github.com/Oreolek/kohana-migrations.git synced 2024-06-16 15:01:08 +03:00

Added basis for migrations deciding what db connection to use, refs #6

This commit is contained in:
Matt Button 2011-01-10 16:01:40 +00:00
parent a584ac47ea
commit 959d130669
3 changed files with 48 additions and 28 deletions

View file

@ -10,6 +10,40 @@
*/
abstract class Minion_Migration_Base {
/**
* Array of information about this migration
* @var array
*/
protected $_info = array();
/**
* Constructs the migration
*
* @param array Information about this migration
*/
public function __construct(array $info)
{
$this->_info = $info;
}
/**
* Get the name of the database connection group this migration should be run against
*
* @return string
*/
public function get_database_connection()
{
$config = Kohana::config('minion/migration');
$location = $this->_info['location'];
if(isset($config->location_connection[$location]))
{
return $config->location_connection[$location];
}
return Database::$default;
}
/**
* Runs any SQL queries necessary to bring the database up a migration version
*

View file

@ -153,7 +153,6 @@ class Minion_Migration_Manager {
public function run_migration(array $locations = array(), $versions = array(), $default_direction = TRUE)
{
$migrations = $this->_model->fetch_required_migrations($locations, $versions, $default_direction);
$db = $this->_get_db_instance();
foreach($migrations as $path => $location)
{
@ -176,24 +175,14 @@ class Minion_Migration_Manager {
$class = Minion_Migration_Util::get_class_from_migration($migration);
$db->query(NULL, 'START TRANSACTION');
try
{
include_once $file;
$instance = new $class;
$instance->$method($db);
}
catch(Exception $e)
{
$db->query(NULL, 'ROLLBACK');
throw $e;
}
$db->query(NULL, 'COMMIT');
include_once $file;
$instance = new $class($migration);
$db = $this->_get_db_instance($instance->get_database_connection());
$instance->$method($db);
if($this->_dry_run)
{
@ -275,15 +264,14 @@ class Minion_Migration_Manager {
/**
* Gets a database connection for running the migrations
*
* @param string Database connection group name
* @return Kohana_Database Database connection
*/
protected function _get_db_instance()
protected function _get_db_instance($db_group)
{
// If this isn't a dry run then just use the normal database connection
// If this isn't a dry run then just use a normal database connection
if( ! $this->_dry_run)
return $this->_db;
$db_group = array_search($this->_db, Database::$instances);
return Database::instance($db_group);
return Minion_Migration_Database::faux_instance($db_group);
}

View file

@ -2,10 +2,8 @@
return array(
// Allows you to map an environment to a specific database config group
'db_connections' => array(
'development' => 'default',
'unittest' => 'unittest'
// A mapping of location_connections => db_connection to use
'location_connection' => array(
),
);