From e1a60f9d7402202ef33a8661727137bb4fa982af Mon Sep 17 00:00:00 2001 From: Matt Button Date: Wed, 29 Dec 2010 04:29:05 +0000 Subject: [PATCH] Added migration sync functionality between FS and DB table --- classes/minion/migration/manager.php | 30 ++++++++++++ classes/model/minion/migration.php | 72 ++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/classes/minion/migration/manager.php b/classes/minion/migration/manager.php index 87300d0..8601092 100644 --- a/classes/minion/migration/manager.php +++ b/classes/minion/migration/manager.php @@ -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; } /** diff --git a/classes/model/minion/migration.php b/classes/model/minion/migration.php index e78ff03..0d48fa4 100644 --- a/classes/model/minion/migration.php +++ b/classes/model/minion/migration.php @@ -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 *