diff --git a/classes/Minion/Migration/Base.php b/classes/Minion/Migration/Base.php index 066b6e5..2e3cb94 100644 --- a/classes/Minion/Migration/Base.php +++ b/classes/Minion/Migration/Base.php @@ -1,9 +1,9 @@ - @@ -19,7 +19,7 @@ abstract class Minion_Migration_Base { /** * Constructs the migration * - * @param array Information about this migration + * @param array $info Information about this migration */ public function __construct(array $info) { @@ -33,8 +33,8 @@ abstract class Minion_Migration_Base { */ public function get_database_connection() { - $config = Kohana::$config->load('minion/migration'); - $group = $this->_info['group']; + $config = Kohana::$config->load('minion/migration'); + $group = $this->_info['group']; if (isset($config->group_connection[$group])) { @@ -47,14 +47,15 @@ abstract class Minion_Migration_Base { /** * Runs any SQL queries necessary to bring the database up a migration version * - * @param Kohana_Database The database connection to perform actions on + * @param Kohana_Database $db The database connection to perform actions on */ abstract public function up(Kohana_Database $db); /** * Runs any SQL queries necessary to bring the database schema down a version * - * @param Kohana_Database The database connection to perform actions on + * @param Kohana_Database $db The database connection to perform actions on */ abstract public function down(Kohana_Database $db); + } diff --git a/classes/Minion/Migration/Database.php b/classes/Minion/Migration/Database.php index f58d369..5679a2b 100644 --- a/classes/Minion/Migration/Database.php +++ b/classes/Minion/Migration/Database.php @@ -1,4 +1,4 @@ -_migration; } + } diff --git a/classes/Minion/Migration/Manager.php b/classes/Minion/Migration/Manager.php index 37fb2c8..cbb997b 100644 --- a/classes/Minion/Migration/Manager.php +++ b/classes/Minion/Migration/Manager.php @@ -1,4 +1,4 @@ -_model->get_filename_from_migration($migration); + $filename = $this->_model->get_filename_from_migration($migration); if ( ! ($file = Kohana::find_file('migrations', $filename, FALSE))) { @@ -166,7 +165,7 @@ class Minion_Migration_Manager { { $instance->$method($db); } - catch(Database_Exception $e) + catch (Database_Exception $e) { throw new Minion_Migration_Exception($e->getMessage(), $migration); } @@ -230,8 +229,8 @@ class Minion_Migration_Manager { /** * Gets a database connection for running the migrations * - * @param string Database connection group name - * @return Kohana_Database Database connection + * @param string $db_group Database connection group name + * @return Kohana_Database Database connection */ protected function _get_db_instance($db_group) { @@ -241,4 +240,5 @@ class Minion_Migration_Manager { return Minion_Migration_Database::faux_instance($db_group); } + } diff --git a/classes/Model/Minion/Migration.php b/classes/Model/Minion/Migration.php index f861d2c..57d3d01 100644 --- a/classes/Model/Minion/Migration.php +++ b/classes/Model/Minion/Migration.php @@ -1,4 +1,4 @@ - 'mygroup:1293214439' * ); * - * @param string The migration's filename + * @param string $file The migration's filename * @return array Array of components about the migration */ public function get_migration_from_filename($file) @@ -99,7 +99,7 @@ class Model_Minion_Migration extends Model // path from the migrations folder to the migration file $migration['group'] = dirname(substr($file, 11, -strlen(EXT))); - if(strpos(basename($file), "_")) + if (strpos(basename($file), "_")) { list($migration['timestamp'], $migration['description']) = explode('_', basename($file, EXT), 2); @@ -117,15 +117,14 @@ class Model_Minion_Migration extends Model /** * 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 + * @param integer|array $migration The migration's ID or an array of timestamp, description + * @return string Path to the migration file */ public function get_filename_from_migration(array $migration) { $group = $migration['group']; - if(!empty($migration['description'])) + if ( ! empty($migration['description'])) { $migration = $migration['timestamp'].'_'.$migration['description']; } @@ -143,8 +142,8 @@ class Model_Minion_Migration extends Model * 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 + * @param string|array $migration The migration's ID or array of migration data + * @return string The migration class name */ public function get_class_from_migration($migration) { @@ -209,7 +208,7 @@ class Model_Minion_Migration extends Model * * Should only really be used during testing * - * @param string Table name + * @param string $table Table name * @return string|Model_Minion_Migration Get table name or return $this on set */ public function table($table = NULL) @@ -237,7 +236,7 @@ class Model_Minion_Migration extends Model /** * Inserts a migration into the database * - * @param array Migration data + * @param array $migration Migration data * @return Model_Minion_Migration $this */ public function add_migration(array $migration) @@ -252,8 +251,9 @@ class Model_Minion_Migration extends Model /** * Get a migration by its id * - * @param string Migration ID - * @return array Migration info + * @param string $group Migration ID + * @param string $timestamp The migrations timestamp + * @return array Migration info */ public function get_migration($group, $timestamp = NULL) { @@ -277,8 +277,8 @@ class Model_Minion_Migration extends Model /** * Deletes a migration from the database * - * @param string|array Migration id / info - * @return Model_Minion_Migration $this + * @param string|array $migration Migration id / info + * @return Model_Minion_Migration $this */ public function delete_migration($migration) { @@ -303,8 +303,8 @@ class Model_Minion_Migration extends Model /** * Update an existing migration record to reflect a new one * - * @param array The current migration - * @param array The new migration + * @param array $current The current migration + * @param array $new The new migration * @return Model_Minion_Migration $this */ public function update_migration(array $current, array $new) @@ -334,8 +334,8 @@ class Model_Minion_Migration extends Model /** * Change the applied status for a migration * - * @param array Migration information - * @param bool Whether this migration has been applied or unapplied + * @param array $migration Migration information + * @param bool $applied Whether this migration has been applied or unapplied * @return Model_Minion_Migration */ public function mark_migration(array $migration, $applied) @@ -402,8 +402,8 @@ class Model_Minion_Migration extends Model * Fetch a list of migrations that need to be applied in order to reach the * required version * - * @param string The groups to get migrations for - * @param mixed Target version + * @param string $group The groups to get migrations for + * @param mixed $target Target version */ public function fetch_required_migrations(array $group, $target = TRUE) { @@ -445,7 +445,7 @@ class Model_Minion_Migration extends Model $query->where('group', '=', $group); - if( $target !== NULL) + if ($target !== NULL) { if ($up) { @@ -497,9 +497,10 @@ class Model_Minion_Migration extends Model /** * Resolve a (potentially relative) target for a group to a definite timestamp * - * @param string Group name - * @param string|int Target - * @return array First element timestamp, second is boolean (TRUE if up, FALSE if down) + * @param string $group Group name + * @param string|int $target Target + * @return array First element timestamp, second is boolean + * (TRUE if up, FALSE if down) */ public function resolve_target($group, $target) { @@ -518,7 +519,7 @@ class Model_Minion_Migration extends Model $group = $group[0]; } - if( ! in_array($target[0], array('+', '-'))) + if ( ! in_array($target[0], array('+', '-'))) { throw new Kohana_Exception("Invalid relative target"); } @@ -572,6 +573,7 @@ class Model_Minion_Migration extends Model $results->next(); } - return array((string) $results->get('timestamp'), $up); + return array( (string) $results->get('timestamp'), $up); } + } diff --git a/classes/Task/Migrations/New.php b/classes/Task/Migrations/New.php index 188ca46..c625f17 100644 --- a/classes/Task/Migrations/New.php +++ b/classes/Task/Migrations/New.php @@ -1,4 +1,4 @@ - */ -class Task_Migrations_New extends Minion_Task -{ +class Task_Migrations_New extends Minion_Task { /** * A set of config options that this task accepts * @var array @@ -43,7 +42,7 @@ class Task_Migrations_New extends Minion_Task /** * Execute the task * - * @param array Configuration + * @param array $options Configuration */ protected function _execute(array $options) { @@ -52,14 +51,22 @@ class Task_Migrations_New extends Minion_Task $file = $this->generate($options); Minion_CLI::write('Migration generated: '.$file); } - catch(ErrorException $e) + catch (ErrorException $e) { Minion_CLI::write($e->getMessage()); } } - public function generate($options, $up = null, $down = null) + /** + * Generate the migration file and return the file path + * + * @param array $options The migration options + * @param string $up Contents of the up migration + * @param string $down Contents of the down migration + * @return string Filename + */ + public function generate($options, $up = NULL, $down = NULL) { // Trim slashes in group $options['group'] = trim($options['group'], '/'); @@ -74,13 +81,13 @@ class Task_Migrations_New extends Minion_Task $location = rtrim(realpath($options['location']), '/').'/migrations/'; // {year}{month}{day}{hour}{minute}{second} - $time = date('YmdHis'); + $time = date('YmdHis'); $class = $this->_generate_classname($group, $time); - $file = $this->_generate_filename($location, $group, $time, $description); + $file = $this->_generate_filename($location, $group, $time, $description); - $data = Kohana::FILE_SECURITY.PHP_EOL. - View::factory('minion/task/migrations/new/template') + $data = Kohana::FILE_SECURITY.PHP_EOL + .View::factory('minion/task/migrations/new/template') ->set('class', $class) ->set('description', $description) ->set('up', $up) @@ -100,9 +107,9 @@ class Task_Migrations_New extends Minion_Task /** * Generate a class name from the group * - * @param string group - * @param string Timestamp - * @return string Class name + * @param string $group The group + * @param string $time Timestamp + * @return string Class name */ protected function _generate_classname($group, $time) { @@ -123,24 +130,35 @@ class Task_Migrations_New extends Minion_Task /** * Generates a filename from the group, time and description * - * @param string Location to store migration - * @param string Timestamp - * @param string Description - * @return string Filename + * @param string $location Location to store migration + * @param string $group The group + * @param string $time Timestamp + * @param string $description Description + * @return string Filename */ - public function _generate_filename($location, $group, $time, $description) + protected function _generate_filename($location, $group, $time, $description) { - // Max 100 characters, lowecase filenames. + // Max 100 characters, lowercase filenames. $label = substr(strtolower($description), 0, 100); + // Only letters $label = preg_replace('~[^a-z]+~', '-', $label); + // Add the location, group, and time $filename = $location.$group.$time.'_'.$label; + // If description was empty, trim underscores $filename = trim($filename, '_'); + return $filename.EXT; } + /** + * Validate that the name of the group + * + * @param string $group The group name + * @return boolean + */ protected function _valid_group($group) { // Group cannot be empty diff --git a/classes/Task/Migrations/Run.php b/classes/Task/Migrations/Run.php index 2d23f10..1978123 100644 --- a/classes/Task/Migrations/Run.php +++ b/classes/Task/Migrations/Run.php @@ -1,4 +1,4 @@ - */ -class Task_Migrations_Run extends Minion_Task -{ +class Task_Migrations_Run extends Minion_Task { + /** * A set of config options that this task accepts * @var array @@ -63,12 +63,10 @@ class Task_Migrations_Run extends Minion_Task /** * Migrates the database to the version specified * - * @param array Configuration to use + * @param array $options Configuration to use */ protected function _execute(array $options) { - $k_config = Kohana::$config->load('minion/migration'); - $groups = $options['group']; $target = $options['to']; @@ -91,15 +89,15 @@ class Task_Migrations_Run extends Minion_Task } } - $db = Database::instance(); - $model = new Model_Minion_Migration($db); + $db = Database::instance(); + $model = new Model_Minion_Migration($db); $model->ensure_table_exists(); $manager = new Minion_Migration_Manager($db, $model); + // Sync the available migrations with those in the db $manager - // Sync the available migrations with those in the db ->sync_migration_files() ->set_dry_run($dry_run); @@ -108,7 +106,7 @@ class Task_Migrations_Run extends Minion_Task // Run migrations for specified groups & versions $manager->run_migration($groups, $target); } - catch(Minion_Migration_Exception $e) + catch (Minion_Migration_Exception $e) { echo View::factory('minion/task/migrations/run/exception') ->set('migration', $e->get_migration()) @@ -130,8 +128,8 @@ class Task_Migrations_Run extends Minion_Task /** * Parses a comma delimited set of groups and returns an array of them * - * @param string Comma delimited string of groups - * @return array Locations + * @param string $group Comma delimited string of groups + * @return array Locations */ protected function _parse_groups($group) { @@ -168,8 +166,8 @@ class Task_Migrations_Run extends Minion_Task * * {group}:(TRUE|FALSE|{migration_id}) * - * @param string Target version(s) specified by user - * @return array Versions + * @param string $versions Target version(s) specified by user + * @return array Versions */ protected function _parse_target_versions($versions) { @@ -205,8 +203,8 @@ class Task_Migrations_Run extends Minion_Task /* * Helper function for parsing target versions in user input * - * @param string Input migration target - * @return boolean|string The parsed target + * @param string $version Input migration target + * @return boolean|string The parsed target */ protected function _parse_version($version) { @@ -221,4 +219,5 @@ class Task_Migrations_Run extends Minion_Task throw new Kohana_Exception('Invalid target version :version', array(':version' => $version)); } + } diff --git a/classes/Task/Migrations/Status.php b/classes/Task/Migrations/Status.php index 4c82651..c4b7e8e 100644 --- a/classes/Task/Migrations/Status.php +++ b/classes/Task/Migrations/Status.php @@ -1,4 +1,4 @@ -groups = $model->get_group_statuses(); echo $view; } + } diff --git a/config/minion/migration.php b/config/minion/migration.php index cba92c5..999eeeb 100644 --- a/config/minion/migration.php +++ b/config/minion/migration.php @@ -1,11 +1,11 @@ db_connection to use - 'group_connection' => array( - - ), + + /** + * A mapping of group_connections => db_connection to use + */ + 'group_connection' => array(), /** * The table used to store migrations @@ -15,7 +15,7 @@ return array( /** * This specifies which migration should be the "base", in timestamp form. * This migration will not be run when --migrate-down is called - * + * * NULL means all migrations will run */ 'lowest_migration' => NULL, diff --git a/views/minion/task/migrations/new/template.php b/views/minion/task/migrations/new/template.php index 8239e6f..8db1dca 100755 --- a/views/minion/task/migrations/new/template.php +++ b/views/minion/task/migrations/new/template.php @@ -1,5 +1,5 @@ - + /** * */ @@ -13,7 +13,7 @@ class extends Minion_Migration_Base { */ public function up(Kohana_Database $db) { - + @@ -28,11 +28,12 @@ class extends Minion_Migration_Base { */ public function down(Kohana_Database $db) { - + // $db->query(NULL, 'DROP TABLE ... '); } + } diff --git a/views/minion/task/migrations/run.php b/views/minion/task/migrations/run.php index 6d9e32f..ed19248 100644 --- a/views/minion/task/migrations/run.php +++ b/views/minion/task/migrations/run.php @@ -1,24 +1,24 @@ - + Executed migrations Current versions of groups: $group_versions)) ?> - + This was a dry run, if it was a real run the following SQL would've been executed: - $migrations): ?> + $migrations): ?> #################### # Begin Location: # #################### - $sql): ?> + $sql): ?> # Begin - + ; @@ -31,4 +31,3 @@ This was a dry run, if it was a real run the following SQL would've been execute # End Location: # ################## - diff --git a/views/minion/task/migrations/run/exception.php b/views/minion/task/migrations/run/exception.php index c5ed39a..c1d6893 100644 --- a/views/minion/task/migrations/run/exception.php +++ b/views/minion/task/migrations/run/exception.php @@ -1,3 +1,3 @@ -Minion encountered an error while executing migration `` ): +Minion encountered an error while executing migration `` ): diff --git a/views/minion/task/migrations/status.php b/views/minion/task/migrations/status.php index 98b0f1c..df3b597 100644 --- a/views/minion/task/migrations/status.php +++ b/views/minion/task/migrations/status.php @@ -1,4 +1,4 @@ - $status): ?> - * + $status): ?> + *