Comparator.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. /**
  21. * Compare to Schemas and return an instance of SchemaDiff
  22. *
  23. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  24. * @license http://ez.no/licenses/new_bsd New BSD License
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @version $Revision$
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. */
  31. class Comparator
  32. {
  33. /**
  34. * @param Schema $fromSchema
  35. * @param Schema $toSchema
  36. * @return SchemaDiff
  37. */
  38. static public function compareSchemas( Schema $fromSchema, Schema $toSchema )
  39. {
  40. $c = new self();
  41. return $c->compare($fromSchema, $toSchema);
  42. }
  43. /**
  44. * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
  45. *
  46. * The returned diferences are returned in such a way that they contain the
  47. * operations to change the schema stored in $fromSchema to the schema that is
  48. * stored in $toSchema.
  49. *
  50. * @param Schema $fromSchema
  51. * @param Schema $toSchema
  52. *
  53. * @return SchemaDiff
  54. */
  55. public function compare(Schema $fromSchema, Schema $toSchema)
  56. {
  57. $diff = new SchemaDiff();
  58. $foreignKeysToTable = array();
  59. foreach ( $toSchema->getTables() AS $table ) {
  60. $tableName = $table->getShortestName($toSchema->getName());
  61. if ( ! $fromSchema->hasTable($tableName)) {
  62. $diff->newTables[$tableName] = $toSchema->getTable($tableName);
  63. } else {
  64. $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName));
  65. if ($tableDifferences !== false) {
  66. $diff->changedTables[$tableName] = $tableDifferences;
  67. }
  68. }
  69. }
  70. /* Check if there are tables removed */
  71. foreach ($fromSchema->getTables() AS $table) {
  72. $tableName = $table->getShortestName($fromSchema->getName());
  73. $table = $fromSchema->getTable($tableName);
  74. if ( ! $toSchema->hasTable($tableName) ) {
  75. $diff->removedTables[$tableName] = $table;
  76. }
  77. // also remember all foreign keys that point to a specific table
  78. foreach ($table->getForeignKeys() AS $foreignKey) {
  79. $foreignTable = strtolower($foreignKey->getForeignTableName());
  80. if (!isset($foreignKeysToTable[$foreignTable])) {
  81. $foreignKeysToTable[$foreignTable] = array();
  82. }
  83. $foreignKeysToTable[$foreignTable][] = $foreignKey;
  84. }
  85. }
  86. foreach ($diff->removedTables AS $tableName => $table) {
  87. if (isset($foreignKeysToTable[$tableName])) {
  88. $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
  89. }
  90. }
  91. foreach ($toSchema->getSequences() AS $sequence) {
  92. $sequenceName = $sequence->getShortestName($toSchema->getName());
  93. if (!$fromSchema->hasSequence($sequenceName)) {
  94. $diff->newSequences[] = $sequence;
  95. } else {
  96. if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
  97. $diff->changedSequences[] = $toSchema->getSequence($sequenceName);
  98. }
  99. }
  100. }
  101. foreach ($fromSchema->getSequences() AS $sequence) {
  102. $sequenceName = $sequence->getShortestName($fromSchema->getName());
  103. if (!$toSchema->hasSequence($sequenceName)) {
  104. $diff->removedSequences[] = $sequence;
  105. }
  106. }
  107. return $diff;
  108. }
  109. /**
  110. *
  111. * @param Sequence $sequence1
  112. * @param Sequence $sequence2
  113. */
  114. public function diffSequence(Sequence $sequence1, Sequence $sequence2)
  115. {
  116. if($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) {
  117. return true;
  118. }
  119. if($sequence1->getInitialValue() != $sequence2->getInitialValue()) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Returns the difference between the tables $table1 and $table2.
  126. *
  127. * If there are no differences this method returns the boolean false.
  128. *
  129. * @param Table $table1
  130. * @param Table $table2
  131. *
  132. * @return bool|TableDiff
  133. */
  134. public function diffTable(Table $table1, Table $table2)
  135. {
  136. $changes = 0;
  137. $tableDifferences = new TableDiff($table1->getName());
  138. $table1Columns = $table1->getColumns();
  139. $table2Columns = $table2->getColumns();
  140. /* See if all the fields in table 1 exist in table 2 */
  141. foreach ( $table2Columns as $columnName => $column ) {
  142. if ( !$table1->hasColumn($columnName) ) {
  143. $tableDifferences->addedColumns[$columnName] = $column;
  144. $changes++;
  145. }
  146. }
  147. /* See if there are any removed fields in table 2 */
  148. foreach ( $table1Columns as $columnName => $column ) {
  149. if ( !$table2->hasColumn($columnName) ) {
  150. $tableDifferences->removedColumns[$columnName] = $column;
  151. $changes++;
  152. }
  153. }
  154. foreach ( $table1Columns as $columnName => $column ) {
  155. if ( $table2->hasColumn($columnName) ) {
  156. $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
  157. if (count($changedProperties) ) {
  158. $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
  159. $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
  160. $changes++;
  161. }
  162. }
  163. }
  164. $this->detectColumnRenamings($tableDifferences);
  165. $table1Indexes = $table1->getIndexes();
  166. $table2Indexes = $table2->getIndexes();
  167. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  168. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  169. if ($this->diffIndex($index1Definition, $index2Definition) === false) {
  170. unset($table1Indexes[$index1Name]);
  171. unset($table2Indexes[$index2Name]);
  172. } else {
  173. if ($index1Name == $index2Name) {
  174. $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name];
  175. unset($table1Indexes[$index1Name]);
  176. unset($table2Indexes[$index2Name]);
  177. $changes++;
  178. }
  179. }
  180. }
  181. }
  182. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  183. $tableDifferences->removedIndexes[$index1Name] = $index1Definition;
  184. $changes++;
  185. }
  186. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  187. $tableDifferences->addedIndexes[$index2Name] = $index2Definition;
  188. $changes++;
  189. }
  190. $fromFkeys = $table1->getForeignKeys();
  191. $toFkeys = $table2->getForeignKeys();
  192. foreach ($fromFkeys AS $key1 => $constraint1) {
  193. foreach ($toFkeys AS $key2 => $constraint2) {
  194. if($this->diffForeignKey($constraint1, $constraint2) === false) {
  195. unset($fromFkeys[$key1]);
  196. unset($toFkeys[$key2]);
  197. } else {
  198. if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) {
  199. $tableDifferences->changedForeignKeys[] = $constraint2;
  200. $changes++;
  201. unset($fromFkeys[$key1]);
  202. unset($toFkeys[$key2]);
  203. }
  204. }
  205. }
  206. }
  207. foreach ($fromFkeys AS $key1 => $constraint1) {
  208. $tableDifferences->removedForeignKeys[] = $constraint1;
  209. $changes++;
  210. }
  211. foreach ($toFkeys AS $key2 => $constraint2) {
  212. $tableDifferences->addedForeignKeys[] = $constraint2;
  213. $changes++;
  214. }
  215. return $changes ? $tableDifferences : false;
  216. }
  217. /**
  218. * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
  219. * however ambiguouties between different possibilites should not lead to renaming at all.
  220. *
  221. * @param TableDiff $tableDifferences
  222. */
  223. private function detectColumnRenamings(TableDiff $tableDifferences)
  224. {
  225. $renameCandidates = array();
  226. foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) {
  227. foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) {
  228. if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) {
  229. $renameCandidates[$addedColumn->getName()][] = array($removedColumn, $addedColumn, $addedColumnName);
  230. }
  231. }
  232. }
  233. foreach ($renameCandidates AS $candidate => $candidateColumns) {
  234. if (count($candidateColumns) == 1) {
  235. list($removedColumn, $addedColumn) = $candidateColumns[0];
  236. $removedColumnName = strtolower($removedColumn->getName());
  237. $addedColumnName = strtolower($addedColumn->getName());
  238. $tableDifferences->renamedColumns[$removedColumnName] = $addedColumn;
  239. unset($tableDifferences->addedColumns[$addedColumnName]);
  240. unset($tableDifferences->removedColumns[$removedColumnName]);
  241. }
  242. }
  243. }
  244. /**
  245. * @param ForeignKeyConstraint $key1
  246. * @param ForeignKeyConstraint $key2
  247. * @return bool
  248. */
  249. public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
  250. {
  251. if (array_map('strtolower', $key1->getLocalColumns()) != array_map('strtolower', $key2->getLocalColumns())) {
  252. return true;
  253. }
  254. if (array_map('strtolower', $key1->getForeignColumns()) != array_map('strtolower', $key2->getForeignColumns())) {
  255. return true;
  256. }
  257. if ($key1->onUpdate() != $key2->onUpdate()) {
  258. return true;
  259. }
  260. if ($key1->onDelete() != $key2->onDelete()) {
  261. return true;
  262. }
  263. return false;
  264. }
  265. /**
  266. * Returns the difference between the fields $field1 and $field2.
  267. *
  268. * If there are differences this method returns $field2, otherwise the
  269. * boolean false.
  270. *
  271. * @param Column $column1
  272. * @param Column $column2
  273. *
  274. * @return array
  275. */
  276. public function diffColumn(Column $column1, Column $column2)
  277. {
  278. $changedProperties = array();
  279. if ( $column1->getType() != $column2->getType() ) {
  280. $changedProperties[] = 'type';
  281. }
  282. if ($column1->getNotnull() != $column2->getNotnull()) {
  283. $changedProperties[] = 'notnull';
  284. }
  285. if ($column1->getDefault() != $column2->getDefault()) {
  286. $changedProperties[] = 'default';
  287. }
  288. if ($column1->getUnsigned() != $column2->getUnsigned()) {
  289. $changedProperties[] = 'unsigned';
  290. }
  291. if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
  292. // check if value of length is set at all, default value assumed otherwise.
  293. $length1 = $column1->getLength() ?: 255;
  294. $length2 = $column2->getLength() ?: 255;
  295. if ($length1 != $length2) {
  296. $changedProperties[] = 'length';
  297. }
  298. if ($column1->getFixed() != $column2->getFixed()) {
  299. $changedProperties[] = 'fixed';
  300. }
  301. }
  302. if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
  303. if (($column1->getPrecision()?:10) != ($column2->getPrecision()?:10)) {
  304. $changedProperties[] = 'precision';
  305. }
  306. if ($column1->getScale() != $column2->getScale()) {
  307. $changedProperties[] = 'scale';
  308. }
  309. }
  310. if ($column1->getAutoincrement() != $column2->getAutoincrement()) {
  311. $changedProperties[] = 'autoincrement';
  312. }
  313. // only allow to delete comment if its set to '' not to null.
  314. if ($column1->getComment() !== null && $column1->getComment() != $column2->getComment()) {
  315. $changedProperties[] = 'comment';
  316. }
  317. $options1 = $column1->getCustomSchemaOptions();
  318. $options2 = $column2->getCustomSchemaOptions();
  319. $commonKeys = array_keys(array_intersect_key($options1, $options2));
  320. foreach ($commonKeys as $key) {
  321. if ($options1[$key] !== $options2[$key]) {
  322. $changedProperties[] = $key;
  323. }
  324. }
  325. $diffKeys = array_keys(array_diff_key($options1, $options2) + array_diff_key($options2, $options1));
  326. $changedProperties = array_merge($changedProperties, $diffKeys);
  327. return $changedProperties;
  328. }
  329. /**
  330. * Finds the difference between the indexes $index1 and $index2.
  331. *
  332. * Compares $index1 with $index2 and returns $index2 if there are any
  333. * differences or false in case there are no differences.
  334. *
  335. * @param Index $index1
  336. * @param Index $index2
  337. * @return bool
  338. */
  339. public function diffIndex(Index $index1, Index $index2)
  340. {
  341. if ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)) {
  342. return false;
  343. }
  344. return true;
  345. }
  346. }