PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Group-I/jobeet/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Migration/Diff.php

https://bitbucket.org/hosseinzolfi/db-lab-spring-2011/
PHP | 421 lines | 247 code | 34 blank | 140 comment | 40 complexity | eb36cb7a6837b0dab2cdd05c3b67d313 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: Diff.php 1080 2007-02-10 18:17:08Z jwage $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Migration_Diff - class used for generating differences and migration
  23. * classes from 'from' and 'to' schema information.
  24. *
  25. * @package Doctrine
  26. * @subpackage Migration
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 1.0
  30. * @version $Revision: 1080 $
  31. * @author Jonathan H. Wage <jonwage@gmail.com>
  32. */
  33. class Doctrine_Migration_Diff
  34. {
  35. protected $_from,
  36. $_to,
  37. $_changes = array('created_tables' => array(),
  38. 'dropped_tables' => array(),
  39. 'created_foreign_keys'=> array(),
  40. 'dropped_foreign_keys'=> array(),
  41. 'created_columns' => array(),
  42. 'dropped_columns' => array(),
  43. 'changed_columns' => array(),
  44. 'created_indexes' => array(),
  45. 'dropped_indexes' => array()),
  46. $_migration,
  47. $_startingModelFiles = array(),
  48. $_tmpPath;
  49. protected static $_toPrefix = 'ToPrfx',
  50. $_fromPrefix = 'FromPrfx';
  51. /**
  52. * Instantiate new Doctrine_Migration_Diff instance
  53. *
  54. * <code>
  55. * $diff = new Doctrine_Migration_Diff('/path/to/old_models', '/path/to/new_models', '/path/to/migrations');
  56. * $diff->generateMigrationClasses();
  57. * </code>
  58. *
  59. * @param string $from The from schema information source
  60. * @param string $to The to schema information source
  61. * @param mixed $migration Instance of Doctrine_Migration or path to migration classes
  62. * @return void
  63. */
  64. public function __construct($from, $to, $migration)
  65. {
  66. $this->_from = $from;
  67. $this->_to = $to;
  68. $this->_startingModelFiles = Doctrine_Core::getLoadedModelFiles();
  69. $this->setTmpPath(sys_get_temp_dir() . DIRECTORY_SEPARATOR . getmypid());
  70. if ($migration instanceof Doctrine_Migration) {
  71. $this->_migration = $migration;
  72. } else if (is_dir($migration)) {
  73. $this->_migration = new Doctrine_Migration($migration);
  74. }
  75. }
  76. /**
  77. * Set the temporary path to store the generated models for generating diffs
  78. *
  79. * @param string $tmpPath
  80. * @return void
  81. */
  82. public function setTmpPath($tmpPath)
  83. {
  84. if ( ! is_dir($tmpPath)) {
  85. mkdir($tmpPath, 0777, true);
  86. }
  87. $this->_tmpPath = $tmpPath;
  88. }
  89. /**
  90. * Get unique hash id for this migration instance
  91. *
  92. * @return string $uniqueId
  93. */
  94. protected function getUniqueId()
  95. {
  96. return md5($this->_from . $this->_to);
  97. }
  98. /**
  99. * Generate an array of changes found between the from and to schema information.
  100. *
  101. * @return array $changes
  102. */
  103. public function generateChanges()
  104. {
  105. $this->_cleanup();
  106. $from = $this->_generateModels(self::$_fromPrefix, $this->_from);
  107. $to = $this->_generateModels(
  108. Doctrine_Manager::getInstance()->getAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX) . self::$_toPrefix,
  109. $this->_to
  110. );
  111. return $this->_diff($from, $to);
  112. }
  113. /**
  114. * Generate a migration class for the changes in this diff instance
  115. *
  116. * @return array $changes
  117. */
  118. public function generateMigrationClasses()
  119. {
  120. $builder = new Doctrine_Migration_Builder($this->_migration);
  121. return $builder->generateMigrationsFromDiff($this);
  122. }
  123. /**
  124. * Initialize some Doctrine models at a given path.
  125. *
  126. * @param string $path
  127. * @return array $models
  128. */
  129. protected function _initializeModels($path)
  130. {
  131. $manager = Doctrine_Manager::getInstance();
  132. $modelLoading = $manager->getAttribute(Doctrine_Core::ATTR_MODEL_LOADING);
  133. if ($modelLoading === Doctrine_Core::MODEL_LOADING_PEAR) {
  134. $orig = Doctrine_Core::getModelsDirectory();
  135. Doctrine_Core::setModelsDirectory($path);
  136. $models = Doctrine_Core::initializeModels(Doctrine_Core::loadModels($path));
  137. Doctrine_Core::setModelsDirectory($orig);
  138. } else {
  139. $models = Doctrine_Core::initializeModels(Doctrine_Core::loadModels($path));
  140. }
  141. return $models;
  142. }
  143. /**
  144. * Generate a diff between the from and to schema information
  145. *
  146. * @param string $from Path to set of models to migrate from
  147. * @param string $to Path to set of models to migrate to
  148. * @return array $changes
  149. */
  150. protected function _diff($from, $to)
  151. {
  152. // Load the from and to models
  153. $fromModels = $this->_initializeModels($from);
  154. $toModels = $this->_initializeModels($to);
  155. // Build schema information for the models
  156. $fromInfo = $this->_buildModelInformation($fromModels);
  157. $toInfo = $this->_buildModelInformation($toModels);
  158. // Build array of changes between the from and to information
  159. $changes = $this->_buildChanges($fromInfo, $toInfo);
  160. $this->_cleanup();
  161. return $changes;
  162. }
  163. /**
  164. * Build array of changes between the from and to array of schema information
  165. *
  166. * @param array $from Array of schema information to generate changes from
  167. * @param array $to Array of schema information to generate changes for
  168. * @return array $changes
  169. */
  170. protected function _buildChanges($from, $to)
  171. {
  172. // Loop over the to schema information and compare it to the from
  173. foreach ($to as $className => $info) {
  174. // If the from doesn't have this class then it is a new table
  175. if ( ! isset($from[$className])) {
  176. $names = array('type', 'charset', 'collate', 'indexes', 'foreignKeys', 'primary');
  177. $options = array();
  178. foreach ($names as $name) {
  179. if (isset($info['options'][$name]) && $info['options'][$name]) {
  180. $options[$name] = $info['options'][$name];
  181. }
  182. }
  183. $table = array('tableName' => $info['tableName'],
  184. 'columns' => $info['columns'],
  185. 'options' => $options);
  186. $this->_changes['created_tables'][$info['tableName']] = $table;
  187. }
  188. // Check for new and changed columns
  189. foreach ($info['columns'] as $name => $column) {
  190. // If column doesn't exist in the from schema information then it is a new column
  191. if (isset($from[$className]) && ! isset($from[$className]['columns'][$name])) {
  192. $this->_changes['created_columns'][$info['tableName']][$name] = $column;
  193. }
  194. // If column exists in the from schema information but is not the same then it is a changed column
  195. if (isset($from[$className]['columns'][$name]) && $from[$className]['columns'][$name] != $column) {
  196. $this->_changes['changed_columns'][$info['tableName']][$name] = $column;
  197. }
  198. }
  199. // Check for new foreign keys
  200. foreach ($info['options']['foreignKeys'] as $name => $foreignKey) {
  201. $foreignKey['name'] = $name;
  202. // If foreign key doesn't exist in the from schema information then we need to add a index and the new fk
  203. if ( ! isset($from[$className]['options']['foreignKeys'][$name])) {
  204. $this->_changes['created_foreign_keys'][$info['tableName']][$name] = $foreignKey;
  205. $indexName = Doctrine_Manager::connection()->generateUniqueIndexName($info['tableName'], $foreignKey['local']);
  206. $this->_changes['created_indexes'][$info['tableName']][$indexName] = array('fields' => array($foreignKey['local']));
  207. // If foreign key does exist then lets see if anything has changed with it
  208. } else if (isset($from[$className]['options']['foreignKeys'][$name])) {
  209. $oldForeignKey = $from[$className]['options']['foreignKeys'][$name];
  210. $oldForeignKey['name'] = $name;
  211. // If the foreign key has changed any then we need to drop the foreign key and readd it
  212. if ($foreignKey !== $oldForeignKey) {
  213. $this->_changes['dropped_foreign_keys'][$info['tableName']][$name] = $oldForeignKey;
  214. $this->_changes['created_foreign_keys'][$info['tableName']][$name] = $foreignKey;
  215. }
  216. }
  217. }
  218. // Check for new indexes
  219. foreach ($info['options']['indexes'] as $name => $index) {
  220. // If index doesn't exist in the from schema information
  221. if ( ! isset($from[$className]['options']['indexes'][$name])) {
  222. $this->_changes['created_indexes'][$info['tableName']][$name] = $index;
  223. }
  224. }
  225. }
  226. // Loop over the from schema information and compare it to the to schema information
  227. foreach ($from as $className => $info) {
  228. // If the class exists in the from but not in the to then it is a dropped table
  229. if ( ! isset($to[$className])) {
  230. $table = array('tableName' => $info['tableName'],
  231. 'columns' => $info['columns'],
  232. 'options' => array('type' => $info['options']['type'],
  233. 'charset' => $info['options']['charset'],
  234. 'collate' => $info['options']['collate'],
  235. 'indexes' => $info['options']['indexes'],
  236. 'foreignKeys' => $info['options']['foreignKeys'],
  237. 'primary' => $info['options']['primary']));
  238. $this->_changes['dropped_tables'][$info['tableName']] = $table;
  239. }
  240. // Check for removed columns
  241. foreach ($info['columns'] as $name => $column) {
  242. // If column exists in the from but not in the to then we need to remove it
  243. if (isset($to[$className]) && ! isset($to[$className]['columns'][$name])) {
  244. $this->_changes['dropped_columns'][$info['tableName']][$name] = $column;
  245. }
  246. }
  247. // Check for dropped foreign keys
  248. foreach ($info['options']['foreignKeys'] as $name => $foreignKey) {
  249. // If the foreign key exists in the from but not in the to then we need to drop it
  250. if ( ! isset($to[$className]['options']['foreignKeys'][$name])) {
  251. $this->_changes['dropped_foreign_keys'][$info['tableName']][$name] = $foreignKey;
  252. }
  253. }
  254. // Check for removed indexes
  255. foreach ($info['options']['indexes'] as $name => $index) {
  256. // If the index exists in the from but not the to then we need to remove it
  257. if ( ! isset($to[$className]['options']['indexes'][$name])) {
  258. $this->_changes['dropped_indexes'][$info['tableName']][$name] = $index;
  259. }
  260. }
  261. }
  262. return $this->_changes;
  263. }
  264. /**
  265. * Build all the model schema information for the passed array of models
  266. *
  267. * @param array $models Array of models to build the schema information for
  268. * @return array $info Array of schema information for all the passed models
  269. */
  270. protected function _buildModelInformation(array $models)
  271. {
  272. $info = array();
  273. foreach ($models as $key => $model) {
  274. $table = Doctrine_Core::getTable($model);
  275. if ($table->getTableName() !== $this->_migration->getTableName()) {
  276. $info[$model] = $table->getExportableFormat();
  277. }
  278. }
  279. $info = $this->_cleanModelInformation($info);
  280. return $info;
  281. }
  282. /**
  283. * Clean the produced model information of any potential prefix text
  284. *
  285. * @param mixed $info Either array or string to clean of prefixes
  286. * @return mixed $info Cleaned value which is either an array or string
  287. */
  288. protected function _cleanModelInformation($info)
  289. {
  290. if (is_array($info)) {
  291. foreach ($info as $key => $value) {
  292. unset($info[$key]);
  293. $key = $this->_cleanModelInformation($key);
  294. $info[$key] = $this->_cleanModelInformation($value);
  295. }
  296. return $info;
  297. } else {
  298. $find = array(
  299. self::$_toPrefix,
  300. self::$_fromPrefix,
  301. Doctrine_Inflector::tableize(self::$_toPrefix) . '_',
  302. Doctrine_Inflector::tableize(self::$_fromPrefix) . '_',
  303. Doctrine_Inflector::tableize(self::$_toPrefix),
  304. Doctrine_Inflector::tableize(self::$_fromPrefix)
  305. );
  306. return str_replace($find, null, $info);
  307. }
  308. }
  309. /**
  310. * Get the extension of the type of file contained in a directory.
  311. * Used to determine if a directory contains YAML or PHP files.
  312. *
  313. * @param string $item
  314. * @return string $extension
  315. */
  316. protected function _getItemExtension($item)
  317. {
  318. if (is_dir($item)) {
  319. $files = glob($item . DIRECTORY_SEPARATOR . '*');
  320. } else {
  321. $files = array($item);
  322. }
  323. $extension = null;
  324. if (isset($files[0])) {
  325. if (is_dir($files[0])) {
  326. $extension = $this->_getItemExtension($files[0]);
  327. } else {
  328. $pathInfo = pathinfo($files[0]);
  329. $extension = $pathInfo['extension'];
  330. }
  331. }
  332. return $extension;
  333. }
  334. /**
  335. * Generate a set of models for the schema information source
  336. *
  337. * @param string $prefix Prefix to generate the models with
  338. * @param mixed $item The item to generate the models from
  339. * @return string $path The path where the models were generated
  340. * @throws Doctrine_Migration_Exception $e
  341. */
  342. protected function _generateModels($prefix, $item)
  343. {
  344. $path = $this->_tmpPath . DIRECTORY_SEPARATOR . strtolower($prefix) . '_doctrine_tmp_dirs';
  345. $options = array(
  346. 'classPrefix' => $prefix,
  347. 'generateBaseClasses' => false
  348. );
  349. if (is_string($item) && file_exists($item)) {
  350. $extension = $this->_getItemExtension($item);
  351. if ($extension === 'yml') {
  352. Doctrine_Core::generateModelsFromYaml($item, $path, $options);
  353. return $path;
  354. } else if ($extension === 'php') {
  355. Doctrine_Lib::copyDirectory($item, $path);
  356. return $path;
  357. } else {
  358. throw new Doctrine_Migration_Exception('No php or yml files found at path: "' . $item . '"');
  359. }
  360. } else {
  361. try {
  362. Doctrine_Core::generateModelsFromDb($path, (array) $item, $options);
  363. return $path;
  364. } catch (Exception $e) {
  365. throw new Doctrine_Migration_Exception('Could not generate models from connection: ' . $e->getMessage());
  366. }
  367. }
  368. }
  369. /**
  370. * Cleanup temporary generated models after a diff is performed
  371. *
  372. * @return void
  373. */
  374. protected function _cleanup()
  375. {
  376. $modelFiles = Doctrine_Core::getLoadedModelFiles();
  377. $filesToClean = array_diff($modelFiles, $this->_startingModelFiles);
  378. foreach ($filesToClean as $file) {
  379. if (file_exists($file)) {
  380. unlink($file);
  381. }
  382. }
  383. // clean up tmp directories
  384. Doctrine_Lib::removeDirectories($this->_tmpPath . DIRECTORY_SEPARATOR . strtolower(self::$_fromPrefix) . '_doctrine_tmp_dirs');
  385. Doctrine_Lib::removeDirectories($this->_tmpPath . DIRECTORY_SEPARATOR . strtolower(self::$_toPrefix) . '_doctrine_tmp_dirs');
  386. }
  387. }