2010-12-24 19:52:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
**/
|
|
|
|
class Minion_Migration_Manager {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The database connection that sould be used
|
|
|
|
* @var Kohana_Database
|
|
|
|
*/
|
|
|
|
protected $_db;
|
|
|
|
|
2010-12-28 19:51:17 +02:00
|
|
|
/**
|
|
|
|
* Model used to interact with the migrations table in the database
|
|
|
|
* @var Model_Minion_Migration
|
|
|
|
*/
|
|
|
|
protected $_model;
|
|
|
|
|
2010-12-24 19:52:58 +02:00
|
|
|
/**
|
|
|
|
* Constructs the object, allows injection of a Database connection
|
|
|
|
*
|
2010-12-28 19:51:17 +02:00
|
|
|
* @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
|
2010-12-24 19:52:58 +02:00
|
|
|
*/
|
2010-12-28 19:51:17 +02:00
|
|
|
public function __construct(Kohana_Database $db, Model_Minion_Migration $model = NULL)
|
2010-12-24 19:52:58 +02:00
|
|
|
{
|
2010-12-28 19:51:17 +02:00
|
|
|
if($model === NULL)
|
|
|
|
{
|
|
|
|
$model = new Model_Minion_Migration($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_db = $db;
|
|
|
|
$this->_model = $model;
|
2010-12-24 19:52:58 +02:00
|
|
|
}
|
|
|
|
|
2010-12-28 07:02:27 +02:00
|
|
|
/**
|
|
|
|
* Run migrations in the specified locations so as to reach specified targets
|
|
|
|
*
|
|
|
|
* There are three methods for specifying target versions:
|
|
|
|
*
|
|
|
|
* 1. Pass them in with the array of locations, i.e.
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* location => target_version
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* 2. Pass them in separately, with param1 containing an array of
|
|
|
|
* locations like:
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* location,
|
|
|
|
* location2,
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* And param2 containing an array structured in the same way as in #1
|
|
|
|
*
|
|
|
|
* 3. Perform a mix of the above two methods
|
|
|
|
*
|
|
|
|
* It may seem odd to use two arrays to specify locations and versions, but
|
|
|
|
* it's this way to allow users to upgrade / downgrade all locations while
|
|
|
|
* migrating a specific location to a specific version
|
|
|
|
*
|
|
|
|
* If no locations are specified then migrations from all locations will be
|
|
|
|
* run and be brought up to the latest available version
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function run_migration(array $locations = array(), $versions = array(), $default_direction = TRUE)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-24 19:52:58 +02:00
|
|
|
/**
|
|
|
|
* Syncs all available migration files with the database
|
|
|
|
*
|
|
|
|
* @chainable
|
|
|
|
* @return Minion_Migration_Manager Chainable instance
|
|
|
|
*/
|
2010-12-28 07:02:27 +02:00
|
|
|
public function sync_migration_files()
|
2010-12-24 19:52:58 +02:00
|
|
|
{
|
2010-12-28 19:51:17 +02:00
|
|
|
$installed = $this->_model->fetch_all();
|
2010-12-24 19:52:58 +02:00
|
|
|
|
|
|
|
$available = $this->scan_for_migrations();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans all migration directories for available migration files
|
|
|
|
*
|
|
|
|
* Returns an array of
|
|
|
|
*
|
|
|
|
* migration_id => array(
|
|
|
|
* 'file' => migration_file,
|
2010-12-28 04:47:49 +02:00
|
|
|
* 'location' => migration_location
|
2010-12-24 19:52:58 +02:00
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @param return array
|
|
|
|
*/
|
|
|
|
public function scan_for_migrations()
|
|
|
|
{
|
|
|
|
$files = Kohana::list_files('migrations');
|
|
|
|
|
|
|
|
return Minion_Migration_Util::parse_migrations_from_files($files);
|
|
|
|
}
|
|
|
|
}
|