mirror of
https://github.com/Oreolek/kohana-migrations.git
synced 2024-05-24 11:58:11 +03:00
Added migration sync functionality between FS and DB table
This commit is contained in:
parent
d164f69f6a
commit
e1a60f9d74
|
@ -155,6 +155,36 @@ class Minion_Migration_Manager {
|
|||
$installed = $this->_model->fetch_all('id');
|
||||
|
||||
$available = $this->scan_for_migrations();
|
||||
|
||||
$all_migrations = array_keys($installed) + array_keys($available);
|
||||
|
||||
foreach($all_migrations as $migration)
|
||||
{
|
||||
// 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
|
||||
// not exist in the "real world"
|
||||
if($installed[$migration]['applied'] === '0')
|
||||
{
|
||||
$this->_model->delete_migration($installed[$migration]);
|
||||
}
|
||||
}
|
||||
// If the migration has not yet been installed :D
|
||||
elseif( ! isset($installed[$migration]) AND isset($available[$migration]))
|
||||
{
|
||||
$this->_model->add_migration($available[$migration]);
|
||||
}
|
||||
// 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'])
|
||||
{
|
||||
$this->_model->update_migration($installed[$migration], $available[$migration]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -40,6 +40,78 @@ class Model_Minion_Migration extends Model
|
|||
return DB::select('*', DB::expr('CONCAT(`location`, ":", CAST(`timestamp` AS CHAR)) AS `id`'))->from($this->_table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a migration into the database
|
||||
*
|
||||
* @param array Migration data
|
||||
* @return Model_Minion_Migration $this
|
||||
*/
|
||||
public function add_migration(array $migration)
|
||||
{
|
||||
DB::insert($this->_table, array('timestamp', 'location', 'description'))
|
||||
->values(array($migration['timestamp'], $migration['location'], $migration['description']))
|
||||
->execute($this->_db);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a migration from the database
|
||||
*
|
||||
* @param string|array Migration id / info
|
||||
* @return Model_Minion_Migration $this
|
||||
*/
|
||||
public function delete_migration($migration)
|
||||
{
|
||||
if(is_array($migration))
|
||||
{
|
||||
$timestamp = $migration['timestamp'];
|
||||
$location = $migration['location'];
|
||||
}
|
||||
else
|
||||
{
|
||||
list($timestamp, $location) = explode(':', $migration);
|
||||
}
|
||||
|
||||
DB::delete($this->_table)
|
||||
->where('timestamp', '=', $timestamp)
|
||||
->where('location', '=', $location)
|
||||
->execute($this->_db);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing migration record to reflect a new one
|
||||
*
|
||||
* @param array The current migration
|
||||
* @param array The new migration
|
||||
* @return Model_Minion_Migration $this
|
||||
*/
|
||||
public function update_migration(array $current, array $new)
|
||||
{
|
||||
$set = array();
|
||||
|
||||
foreach($new as $key => $value)
|
||||
{
|
||||
if($key !== 'id' AND $current[$key] !== $value)
|
||||
{
|
||||
$set[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if(count($set))
|
||||
{
|
||||
DB::update($this->_table)
|
||||
->set($set)
|
||||
->where('timestamp', '=', $current['timestamp'])
|
||||
->where('location', '=', $current['location'])
|
||||
->execute($this->_db);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects all migrations from the migratinos table
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue