1
0
Fork 0
mirror of https://github.com/Oreolek/kohana-migrations.git synced 2024-05-05 02:28:16 +03:00

Merge pull request #37 from EvanPurkhiser/CS-cleanup

Cleanup coding style to conform to Kohana CS
This commit is contained in:
Lorenzo Pisani 2012-10-26 20:00:37 -07:00
commit 79614748ef
13 changed files with 138 additions and 117 deletions

View file

@ -1,9 +1,9 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* The base migration class, must be extended by all migration files
*
* Each migration file must implement an up() and a down() which are used to
* Each migration file must implement an up() and a down() which are used to
* apply / remove this migration from the schema respectively
*
* @author Matt Button <matthew@sigswitch.com>
@ -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);
}

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* A faux database connection for doing dry run migrations
@ -7,8 +7,9 @@ class Minion_Migration_Database extends Database_MySQL {
/**
* Creates a disposable instance of the faux connection
*
* @param array Config for the underlying DB connection
*
* @param string $db_group The database group to use
* @param array $config Config for the underlying DB connection
* @return Minion_Migration_Database
*/
public static function faux_instance($db_group = NULL, array $config = NULL)
@ -57,7 +58,7 @@ class Minion_Migration_Database extends Database_MySQL {
}
/**
* Appears to allow calling script to execute an SQL query, but merely logs
* Appears to allow calling script to execute an SQL query, but merely logs
* it and returns NULL
*
* @return NULL
@ -68,4 +69,5 @@ class Minion_Migration_Database extends Database_MySQL {
return NULL;
}
}

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Minion exception, thrown during a migration error
@ -9,7 +9,6 @@ class Minion_Migration_Exception extends Kohana_Exception {
/**
* Constructor
*
*/
public function __construct($message, array $migration, array $variables = array(), $code = 0)
{
@ -30,4 +29,5 @@ class Minion_Migration_Exception extends Kohana_Exception {
{
return $this->_migration;
}
}

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* The migration manager is responsible for locating migration files, syncing
@ -10,7 +10,7 @@
class Minion_Migration_Manager {
/**
* The database connection that sould be used
* The database connection that should be used
* @var Kohana_Database
*/
protected $_db;
@ -38,12 +38,11 @@ class Minion_Migration_Manager {
*/
protected $_executed_migrations = array();
/**
* Constructs the object, allows injection of a Database connection
*
* @param Kohana_Database The database connection that should be passed to migrations
* @param Model_Minion_Migration Inject an instance of the minion model into the manager
* @param Kohana_Database $db The database connection that should be passed to migrations
* @param Model_Minion_Migration $model Inject an instance of the minion model into the manager
*/
public function __construct(Kohana_Database $db, Model_Minion_Migration $model = NULL)
{
@ -59,7 +58,7 @@ class Minion_Migration_Manager {
/**
* Set the database connection to be used
*
* @param Kohana_Database Database connection
* @param Kohana_Database $db Database connection
* @return Minion_Migration_Manager
*/
public function set_db(Kohana_Database $db)
@ -72,7 +71,7 @@ class Minion_Migration_Manager {
/**
* Set the model to be used in the rest of the app
*
* @param Model_Minion_Migration Model instance
* @param Model_Minion_Migration $model Model instance
* @return Minion_Migration_Manager
*/
public function set_model(Model_Minion_Migration $model)
@ -85,7 +84,7 @@ class Minion_Migration_Manager {
/**
* Set whether the manager should execute a dry run instead of a real run
*
* @param boolean Whether we should do a dry run
* @param boolean $dry_run Whether we should do a dry run
* @return Minion_Migration_Manager
*/
public function set_dry_run($dry_run)
@ -97,7 +96,7 @@ class Minion_Migration_Manager {
/**
* 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
* enabled. If dry run was not enabled, this returns an empty array
*
* @return array SQL Queries
*/
@ -120,9 +119,9 @@ class Minion_Migration_Manager {
*
*
*
* @param array Set of groups to update, empty array means all
* @param array Versions for specified groups
* @return array Array of all migrations that were successfully applied
* @param array $group Set of groups to update, empty array means all
* @param array $target Versions for specified groups
* @return array Array of all migrations that were successfully applied
*/
public function run_migration($group = array(), $target = TRUE)
{
@ -141,7 +140,7 @@ class Minion_Migration_Manager {
return;
}
$filename = $this->_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);
}
}

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Model for managing migrations
@ -21,7 +21,7 @@ class Model_Minion_Migration extends Model
* Constructs the model, taking a Database connection as the first and only
* parameter
*
* @param Kohana_Database Database connection to use
* @param Kohana_Database $db Database connection to use
*/
public function __construct(Kohana_Database $db)
{
@ -46,7 +46,7 @@ class Model_Minion_Migration extends Model
* Parses a set of files generated by Kohana::find_files and compiles it
* down into an array of migrations
*
* @param array Available files
* @param array $files Available files
* @return array Available Migrations
*/
public function compile_migrations_from_files(array $files)
@ -87,7 +87,7 @@ class Model_Minion_Migration extends Model
* 'id' => '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);
}
}

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* The new task provides an easy way to create migration files
@ -28,8 +28,7 @@
*
* @author Matt Button <matthew@sigswitch.com>
*/
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

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* The Run task compares the current version of the database with the target
@ -44,8 +44,8 @@
*
* @author Matt Button <matthew@sigswitch.com>
*/
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));
}
}

View file

@ -1,4 +1,4 @@
<?php defined('SYSPATH') or die('No direct script access.');
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Displays the current status of migrations in all groups
@ -12,17 +12,16 @@ class Task_Migrations_Status extends Minion_Task {
/**
* Execute the task
*
* @param array Config for the task
* @param array $options Config for the task
*/
protected function _execute(array $options)
{
$db = Database::instance();
$model = new Model_Minion_Migration($db);
$view = new View('minion/task/migrations/status');
$model = new Model_Minion_Migration(Database::instance());
$view = new View('minion/task/migrations/status');
$view->groups = $model->get_group_statuses();
echo $view;
}
}

View file

@ -1,11 +1,11 @@
<?php
return array(
// A mapping of group_connections => 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,

View file

@ -1,5 +1,5 @@
<?php if(!empty($description)): ?>
<?php if ( ! empty($description)): ?>
/**
* <?php echo $description.PHP_EOL; ?>
*/
@ -13,7 +13,7 @@ class <?php echo $class; ?> extends Minion_Migration_Base {
*/
public function up(Kohana_Database $db)
{
<?php if(!empty($up)): ?>
<?php if ( ! empty($up)): ?>
<?php echo $up; ?>
<?php else: ?>
@ -28,11 +28,12 @@ class <?php echo $class; ?> extends Minion_Migration_Base {
*/
public function down(Kohana_Database $db)
{
<?php if(!empty($down)): ?>
<?php if ( ! empty($down)): ?>
<?php echo $down; ?>
<?php else: ?>
// $db->query(NULL, 'DROP TABLE ... ');
<?php endif; ?>
}
}

View file

@ -1,24 +1,24 @@
<?php if( ! $quiet): ?>
<?php if ( ! $quiet): ?>
Executed <?php echo count($executed_migrations); ?> migrations
Current versions of groups:
<?php echo new View('minion/task/migrations/status', array('groups' => $group_versions)) ?>
<?php if($dry_run): ?>
<?php if ($dry_run): ?>
This was a dry run, if it was a real run the following SQL would've been executed:
<?php endif; ?>
<?php endif; ?>
<?php foreach($dry_run_sql as $group => $migrations): ?>
<?php foreach ($dry_run_sql as $group => $migrations): ?>
<?php $group_padding = str_repeat('#', strlen($group)); ?>
##################<?php echo $group_padding ?>##
# Begin Location: <?php echo $group; ?> #
##################<?php echo $group_padding ?>##
<?php foreach($migrations as $timestamp => $sql): ?>
<?php foreach ($migrations as $timestamp => $sql): ?>
# Begin <?php echo $timestamp; ?>
<?php foreach($sql as $query): ?>
<?php foreach ($sql as $query): ?>
<?php echo $query;?>;
<?php endforeach; ?>
@ -31,4 +31,3 @@ This was a dry run, if it was a real run the following SQL would've been execute
# End Location: <?php echo $group; ?> #
################<?php echo $group_padding ?>##
<?php endforeach; ?>

View file

@ -1,3 +1,3 @@
Minion encountered an error while executing migration `<?php echo $migration['id']; ?>` <?php echo (!empty($migration['description']) ? '('.$migration['description'].')' : '') ?>):
Minion encountered an error while executing migration `<?php echo $migration['id']; ?>` <?php echo (( ! empty($migration['description'])) ? ('('.$migration['description'].')') : '') ?>):
<?php echo $error; ?>

View file

@ -1,4 +1,4 @@
<?php foreach($groups as $group => $status): ?>
* <?php echo $group ?> <?php echo ($status !== NULL ? $status['timestamp'].' '.( !empty($status['description']) ? '('.$status['description'].')' : '' ) : 'Not installed'); ?>
<?php foreach ($groups as $group => $status): ?>
* <?php echo $group ?> <?php echo (($status !== NULL) ? ($status['timestamp'].' '.(( ! empty($status['description'])) ? ('('.$status['description'].')') : '')) : 'Not installed'); ?>
<?php endforeach; ?>