mirror of
https://github.com/Oreolek/kohana-migrations.git
synced 2024-05-24 11:58:11 +03:00
Outlined changes for dry run, refs #1
This commit is contained in:
parent
e1a60f9d74
commit
e7b6911806
57
classes/minion/migration/database.php
Normal file
57
classes/minion/migration/database.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* A faux database connection for doing dry run migrations
|
||||
*/
|
||||
class Minion_Migration_Database extends Database_MySQL {
|
||||
|
||||
/**
|
||||
* Creates a disposable instance of the faux connection
|
||||
*
|
||||
* @param array Config for the underlying DB connection
|
||||
* @return Minion_Migration_Database
|
||||
*/
|
||||
public static function instance(array $config)
|
||||
{
|
||||
return new Minion_Migration_Database('_minion_faux', $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* The query stack used to store queries
|
||||
* @var array
|
||||
*/
|
||||
protected $_queries = array();
|
||||
|
||||
/**
|
||||
* Gets the stack of queries that have been executed
|
||||
* @return array
|
||||
*/
|
||||
public function get_query_stack()
|
||||
{
|
||||
return $this->_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;
|
||||
}
|
||||
}
|
|
@ -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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue