Permalink
Browse files

Moving the util methods to the model (refs #11)

  • Loading branch information...
1 parent 48df2fe commit 307181ccc49b5d6362fffe2ab0d02e80c1c87b89 @Zeelot Zeelot committed Jul 2, 2011
Showing with 138 additions and 148 deletions.
  1. +11 −11 classes/minion/migration/manager.php
  2. +0 −124 classes/minion/migration/util.php
  3. +127 −13 classes/model/minion/migration.php
@@ -1,8 +1,8 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
- * The migration manager is responsible for locating migration files, syncing
- * them with the migrations table in the database and selecting any migrations
+ * The migration manager is responsible for locating migration files, syncing
+ * them with the migrations table in the database and selecting any migrations
* that need to be executed in order to reach a target version
*
* @author Matt Button <matthew@sigswitch.com>
@@ -58,7 +58,7 @@ public function __construct(Kohana_Database $db, Model_Minion_Migration $model =
/**
* Set the database connection to be used
- *
+ *
* @param Kohana_Database Database connection
* @return Minion_Migration_Manager
*/
@@ -96,7 +96,7 @@ public function set_dry_run($dry_run)
}
/**
- * Returns a set of queries that would've been executed had dry run not been
+ * Returns a set of queries that would've been executed had dry run not been
* enabled. If dry run was not enabled, this returns an empty array
*
* @return array SQL Queries
@@ -141,28 +141,28 @@ public function run_migration($group = array(), $target = TRUE)
return;
}
- $filename = Minion_Migration_Util::get_filename_from_migration($migration);
+ $filename = $this->_model->get_filename_from_migration($migration);
if ( ! ($file = Kohana::find_file('migrations', $filename, FALSE)))
{
throw new Kohana_Exception(
- 'Cannot load migration :migration (:file)',
+ 'Cannot load migration :migration (:file)',
array(
- ':migration' => $migration['id'],
+ ':migration' => $migration['id'],
':file' => $filename
)
);
}
- $class = Minion_Migration_Util::get_class_from_migration($migration);
+ $class = $this->_model->get_class_from_migration($migration);
include_once $file;
$instance = new $class($migration);
$db = $this->_get_db_instance($instance->get_database_connection());
- try
+ try
{
$instance->$method($db);
}
@@ -204,7 +204,7 @@ public function sync_migration_files()
// If this migration has since been deleted
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"
if ($installed[$migration]['applied'] === '0')
{
@@ -216,7 +216,7 @@ public function sync_migration_files()
{
$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!
elseif ($installed[$migration]['description'] !== $available[$migration]['description'])
{
@@ -1,124 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-
-/**
- * Provides a set of utility functions for managing migrations
- *
- * @author Matt Button <matthew@sigswitch.com>
- */
-class Minion_Migration_Util {
-
- /**
- * Parses a set of files generated by Kohana::find_files and compiles it
- * down into an array of migrations
- *
- * @param array Available files
- * @return array Available Migrations
- */
- public static function compile_migrations_from_files(array $files)
- {
- $migrations = array();
-
- foreach ($files as $file => $path)
- {
- // If this is a directory we're dealing with
- if (is_array($path))
- {
- $migrations += Minion_Migration_Util::compile_migrations_from_files($path);
- }
- else
- {
- $migration = Minion_Migration_Util::get_migration_from_filename($file);
-
- $migrations[$migration['group'].':'.$migration['timestamp']] = $migration;
- }
- }
-
- return $migrations;
- }
-
- /**
- * Extracts information about a migration from its filename.
- *
- * Returns an array like:
- *
- * array(
- * 'group' => 'mygroup',
- * 'timestamp' => '1293214439',
- * 'description' => 'initial-setup',
- * 'id' => 'mygroup:1293214439'
- * );
- *
- * @param string The migration's filename
- * @return array Array of components about the migration
- */
- public static function get_migration_from_filename($file)
- {
- $migration = array();
-
- // Get rid of the file's "migrations/" prefix, the file extension and then
- // the filename itself. The "group" is essentially a slash delimited
- // path from the migrations folder to the migration file
- $migration['group'] = dirname(substr($file, 11, -strlen(EXT)));
-
- if(strpos(basename($file), "_"))
- {
- list($migration['timestamp'], $migration['description'])
- = explode('_', basename($file, EXT), 2);
- }
- else
- {
- $migration['timestamp'] = basename($file, EXT);
- $migration['description'] = "";
- }
- $migration['id'] = $migration['group'].':'.$migration['timestamp'];
-
- return $migration;
- }
-
- /**
- * Gets a migration file from its timestamp, description and group
- *
- * @param integer|array The migration's ID or an array of timestamp, description
- * @param string The migration group
- * @return string Path to the migration file
- */
- public static function get_filename_from_migration(array $migration)
- {
- $group = $migration['group'];
-
- if(!empty($migration['description']))
- {
- $migration = $migration['timestamp'].'_'.$migration['description'];
- }
- else
- {
- $migration = $migration['timestamp'];
- }
-
- $group = ( ! empty($group)) ? (rtrim($group, '/').'/') : '';
-
- return $group.$migration.EXT;
- }
-
- /**
- * Allows you to work out the class name from either an array of migration
- * info, or from a migration id
- *
- * @param string|array The migration's ID or array of migration data
- * @return string The migration class name
- */
- public static function get_class_from_migration($migration)
- {
- if (is_string($migration))
- {
- $migration = str_replace(array(':', '/'), ' ', $migration);
- }
- else
- {
- $migration = str_replace('/', ' ', $migration['group']).'_'.$migration['timestamp'];
- }
-
- return 'Migration_'.str_replace(array(' ', '-'), '_', ucwords($migration));
- }
-}
Oops, something went wrong.

0 comments on commit 307181c

Please sign in to comment.