From 959d1306695a68ae8b749f4858a1694d9d098eb1 Mon Sep 17 00:00:00 2001 From: Matt Button Date: Mon, 10 Jan 2011 16:01:40 +0000 Subject: [PATCH] Added basis for migrations deciding what db connection to use, refs #6 --- classes/minion/migration/base.php | 34 ++++++++++++++++++++++++++++ classes/minion/migration/manager.php | 34 +++++++++------------------- config/minion/migration.php | 8 +++---- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/classes/minion/migration/base.php b/classes/minion/migration/base.php index fed6493..e0196d7 100644 --- a/classes/minion/migration/base.php +++ b/classes/minion/migration/base.php @@ -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 * diff --git a/classes/minion/migration/manager.php b/classes/minion/migration/manager.php index 543c0d0..d0ae735 100644 --- a/classes/minion/migration/manager.php +++ b/classes/minion/migration/manager.php @@ -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); } diff --git a/config/minion/migration.php b/config/minion/migration.php index 0922c70..57518b3 100644 --- a/config/minion/migration.php +++ b/config/minion/migration.php @@ -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( + ), );