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

Updating code to follow kohana coding standards

This commit is contained in:
Matt Button 2011-01-31 17:27:21 +00:00
parent e52563165c
commit 8fc8ea440e
11 changed files with 79 additions and 81 deletions

View file

@ -4,7 +4,7 @@
* Controller for interacting with minion on the cli * Controller for interacting with minion on the cli
* *
* @author Matt Button <matthew@sigswitch.com> * @author Matt Button <matthew@sigswitch.com>
**/ */
class Controller_Minion extends Controller class Controller_Minion extends Controller
{ {
/** /**
@ -18,7 +18,7 @@ class Controller_Minion extends Controller
*/ */
public function before() public function before()
{ {
if( ! Kohana::$is_cli) if ( ! Kohana::$is_cli)
{ {
throw new Kohana_Exception("Minion can only be ran from the cli"); throw new Kohana_Exception("Minion can only be ran from the cli");
} }
@ -27,12 +27,12 @@ class Controller_Minion extends Controller
$options = CLI::options('help', 'task'); $options = CLI::options('help', 'task');
if(array_key_exists('help', $options)) if (array_key_exists('help', $options))
{ {
$this->request->action = 'help'; $this->request->action = 'help';
} }
if( ! empty($options['task'])) if ( ! empty($options['task']))
{ {
$this->_task = $options['task']; $this->_task = $options['task'];
} }
@ -49,7 +49,7 @@ class Controller_Minion extends Controller
$tasks = Minion_Util::compile_task_list(Kohana::list_files('classes/minion/task')); $tasks = Minion_Util::compile_task_list(Kohana::list_files('classes/minion/task'));
$view = NULL; $view = NULL;
if(empty($this->_task)) if (empty($this->_task))
{ {
$view = new View('minion/help/list'); $view = new View('minion/help/list');
@ -59,7 +59,7 @@ class Controller_Minion extends Controller
{ {
$class = Minion_Util::convert_task_to_class_name($this->_task); $class = Minion_Util::convert_task_to_class_name($this->_task);
if( ! class_exists($class)) if ( ! class_exists($class))
{ {
echo View::factory('minion/help/error') echo View::factory('minion/help/error')
->set('error', 'Task "'.$task.'" does not exist'); ->set('error', 'Task "'.$task.'" does not exist');
@ -88,7 +88,7 @@ class Controller_Minion extends Controller
*/ */
public function action_execute() public function action_execute()
{ {
if(empty($this->_task)) if (empty($this->_task))
{ {
return $this->action_help(); return $this->action_help();
} }
@ -108,7 +108,7 @@ class Controller_Minion extends Controller
$config = array(); $config = array();
$options = (array) $task->get_config_options(); $options = (array) $task->get_config_options();
if( ! empty($options)) if ( ! empty($options))
{ {
$options = $task->get_config_options(); $options = $task->get_config_options();
$config = call_user_func_array(array('CLI', 'options'), $options); $config = call_user_func_array(array('CLI', 'options'), $options);

View file

@ -36,7 +36,7 @@ abstract class Minion_Migration_Base {
$config = Kohana::config('minion/migration'); $config = Kohana::config('minion/migration');
$location = $this->_info['location']; $location = $this->_info['location'];
if(isset($config->location_connection[$location])) if (isset($config->location_connection[$location]))
{ {
return $config->location_connection[$location]; return $config->location_connection[$location];
} }

View file

@ -13,9 +13,9 @@ class Minion_Migration_Database extends Database_MySQL {
*/ */
public static function faux_instance($db_group = NULL, array $config = NULL) public static function faux_instance($db_group = NULL, array $config = NULL)
{ {
if($config === NULL) if ($config === NULL)
{ {
if($db_group === NULL) if ($db_group === NULL)
{ {
$db_group = Database::$default; $db_group = Database::$default;
} }

View file

@ -6,7 +6,7 @@
* that need to be executed in order to reach a target version * that need to be executed in order to reach a target version
* *
* @author Matt Button <matthew@sigswitch.com> * @author Matt Button <matthew@sigswitch.com>
**/ */
class Minion_Migration_Manager { class Minion_Migration_Manager {
/** /**
@ -47,7 +47,7 @@ class Minion_Migration_Manager {
*/ */
public function __construct(Kohana_Database $db, Model_Minion_Migration $model = NULL) public function __construct(Kohana_Database $db, Model_Minion_Migration $model = NULL)
{ {
if($model === NULL) if ($model === NULL)
{ {
$model = new Model_Minion_Migration($db); $model = new Model_Minion_Migration($db);
} }
@ -154,15 +154,15 @@ class Minion_Migration_Manager {
{ {
$migrations = $this->_model->fetch_required_migrations($locations, $versions, $default_direction); $migrations = $this->_model->fetch_required_migrations($locations, $versions, $default_direction);
foreach($migrations as $path => $location) foreach ($migrations as $path => $location)
{ {
$method = $location['direction'] ? 'up' : 'down'; $method = $location['direction'] ? 'up' : 'down';
foreach($location['migrations'] as $migration) foreach ($location['migrations'] as $migration)
{ {
$filename = Minion_Migration_Util::get_filename_from_migration($migration); $filename = Minion_Migration_Util::get_filename_from_migration($migration);
if( ! ($file = Kohana::find_file('migrations', $filename, FALSE))) if ( ! ($file = Kohana::find_file('migrations', $filename, FALSE)))
{ {
throw new Kohana_Exception( throw new Kohana_Exception(
'Cannot load migration :migration (:file)', 'Cannot load migration :migration (:file)',
@ -192,7 +192,7 @@ class Minion_Migration_Manager {
} }
if($this->_dry_run) if ($this->_dry_run)
{ {
$this->_dry_run_sql[$path][$migration['timestamp']] = $db->reset_query_stack(); $this->_dry_run_sql[$path][$migration['timestamp']] = $db->reset_query_stack();
} }
@ -221,26 +221,26 @@ class Minion_Migration_Manager {
$all_migrations = array_merge(array_keys($installed), array_keys($available)); $all_migrations = array_merge(array_keys($installed), array_keys($available));
foreach($all_migrations as $migration) foreach ($all_migrations as $migration)
{ {
// If this migration has since been deleted // If this migration has since been deleted
if(isset($installed[$migration]) AND ! isset($available[$migration])) if (isset($installed[$migration]) AND ! isset($available[$migration]))
{ {
// We should only delete a record of this migration if it does // We should only delete a record of this migration if it does
// not exist in the "real world" // not exist in the "real world"
if($installed[$migration]['applied'] === '0') if ($installed[$migration]['applied'] === '0')
{ {
$this->_model->delete_migration($installed[$migration]); $this->_model->delete_migration($installed[$migration]);
} }
} }
// If the migration has not yet been installed :D // If the migration has not yet been installed :D
elseif( ! isset($installed[$migration]) AND isset($available[$migration])) elseif ( ! isset($installed[$migration]) AND isset($available[$migration]))
{ {
$this->_model->add_migration($available[$migration]); $this->_model->add_migration($available[$migration]);
} }
// Somebody changed the description of the migration, make sure we // Somebody changed the description of the migration, make sure we
// update it in the db as we use this to build the filename! // update it in the db as we use this to build the filename!
elseif($installed[$migration]['description'] !== $available[$migration]['description']) elseif ($installed[$migration]['description'] !== $available[$migration]['description'])
{ {
$this->_model->update_migration($installed[$migration], $available[$migration]); $this->_model->update_migration($installed[$migration], $available[$migration]);
} }
@ -259,7 +259,7 @@ class Minion_Migration_Manager {
protected function _get_db_instance($db_group) protected function _get_db_instance($db_group)
{ {
// If this isn't a dry run then just use a normal database connection // If this isn't a dry run then just use a normal database connection
if( ! $this->_dry_run) if ( ! $this->_dry_run)
return Database::instance($db_group); return Database::instance($db_group);
return Minion_Migration_Database::faux_instance($db_group); return Minion_Migration_Database::faux_instance($db_group);

View file

@ -5,7 +5,7 @@
* Provides a set of utility functions for managing migrations * Provides a set of utility functions for managing migrations
* *
* @author Matt Button <matthew@sigswitch.com> * @author Matt Button <matthew@sigswitch.com>
**/ */
class Minion_Migration_Util { class Minion_Migration_Util {
/** /**
@ -19,10 +19,10 @@ class Minion_Migration_Util {
{ {
$migrations = array(); $migrations = array();
foreach($files as $file => $path) foreach ($files as $file => $path)
{ {
// If this is a directory we're dealing with // If this is a directory we're dealing with
if(is_array($path)) if (is_array($path))
{ {
$migrations += Minion_Migration_Util::compile_migrations_from_files($path); $migrations += Minion_Migration_Util::compile_migrations_from_files($path);
} }
@ -81,7 +81,7 @@ class Minion_Migration_Util {
$location = $migration['location']; $location = $migration['location'];
$migration = $migration['timestamp'].'_'.$migration['description']; $migration = $migration['timestamp'].'_'.$migration['description'];
$location = ! empty($location) ? rtrim($location, '/').'/' : ''; $location = ( ! empty($location)) ? (rtrim($location, '/').'/') : '';
return $location.$migration.EXT; return $location.$migration.EXT;
} }
@ -95,7 +95,7 @@ class Minion_Migration_Util {
*/ */
public static function get_class_from_migration($migration) public static function get_class_from_migration($migration)
{ {
if(is_string($migration)) if (is_string($migration))
{ {
$migration = str_replace(array(':', '/'), ' ', $migration); $migration = str_replace(array(':', '/'), ' ', $migration);
} }

View file

@ -2,7 +2,6 @@
/** /**
* Interface that all minion tasks must implement * Interface that all minion tasks must implement
*
*/ */
abstract class Minion_Task { abstract class Minion_Task {
@ -15,14 +14,14 @@ abstract class Minion_Task {
*/ */
public static function factory($task) public static function factory($task)
{ {
if(is_string($task)) if (is_string($task))
{ {
$class = Minion_Util::convert_task_to_class_name($task); $class = Minion_Util::convert_task_to_class_name($task);
$task = new $class; $task = new $class;
} }
if( ! $task instanceof Minion_Task) if ( ! $task instanceof Minion_Task)
{ {
throw new Kohana_Exception( throw new Kohana_Exception(
"Task ':task' is not a valid minion task", "Task ':task' is not a valid minion task",
@ -47,7 +46,7 @@ abstract class Minion_Task {
{ {
static $task_name = NULL; static $task_name = NULL;
if($task_name === NULL) if ($task_name === NULL)
{ {
$task_name = Minion_Util::convert_class_to_task($this); $task_name = Minion_Util::convert_class_to_task($this);
} }

View file

@ -28,7 +28,7 @@ class Minion_Task_Cache_Purge extends Minion_Task
*/ */
public function execute(array $config) public function execute(array $config)
{ {
if(empty($config['cache'])) if (empty($config['cache']))
{ {
return 'Please specify a set of cache configs.'; return 'Please specify a set of cache configs.';
} }
@ -37,7 +37,7 @@ class Minion_Task_Cache_Purge extends Minion_Task
$caches = explode(',', $config['cache']); $caches = explode(',', $config['cache']);
foreach($caches as $cache) foreach ($caches as $cache)
{ {
Cache::instance($cache) Cache::instance($cache)
->delete_all(); ->delete_all();

View file

@ -45,7 +45,7 @@ class Minion_Task_Db_Generate extends Minion_Task
*/ */
public function execute(array $config) public function execute(array $config)
{ {
if(empty($config['location']) OR empty($config['description'])) if (empty($config['location']) OR empty($config['description']))
{ {
return 'Please provide --location and --description'.PHP_EOL. return 'Please provide --location and --description'.PHP_EOL.
'See help for more info'.PHP_EOL; 'See help for more info'.PHP_EOL;
@ -86,7 +86,7 @@ class Minion_Task_Db_Generate extends Minion_Task
// If location is empty then we want to avoid double underscore in the // If location is empty then we want to avoid double underscore in the
// class name // class name
if( ! empty($class)) if ( ! empty($class))
{ {
$class .= '_'; $class .= '_';
} }

View file

@ -125,20 +125,20 @@ class Minion_Task_Db_Migrate extends Minion_Task
*/ */
protected function _parse_locations($location) protected function _parse_locations($location)
{ {
if(is_array($location)) if (is_array($location))
return $location; return $location;
$location = trim($location); $location = trim($location);
if(empty($location)) if (empty($location))
return array(); return array();
$locations = array(); $locations = array();
$location = explode(',', trim($location, ',')); $location = explode(',', trim($location, ','));
if( ! empty($location)) if ( ! empty($location))
{ {
foreach($location as $a_location) foreach ($location as $a_location)
{ {
$locations[] = trim($a_location, '/'); $locations[] = trim($a_location, '/');
} }
@ -163,21 +163,21 @@ class Minion_Task_Db_Migrate extends Minion_Task
*/ */
protected function _parse_target_versions($versions) protected function _parse_target_versions($versions)
{ {
if(empty($versions)) if (empty($versions))
return array(); return array();
$targets = array(); $targets = array();
if( ! is_array($versions)) if ( ! is_array($versions))
{ {
$versions = explode(',', trim($versions)); $versions = explode(',', trim($versions));
} }
foreach($versions as $version) foreach ($versions as $version)
{ {
$target = $this->_parse_version($version); $target = $this->_parse_version($version);
if(is_array($target)) if (is_array($target))
{ {
list($location, $version) = $target; list($location, $version) = $target;
@ -200,13 +200,13 @@ class Minion_Task_Db_Migrate extends Minion_Task
*/ */
protected function _parse_version($version) protected function _parse_version($version)
{ {
if(is_bool($version)) if (is_bool($version))
return $version; return $version;
if($version === 'TRUE' OR $version == 'FALSE') if ($version === 'TRUE' OR $version == 'FALSE')
return $version === 'TRUE'; return $version === 'TRUE';
if(strpos($version, ':') !== FALSE) if (strpos($version, ':') !== FALSE)
return explode(':', $version); return explode(':', $version);
throw new Kohana_Exception('Invalid target version :version', array(':version' => $version)); throw new Kohana_Exception('Invalid target version :version', array(':version' => $version));

View file

@ -3,7 +3,7 @@
/** /**
* Utility class for Minion * Utility class for Minion
**/ */
class Minion_Util class Minion_Util
{ {
/** /**
@ -68,15 +68,15 @@ class Minion_Util
{ {
$output = array(); $output = array();
foreach($files as $file => $path) foreach ($files as $file => $path)
{ {
$file = substr($file, strrpos($file, '/') + 1); $file = substr($file, strrpos($file, '/') + 1);
if(is_array($path) AND count($path)) if (is_array($path) AND count($path))
{ {
$task = Minion_Util::compile_task_list($path, $prefix.$file.Minion_Util::$task_separator); $task = Minion_Util::compile_task_list($path, $prefix.$file.Minion_Util::$task_separator);
if($task) if ($task)
{ {
$output = array_merge($output, $task); $output = array_merge($output, $task);
} }
@ -100,7 +100,7 @@ class Minion_Util
{ {
$task = trim($task); $task = trim($task);
if(empty($task)) if (empty($task))
return ''; return '';
return 'Minion_Task_'.implode('_', array_map('ucfirst', explode(Minion_Util::$task_separator, $task))); return 'Minion_Task_'.implode('_', array_map('ucfirst', explode(Minion_Util::$task_separator, $task)));
@ -114,7 +114,7 @@ class Minion_Util
*/ */
public static function convert_class_to_task($class) public static function convert_class_to_task($class)
{ {
if(is_object($class)) if (is_object($class))
{ {
$class = get_class($class); $class = get_class($class);
} }

View file

@ -2,7 +2,7 @@
/** /**
* Model for managing migrations * Model for managing migrations
**/ */
class Model_Minion_Migration extends Model class Model_Minion_Migration extends Model
{ {
/** /**
@ -50,7 +50,7 @@ class Model_Minion_Migration extends Model
{ {
$query = $this->_db->query(Database::SELECT, "SHOW TABLES like '".$this->_table."'"); $query = $this->_db->query(Database::SELECT, "SHOW TABLES like '".$this->_table."'");
if( ! count($query)) if ( ! count($query))
{ {
$sql = file_get_contents(Kohana::find_file('', 'minion_schema', 'sql')); $sql = file_get_contents(Kohana::find_file('', 'minion_schema', 'sql'));
@ -69,9 +69,9 @@ class Model_Minion_Migration extends Model
$locations = $this->fetch_current_versions('location', 'id'); $locations = $this->fetch_current_versions('location', 'id');
$available = $this->available_migrations(); $available = $this->available_migrations();
foreach($available as $migration) foreach ($available as $migration)
{ {
if(array_key_exists($migration['id'], $locations)) if (array_key_exists($migration['id'], $locations))
{ {
continue; continue;
} }
@ -92,7 +92,7 @@ class Model_Minion_Migration extends Model
*/ */
public function table($table = NULL) public function table($table = NULL)
{ {
if($table === NULL) if ($table === NULL)
return $this->_table; return $this->_table;
$this->_table = $table; $this->_table = $table;
@ -135,9 +135,9 @@ class Model_Minion_Migration extends Model
*/ */
public function get_migration($location, $timestamp = NULL) public function get_migration($location, $timestamp = NULL)
{ {
if($timestamp === NULL) if ($timestamp === NULL)
{ {
if(empty($location) OR strpos(':', $location) === FALSE) if (empty($location) OR strpos(':', $location) === FALSE)
{ {
throw new Kohana_Exception('Invalid migration id :id', array(':id' => $location)); throw new Kohana_Exception('Invalid migration id :id', array(':id' => $location));
} }
@ -160,7 +160,7 @@ class Model_Minion_Migration extends Model
*/ */
public function delete_migration($migration) public function delete_migration($migration)
{ {
if(is_array($migration)) if (is_array($migration))
{ {
$timestamp = $migration['timestamp']; $timestamp = $migration['timestamp'];
$location = $migration['location']; $location = $migration['location'];
@ -189,15 +189,15 @@ class Model_Minion_Migration extends Model
{ {
$set = array(); $set = array();
foreach($new as $key => $value) foreach ($new as $key => $value)
{ {
if($key !== 'id' AND $current[$key] !== $value) if ($key !== 'id' AND $current[$key] !== $value)
{ {
$set[$key] = $value; $set[$key] = $value;
} }
} }
if(count($set)) if (count($set))
{ {
DB::update($this->_table) DB::update($this->_table)
->set($set) ->set($set)
@ -286,7 +286,7 @@ class Model_Minion_Migration extends Model
*/ */
public function fetch_required_migrations($locations = NULL, $target_version = TRUE, $default_direction = TRUE) public function fetch_required_migrations($locations = NULL, $target_version = TRUE, $default_direction = TRUE)
{ {
if( ! empty($locations) AND ! is_array($locations)) if ( ! empty($locations) AND ! is_array($locations))
{ {
$locations = array( $locations = array(
$locations => is_array($target_version) $locations => is_array($target_version)
@ -300,7 +300,7 @@ class Model_Minion_Migration extends Model
$migrations = $this->fetch_current_versions('location'); $migrations = $this->fetch_current_versions('location');
// The user wants to run all available migrations // The user wants to run all available migrations
if(empty($locations)) if (empty($locations))
{ {
// Fetch a mirrored array of locations => locations // Fetch a mirrored array of locations => locations
$locations = $this->fetch_locations(TRUE); $locations = $this->fetch_locations(TRUE);
@ -308,11 +308,10 @@ class Model_Minion_Migration extends Model
// If the calling script has been lazy and given us a numerically // If the calling script has been lazy and given us a numerically
// indexed array of locations then we need to convert it to a mirrored // indexed array of locations then we need to convert it to a mirrored
// array // array
//
// We will decide the target version for these within the loop below // We will decide the target version for these within the loop below
elseif( ! Arr::is_assoc($locations)) elseif ( ! Arr::is_assoc($locations))
{ {
foreach($locations as $_pos => $location) foreach ($locations as $_pos => $location)
{ {
unset($locations[$_pos]); unset($locations[$_pos]);
@ -321,7 +320,7 @@ class Model_Minion_Migration extends Model
} }
// Merge locations with specified target versions // Merge locations with specified target versions
if( ! empty($target_version) AND is_array($target_version)) if ( ! empty($target_version) AND is_array($target_version))
{ {
$locations = $target_version + $locations; $locations = $target_version + $locations;
} }
@ -329,18 +328,18 @@ class Model_Minion_Migration extends Model
$migrations_to_apply = array(); $migrations_to_apply = array();
// What follows is a bit of icky code, but there aren't many "nice" ways around it // What follows is a bit of icky code, but there aren't many "nice" ways around it
//
// Basically we need to get a list of migrations that need to be performed, but // Basically we need to get a list of migrations that need to be performed, but
// the ordering of the migrations varies depending on whether we're wanting to // the ordering of the migrations varies depending on whether we're wanting to
// migrate up or migrate down. As such, we can't just apply a generic "order by x" // migrate up or migrate down. As such, we can't just apply a generic "order by x"
// condition, we have to run an individual query for each location // condition, we have to run an individual query for each location
//
// Again, icky, but this appears to be the only "sane" way of doing it with multiple // Again, icky, but this appears to be the only "sane" way of doing it with multiple
// locations // locations
//
// If you have a better way of doing this, please let me know :) // If you have a better way of doing this, please let me know :)
foreach($locations as $location => $target) foreach ($locations as $location => $target)
{ {
// By default all migrations go "up" // By default all migrations go "up"
$migrations_to_apply[$location]['direction'] = TRUE; $migrations_to_apply[$location]['direction'] = TRUE;
@ -350,7 +349,7 @@ class Model_Minion_Migration extends Model
// If this migration was auto-selected from the db then use the // If this migration was auto-selected from the db then use the
// default migration direction // default migration direction
if($target === $location) if ($target === $location)
{ {
$target = is_bool($target_version) $target = is_bool($target_version)
? $target_version ? $target_version
@ -359,10 +358,10 @@ class Model_Minion_Migration extends Model
// If the user is rolling this location to either extreme up or // If the user is rolling this location to either extreme up or
// extreme down // extreme down
if(is_bool($target)) if (is_bool($target))
{ {
// We're "undoing" all applied migrations, i.e. rolling back // We're "undoing" all applied migrations, i.e. rolling back
if($target === FALSE) if ($target === FALSE)
{ {
$migrations_to_apply[$location]['direction'] = FALSE; $migrations_to_apply[$location]['direction'] = FALSE;
@ -388,7 +387,7 @@ class Model_Minion_Migration extends Model
// If the current version is the requested version then nothing // If the current version is the requested version then nothing
// needs to be done // needs to be done
if($current_timestamp === $timestamp) if ($current_timestamp === $timestamp)
{ {
continue; continue;
} }
@ -396,14 +395,14 @@ class Model_Minion_Migration extends Model
// If they haven't applied any migrations for this location // If they haven't applied any migrations for this location
// yet and are justwhere wanting to apply all migrations // yet and are justwhere wanting to apply all migrations
// (i.e. roll forward) // (i.e. roll forward)
if($current_timestamp === NULL) if ($current_timestamp === NULL)
{ {
$query $query
->and_where('timestamp', '<=', $timestamp) ->and_where('timestamp', '<=', $timestamp)
->order_by('timestamp', 'ASC'); ->order_by('timestamp', 'ASC');
} }
// If we need to move forward // If we need to move forward
elseif($timestamp > $current_timestamp) elseif ($timestamp > $current_timestamp)
{ {
$query $query
->and_where('timestamp', '<=', $timestamp) ->and_where('timestamp', '<=', $timestamp)
@ -411,7 +410,7 @@ class Model_Minion_Migration extends Model
->order_by('timestamp', 'ASC'); ->order_by('timestamp', 'ASC');
} }
// If we want to roll back // If we want to roll back
elseif($timestamp < $current_timestamp) elseif ($timestamp < $current_timestamp)
{ {
$query $query
->and_where('timestamp', '<', $current_timestamp) ->and_where('timestamp', '<', $current_timestamp)