PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/propel/propel1/generator/lib/model/diff/PropelTableComparator.php

https://gitlab.com/Isaki/le331.fr
PHP | 312 lines | 185 code | 32 blank | 95 comment | 24 complexity | d64602042dd3f826fb56c8b3ff59408f MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../Table.php';
  10. require_once dirname(__FILE__) . '/PropelTableDiff.php';
  11. require_once dirname(__FILE__) . '/PropelColumnComparator.php';
  12. require_once dirname(__FILE__) . '/PropelColumnDiff.php';
  13. require_once dirname(__FILE__) . '/PropelIndexComparator.php';
  14. require_once dirname(__FILE__) . '/PropelForeignKeyComparator.php';
  15. /**
  16. * Service class for comparing Table objects
  17. * Heavily inspired by Doctrine2's Migrations
  18. * (see http://github.com/doctrine/dbal/tree/master/lib/Doctrine/DBAL/Schema/)
  19. *
  20. * @package propel.generator.model.diff
  21. */
  22. class PropelTableComparator
  23. {
  24. protected $tableDiff;
  25. public function __construct($tableDiff = null)
  26. {
  27. $this->tableDiff = (null === $tableDiff) ? new PropelTableDiff() : $tableDiff;
  28. }
  29. public function getTableDiff()
  30. {
  31. return $this->tableDiff;
  32. }
  33. /**
  34. * Set the table the comparator starts from
  35. *
  36. * @param Table $fromTable
  37. */
  38. public function setFromTable(Table $fromTable)
  39. {
  40. $this->tableDiff->setFromTable($fromTable);
  41. }
  42. /**
  43. * Get the table the comparator starts from
  44. *
  45. * @return Table
  46. */
  47. public function getFromTable()
  48. {
  49. return $this->tableDiff->getFromTable();
  50. }
  51. /**
  52. * Set the table the comparator goes to
  53. *
  54. * @param Table $toTable
  55. */
  56. public function setToTable(Table $toTable)
  57. {
  58. $this->tableDiff->setToTable($toTable);
  59. }
  60. /**
  61. * Get the table the comparator goes to
  62. *
  63. * @return Table
  64. */
  65. public function getToTable()
  66. {
  67. return $this->tableDiff->getToTable();
  68. }
  69. /**
  70. * Compute and return the difference between two table objects
  71. *
  72. * @param Table $fromTable
  73. * @param Table $toTable
  74. * @param boolean $caseInsensitive Whether the comparison is case insensitive.
  75. * False by default.
  76. *
  77. * @return PropelTableDiff|boolean return false if the two tables are similar
  78. */
  79. public static function computeDiff(Table $fromTable, Table $toTable, $caseInsensitive = false)
  80. {
  81. $tc = new self();
  82. $tc->setFromTable($fromTable);
  83. $tc->setToTable($toTable);
  84. $differences = 0;
  85. $differences += $tc->compareColumns($caseInsensitive);
  86. $differences += $tc->comparePrimaryKeys($caseInsensitive);
  87. $differences += $tc->compareIndices($caseInsensitive);
  88. $differences += $tc->compareForeignKeys($caseInsensitive);
  89. return ($differences > 0) ? $tc->getTableDiff() : false;
  90. }
  91. /**
  92. * Compare the columns of the fromTable and the toTable,
  93. * and modifies the inner tableDiff if necessary.
  94. * Returns the number of differences.
  95. *
  96. * @param boolean $caseInsensitive Whether the comparison is case insensitive.
  97. * False by default.
  98. *
  99. * @return integer The number of column differences
  100. */
  101. public function compareColumns($caseInsensitive = false)
  102. {
  103. $fromTableColumns = $this->getFromTable()->getColumns();
  104. $toTableColumns = $this->getToTable()->getColumns();
  105. $columnDifferences = 0;
  106. // check for new columns in $toTable
  107. foreach ($toTableColumns as $column) {
  108. if (!$this->getFromTable()->hasColumn($column->getName(), $caseInsensitive)) {
  109. $this->tableDiff->addAddedColumn($column->getName(), $column);
  110. $columnDifferences++;
  111. }
  112. }
  113. // check for removed columns in $toTable
  114. foreach ($fromTableColumns as $column) {
  115. if (!$this->getToTable()->hasColumn($column->getName(), $caseInsensitive)) {
  116. $this->tableDiff->addRemovedColumn($column->getName(), $column);
  117. $columnDifferences++;
  118. }
  119. }
  120. // check for column differences
  121. foreach ($fromTableColumns as $fromColumn) {
  122. if ($this->getToTable()->hasColumn($fromColumn->getName(), $caseInsensitive)) {
  123. $toColumn = $this->getToTable()->getColumn($fromColumn->getName(), $caseInsensitive);
  124. $columnDiff = PropelColumnComparator::computeDiff($fromColumn, $toColumn);
  125. if ($columnDiff) {
  126. $this->tableDiff->addModifiedColumn($fromColumn->getName(), $columnDiff);
  127. $columnDifferences++;
  128. }
  129. }
  130. }
  131. // check for column renamings
  132. foreach ($this->tableDiff->getAddedColumns() as $addedColumnName => $addedColumn) {
  133. foreach ($this->tableDiff->getRemovedColumns() as $removedColumnName => $removedColumn) {
  134. if (!PropelColumnComparator::computeDiff($addedColumn, $removedColumn)) {
  135. // no difference except the name, that's probably a renaming
  136. $this->tableDiff->addRenamedColumn($removedColumn, $addedColumn);
  137. $this->tableDiff->removeAddedColumn($addedColumnName);
  138. $this->tableDiff->removeRemovedColumn($removedColumnName);
  139. $columnDifferences--;
  140. }
  141. }
  142. }
  143. return $columnDifferences;
  144. }
  145. /**
  146. * Compare the primary keys of the fromTable and the toTable,
  147. * and modifies the inner tableDiff if necessary.
  148. * Returns the number of differences.
  149. *
  150. * @param boolean $caseInsensitive Whether the comparison is case insensitive.
  151. * False by default.
  152. *
  153. * @return integer The number of primary key differences
  154. */
  155. public function comparePrimaryKeys($caseInsensitive = false)
  156. {
  157. $pkDifferences = 0;
  158. $fromTablePk = $this->getFromTable()->getPrimaryKey();
  159. $toTablePk = $this->getToTable()->getPrimaryKey();
  160. // check for new pk columns in $toTable
  161. foreach ($toTablePk as $column) {
  162. if (!$this->getFromTable()->hasColumn($column->getName(), $caseInsensitive) || !$this->getFromTable()->getColumn($column->getName(), $caseInsensitive)->isPrimaryKey()) {
  163. $this->tableDiff->addAddedPkColumn($column->getName(), $column);
  164. $pkDifferences++;
  165. }
  166. }
  167. // check for removed pk columns in $toTable
  168. foreach ($fromTablePk as $column) {
  169. if (!$this->getToTable()->hasColumn($column->getName(), $caseInsensitive) || !$this->getToTable()->getColumn($column->getName(), $caseInsensitive)->isPrimaryKey()) {
  170. $this->tableDiff->addRemovedPkColumn($column->getName(), $column);
  171. $pkDifferences++;
  172. }
  173. }
  174. // check for column renamings
  175. foreach ($this->tableDiff->getAddedPkColumns() as $addedColumnName => $addedColumn) {
  176. foreach ($this->tableDiff->getRemovedPkColumns() as $removedColumnName => $removedColumn) {
  177. if (!PropelColumnComparator::computeDiff($addedColumn, $removedColumn)) {
  178. // no difference except the name, that's probably a renaming
  179. $this->tableDiff->addRenamedPkColumn($removedColumn, $addedColumn);
  180. $this->tableDiff->removeAddedPkColumn($addedColumnName);
  181. $this->tableDiff->removeRemovedPkColumn($removedColumnName);
  182. $pkDifferences--;
  183. }
  184. }
  185. }
  186. return $pkDifferences;
  187. }
  188. /**
  189. * Compare the indices and unique indices of the fromTable and the toTable,
  190. * and modifies the inner tableDiff if necessary.
  191. * Returns the number of differences.
  192. *
  193. * @param boolean $caseInsensitive Whether the comparison is case insensitive.
  194. * False by default.
  195. *
  196. * @return integer The number of index differences
  197. */
  198. public function compareIndices($caseInsensitive = false)
  199. {
  200. $indexDifferences = 0;
  201. $fromTableIndices = array_merge($this->getFromTable()->getIndices(), $this->getFromTable()->getUnices());
  202. $toTableIndices = array_merge($this->getToTable()->getIndices(), $this->getToTable()->getUnices());
  203. foreach ($toTableIndices as $toTableIndexPos => $toTableIndex) {
  204. foreach ($fromTableIndices as $fromTableIndexPos => $fromTableIndex) {
  205. if (PropelIndexComparator::computeDiff($fromTableIndex, $toTableIndex, $caseInsensitive) === false) {
  206. unset($fromTableIndices[$fromTableIndexPos]);
  207. unset($toTableIndices[$toTableIndexPos]);
  208. } else {
  209. $test = $caseInsensitive ?
  210. strtolower($fromTableIndex->getName()) == strtolower($toTableIndex->getName()) :
  211. $fromTableIndex->getName() == $toTableIndex->getName();
  212. if ($test) {
  213. // same name, but different columns
  214. $this->tableDiff->addModifiedIndex($fromTableIndex->getName(), $fromTableIndex, $toTableIndex);
  215. unset($fromTableIndices[$fromTableIndexPos]);
  216. unset($toTableIndices[$toTableIndexPos]);
  217. $indexDifferences++;
  218. }
  219. }
  220. }
  221. }
  222. foreach ($fromTableIndices as $fromTableIndexPos => $fromTableIndex) {
  223. $this->tableDiff->addRemovedIndex($fromTableIndex->getName(), $fromTableIndex);
  224. $indexDifferences++;
  225. }
  226. foreach ($toTableIndices as $toTableIndexPos => $toTableIndex) {
  227. $this->tableDiff->addAddedIndex($toTableIndex->getName(), $toTableIndex);
  228. $indexDifferences++;
  229. }
  230. return $indexDifferences;
  231. }
  232. /**
  233. * Compare the foreign keys of the fromTable and the toTable,
  234. * and modifies the inner tableDiff if necessary.
  235. * Returns the number of differences.
  236. *
  237. * @param boolean $caseInsensitive Whether the comparison is case insensitive.
  238. * False by default.
  239. *
  240. * @return integer The number of foreign key differences
  241. */
  242. public function compareForeignKeys($caseInsensitive = false)
  243. {
  244. $fkDifferences = 0;
  245. $fromTableFks = $this->getFromTable()->getForeignKeys();
  246. $toTableFks = $this->getToTable()->getForeignKeys();
  247. foreach ($fromTableFks as $fromTableFkPos => $fromTableFk) {
  248. foreach ($toTableFks as $toTableFkPos => $toTableFk) {
  249. if (PropelForeignKeyComparator::computeDiff($fromTableFk, $toTableFk, $caseInsensitive) === false) {
  250. unset($fromTableFks[$fromTableFkPos]);
  251. unset($toTableFks[$toTableFkPos]);
  252. } else {
  253. $test = $caseInsensitive ?
  254. strtolower($fromTableFk->getName()) == strtolower($toTableFk->getName()) :
  255. $fromTableFk->getName() == $toTableFk->getName();
  256. if ($test) {
  257. // same name, but different columns
  258. $this->tableDiff->addModifiedFk($fromTableFk->getName(), $fromTableFk, $toTableFk);
  259. unset($fromTableFks[$fromTableFkPos]);
  260. unset($toTableFks[$toTableFkPos]);
  261. $fkDifferences++;
  262. }
  263. }
  264. }
  265. }
  266. foreach ($fromTableFks as $fromTableFkPos => $fromTableFk) {
  267. if (!$fromTableFk->isSkipSql() && !in_array($fromTableFk, $toTableFks)) {
  268. $this->tableDiff->addRemovedFk($fromTableFk->getName(), $fromTableFk);
  269. $fkDifferences++;
  270. }
  271. }
  272. foreach ($toTableFks as $toTableFkPos => $toTableFk) {
  273. if (!$toTableFk->isSkipSql() && !in_array($toTableFk, $fromTableFks)) {
  274. $this->tableDiff->addAddedFk($toTableFk->getName(), $toTableFk);
  275. $fkDifferences++;
  276. }
  277. }
  278. return $fkDifferences;
  279. }
  280. }