PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/DoctrineORM-2.0.0/Doctrine/DBAL/Schema/Comparator.php

https://github.com/sentact/vendor-doctrine2
PHP | 395 lines | 246 code | 48 blank | 101 comment | 71 complexity | 478b89939f6df1880cd8a4419b93d2fc MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * $Id$
  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. namespace Doctrine\DBAL\Schema;
  22. /**
  23. * Compare to Schemas and return an instance of SchemaDiff
  24. *
  25. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  26. * @license http://ez.no/licenses/new_bsd New BSD License
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 2.0
  30. * @version $Revision$
  31. * @author Benjamin Eberlei <kontakt@beberlei.de>
  32. */
  33. class Comparator
  34. {
  35. /**
  36. * @var array
  37. */
  38. private $_checkColumnPlatformOptions = array();
  39. /**
  40. * @param string $optionName
  41. */
  42. public function addColumnPlatformOptionCheck($optionName)
  43. {
  44. $this->_checkColumnPlatformOptions[] = $optionName;
  45. }
  46. /**
  47. * @param Schema $fromSchema
  48. * @param Schema $toSchema
  49. * @return SchemaDiff
  50. */
  51. static public function compareSchemas( Schema $fromSchema, Schema $toSchema )
  52. {
  53. $c = new self();
  54. return $c->compare($fromSchema, $toSchema);
  55. }
  56. /**
  57. * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
  58. *
  59. * The returned diferences are returned in such a way that they contain the
  60. * operations to change the schema stored in $fromSchema to the schema that is
  61. * stored in $toSchema.
  62. *
  63. * @param Schema $fromSchema
  64. * @param Schema $toSchema
  65. *
  66. * @return SchemaDiff
  67. */
  68. public function compare(Schema $fromSchema, Schema $toSchema)
  69. {
  70. if ($fromSchema->hasExplicitForeignKeyIndexes() && !$toSchema->hasExplicitForeignKeyIndexes()) {
  71. $toSchema->visit(new \Doctrine\DBAL\Schema\Visitor\FixSchema(true));
  72. }
  73. $diff = new SchemaDiff();
  74. $foreignKeysToTable = array();
  75. foreach ( $toSchema->getTables() AS $tableName => $table ) {
  76. if ( !$fromSchema->hasTable($tableName) ) {
  77. $diff->newTables[$tableName] = $table;
  78. } else {
  79. $tableDifferences = $this->diffTable( $fromSchema->getTable($tableName), $table );
  80. if ( $tableDifferences !== false ) {
  81. $diff->changedTables[$tableName] = $tableDifferences;
  82. }
  83. }
  84. }
  85. /* Check if there are tables removed */
  86. foreach ( $fromSchema->getTables() AS $tableName => $table ) {
  87. if ( !$toSchema->hasTable($tableName) ) {
  88. $diff->removedTables[$tableName] = $table;
  89. }
  90. // also remember all foreign keys that point to a specific table
  91. foreach ($table->getForeignKeys() AS $foreignKey) {
  92. $foreignTable = strtolower($foreignKey->getForeignTableName());
  93. if (!isset($foreignKeysToTable[$foreignTable])) {
  94. $foreignKeysToTable[$foreignTable] = array();
  95. }
  96. $foreignKeysToTable[$foreignTable][] = $foreignKey;
  97. }
  98. }
  99. foreach ($diff->removedTables AS $tableName => $table) {
  100. if (isset($foreignKeysToTable[$tableName])) {
  101. $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
  102. }
  103. }
  104. foreach ( $toSchema->getSequences() AS $sequenceName => $sequence) {
  105. if (!$fromSchema->hasSequence($sequenceName)) {
  106. $diff->newSequences[] = $sequence;
  107. } else {
  108. if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
  109. $diff->changedSequences[] = $fromSchema->getSequence($sequenceName);
  110. }
  111. }
  112. }
  113. foreach ($fromSchema->getSequences() AS $sequenceName => $sequence) {
  114. if (!$toSchema->hasSequence($sequenceName)) {
  115. $diff->removedSequences[] = $sequence;
  116. }
  117. }
  118. return $diff;
  119. }
  120. /**
  121. *
  122. * @param Sequence $sequence1
  123. * @param Sequence $sequence2
  124. */
  125. public function diffSequence(Sequence $sequence1, Sequence $sequence2)
  126. {
  127. if($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) {
  128. return true;
  129. }
  130. if($sequence1->getInitialValue() != $sequence2->getInitialValue()) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * Returns the difference between the tables $table1 and $table2.
  137. *
  138. * If there are no differences this method returns the boolean false.
  139. *
  140. * @param Table $table1
  141. * @param Table $table2
  142. *
  143. * @return bool|TableDiff
  144. */
  145. public function diffTable(Table $table1, Table $table2)
  146. {
  147. $changes = 0;
  148. $tableDifferences = new TableDiff($table1->getName());
  149. $table1Columns = $table1->getColumns();
  150. $table2Columns = $table2->getColumns();
  151. /* See if all the fields in table 1 exist in table 2 */
  152. foreach ( $table2Columns as $columnName => $column ) {
  153. if ( !$table1->hasColumn($columnName) ) {
  154. $tableDifferences->addedColumns[$columnName] = $column;
  155. $changes++;
  156. }
  157. }
  158. /* See if there are any removed fields in table 2 */
  159. foreach ( $table1Columns as $columnName => $column ) {
  160. if ( !$table2->hasColumn($columnName) ) {
  161. $tableDifferences->removedColumns[$columnName] = $column;
  162. $changes++;
  163. }
  164. }
  165. foreach ( $table1Columns as $columnName => $column ) {
  166. if ( $table2->hasColumn($columnName) ) {
  167. $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
  168. if (count($changedProperties) ) {
  169. $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
  170. $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
  171. $changes++;
  172. }
  173. }
  174. }
  175. // Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
  176. foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) {
  177. foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) {
  178. if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) {
  179. $tableDifferences->renamedColumns[$removedColumn->getName()] = $addedColumn;
  180. unset($tableDifferences->addedColumns[$addedColumnName]);
  181. unset($tableDifferences->removedColumns[$removedColumnName]);
  182. }
  183. }
  184. }
  185. $table1Indexes = $table1->getIndexes();
  186. $table2Indexes = $table2->getIndexes();
  187. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  188. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  189. if ($this->diffIndex($index1Definition, $index2Definition) === false) {
  190. unset($table1Indexes[$index1Name]);
  191. unset($table2Indexes[$index2Name]);
  192. } else {
  193. if ($index1Name == $index2Name) {
  194. $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name];
  195. unset($table1Indexes[$index1Name]);
  196. unset($table2Indexes[$index2Name]);
  197. $changes++;
  198. }
  199. }
  200. }
  201. }
  202. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  203. $tableDifferences->removedIndexes[$index1Name] = $index1Definition;
  204. $changes++;
  205. }
  206. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  207. $tableDifferences->addedIndexes[$index2Name] = $index2Definition;
  208. $changes++;
  209. }
  210. $fromFkeys = $table1->getForeignKeys();
  211. $toFkeys = $table2->getForeignKeys();
  212. foreach ($fromFkeys AS $key1 => $constraint1) {
  213. foreach ($toFkeys AS $key2 => $constraint2) {
  214. if($this->diffForeignKey($constraint1, $constraint2) === false) {
  215. unset($fromFkeys[$key1]);
  216. unset($toFkeys[$key2]);
  217. } else {
  218. if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) {
  219. $tableDifferences->changedForeignKeys[] = $constraint2;
  220. $changes++;
  221. unset($fromFkeys[$key1]);
  222. unset($toFkeys[$key2]);
  223. }
  224. }
  225. }
  226. }
  227. foreach ($fromFkeys AS $key1 => $constraint1) {
  228. $tableDifferences->removedForeignKeys[] = $constraint1;
  229. $changes++;
  230. }
  231. foreach ($toFkeys AS $key2 => $constraint2) {
  232. $tableDifferences->addedForeignKeys[] = $constraint2;
  233. $changes++;
  234. }
  235. return $changes ? $tableDifferences : false;
  236. }
  237. /**
  238. * @param ForeignKeyConstraint $key1
  239. * @param ForeignKeyConstraint $key2
  240. * @return bool
  241. */
  242. public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
  243. {
  244. if (array_map('strtolower', $key1->getLocalColumns()) != array_map('strtolower', $key2->getLocalColumns())) {
  245. return true;
  246. }
  247. if (array_map('strtolower', $key1->getForeignColumns()) != array_map('strtolower', $key2->getForeignColumns())) {
  248. return true;
  249. }
  250. if ($key1->onUpdate() != $key2->onUpdate()) {
  251. return true;
  252. }
  253. if ($key1->onDelete() != $key2->onDelete()) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. /**
  259. * Returns the difference between the fields $field1 and $field2.
  260. *
  261. * If there are differences this method returns $field2, otherwise the
  262. * boolean false.
  263. *
  264. * @param Column $column1
  265. * @param Column $column2
  266. *
  267. * @return array
  268. */
  269. public function diffColumn(Column $column1, Column $column2)
  270. {
  271. $changedProperties = array();
  272. if ( $column1->getType() != $column2->getType() ) {
  273. $changedProperties[] = 'type';
  274. }
  275. if ($column1->getNotnull() != $column2->getNotnull()) {
  276. $changedProperties[] = 'notnull';
  277. }
  278. if ($column1->getDefault() != $column2->getDefault()) {
  279. $changedProperties[] = 'default';
  280. }
  281. if ($column1->getUnsigned() != $column2->getUnsigned()) {
  282. $changedProperties[] = 'unsigned';
  283. }
  284. if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
  285. if ($column1->getLength() != $column2->getLength()) {
  286. $changedProperties[] = 'length';
  287. }
  288. if ($column1->getFixed() != $column2->getFixed()) {
  289. $changedProperties[] = 'fixed';
  290. }
  291. }
  292. if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
  293. if ($column1->getPrecision() != $column2->getPrecision()) {
  294. $changedProperties[] = 'precision';
  295. }
  296. if ($column1->getScale() != $column2->getScale()) {
  297. $changedProperties[] = 'scale';
  298. }
  299. }
  300. foreach ($this->_checkColumnPlatformOptions AS $optionName) {
  301. if($column1->hasPlatformOption($optionName) && $column2->hasPlatformOption($optionName)) {
  302. if ($column1->getPlatformOption($optionName) != $column2->getPlatformOption($optionName)) {
  303. $changedProperties[] = $optionName;
  304. }
  305. } else if ($column1->hasPlatformOption($optionName) != $column2->hasPlatformOption($optionName)) {
  306. $changedProperties[] = $optionName;
  307. }
  308. }
  309. return $changedProperties;
  310. }
  311. /**
  312. * Finds the difference between the indexes $index1 and $index2.
  313. *
  314. * Compares $index1 with $index2 and returns $index2 if there are any
  315. * differences or false in case there are no differences.
  316. *
  317. * @param Index $index1
  318. * @param Index $index2
  319. * @return bool
  320. */
  321. public function diffIndex(Index $index1, Index $index2)
  322. {
  323. if($index1->isPrimary() != $index2->isPrimary()) {
  324. return true;
  325. }
  326. if($index1->isUnique() != $index2->isUnique()) {
  327. return true;
  328. }
  329. // Check for removed index fields in $index2
  330. $index1Columns = $index1->getColumns();
  331. for($i = 0; $i < count($index1Columns); $i++) {
  332. $indexColumn = $index1Columns[$i];
  333. if (!$index2->hasColumnAtPosition($indexColumn, $i)) {
  334. return true;
  335. }
  336. }
  337. // Check for new index fields in $index2
  338. $index2Columns = $index2->getColumns();
  339. for($i = 0; $i < count($index2Columns); $i++) {
  340. $indexColumn = $index2Columns[$i];
  341. if (!$index1->hasColumnAtPosition($indexColumn, $i)) {
  342. return true;
  343. }
  344. }
  345. return false;
  346. }
  347. }