diff --git a/classes/minion/migration/database.php b/classes/minion/migration/database.php new file mode 100644 index 0000000..9c2304a --- /dev/null +++ b/classes/minion/migration/database.php @@ -0,0 +1,57 @@ +_queries; + } + + /** + * Resets the query stack to an empty state + * @return Minion_Migration_Database $this + */ + public function reset_query_stack() + { + $this->_queries = array(); + + return $this; + } + + /** + * Appears to allow calling script to execute an SQL query, but merely logs + * it and returns NULL + * + * @return NULL + */ + public function query($type, $sql, $as_object = FALSE, array $params = NULL) + { + $this->_queries[] = $sql; + + return NULL; + } +} diff --git a/classes/minion/migration/manager.php b/classes/minion/migration/manager.php index 8601092..d91f142 100644 --- a/classes/minion/migration/manager.php +++ b/classes/minion/migration/manager.php @@ -97,9 +97,10 @@ class Minion_Migration_Manager { * @param array Set of locations to update, empty array means all * @param array Versions for specified locations * @param boolean The default direction (up/down) for migrations without a specific version - * @return boolean Whether + * @param boolean Whether successful migrations should be recorded + * @return array Array of all migrations that were successfully applied */ - public function run_migration(array $locations = array(), $versions = array(), $default_direction = TRUE) + public function run_migration(array $locations = array(), $versions = array(), $default_direction = TRUE, $record_success = TRUE) { $migrations = $this->_model->fetch_required_migrations($locations, $versions, $default_direction); @@ -109,17 +110,14 @@ class Minion_Migration_Manager { foreach($location['migrations'] as $migration) { - $file = Minion_Migration_Util::get_migration_from_filename( - $migration['id'], - $migration['location'] - ); + $file = Minion_Migration_Util::get_filename_from_migration($migration); if( ! ($file = Kohana::find_file('migrations', $file))) { throw new Kohana_Exception('Cannot load migration :migration', array(':migration' => $migration['id'])); } - $class = str_replace('-', '_', $migration['id']); + $class = Minion_Migration_Util::get_class_from_migration($migration); $this->_db->query(NULL, 'START TRANSACTION'); @@ -137,8 +135,13 @@ class Minion_Migration_Manager { throw $e; } - + $this->_db->query('COMMIT'); + + if($record_success) + { + $this->_model->mark_migration($migration, $location['direction']); + } } } } diff --git a/classes/minion/task/db/migrate.php b/classes/minion/task/db/migrate.php index c3a96b2..757f497 100644 --- a/classes/minion/task/db/migrate.php +++ b/classes/minion/task/db/migrate.php @@ -75,18 +75,32 @@ class Minion_Task_Db_Migrate extends Minion_Task $environment = Arr::get($config, 'environment', 'development'); $specified_locations = Arr::get($config, 'locations', NULL); $versions = Arr::get($config, 'versions', NULL); - + $dry_run = isset($config['dry-run']); + $targets = $this->_parse_target_versions($versions); $locations = $this->_parse_locations($specified_locations); + $db = Database::instance($k_config['db_connections'][$environment]); - $manager = new Minion_Migration_Manager($db); + if($dry_run) + { + $manager = new Minion_Migration_Manager( + Minion_Migration_Database::instance($k_config['db_connections'][$environment]), + new Model_Minion_Migration($db) + ); + } + else + { + $manager = new Minion_Migration_Manager($db); + } $results = $manager // Sync the available migrations with those in the db ->sync_migration_files() - ->run_migration($locations, $targets, $this->_default_direction); + // Run migrations for specified locations & versions, and if it's + // a dry run don't log results to DB + ->run_migration($locations, $targets, $this->_default_direction, ! $dry_run); } /**