Comparator.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 $tableName => $table ) {
  60. if ( !$fromSchema->hasTable($tableName) ) {
  61. $diff->newTables[$tableName] = $table;
  62. } else {
  63. $tableDifferences = $this->diffTable( $fromSchema->getTable($tableName), $table );
  64. if ( $tableDifferences !== false ) {
  65. $diff->changedTables[$tableName] = $tableDifferences;
  66. }
  67. }
  68. }
  69. /* Check if there are tables removed */
  70. foreach ( $fromSchema->getTables() AS $tableName => $table ) {
  71. if ( !$toSchema->hasTable($tableName) ) {
  72. $diff->removedTables[$tableName] = $table;
  73. }
  74. // also remember all foreign keys that point to a specific table
  75. foreach ($table->getForeignKeys() AS $foreignKey) {
  76. $foreignTable = strtolower($foreignKey->getForeignTableName());
  77. if (!isset($foreignKeysToTable[$foreignTable])) {
  78. $foreignKeysToTable[$foreignTable] = array();
  79. }
  80. $foreignKeysToTable[$foreignTable][] = $foreignKey;
  81. }
  82. }
  83. foreach ($diff->removedTables AS $tableName => $table) {
  84. if (isset($foreignKeysToTable[$tableName])) {
  85. $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
  86. }
  87. }
  88. foreach ( $toSchema->getSequences() AS $sequenceName => $sequence) {
  89. if (!$fromSchema->hasSequence($sequenceName)) {
  90. $diff->newSequences[] = $sequence;
  91. } else {
  92. if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
  93. $diff->changedSequences[] = $toSchema->getSequence($sequenceName);
  94. }
  95. }
  96. }
  97. foreach ($fromSchema->getSequences() AS $sequenceName => $sequence) {
  98. if (!$toSchema->hasSequence($sequenceName)) {
  99. $diff->removedSequences[] = $sequence;
  100. }
  101. }
  102. return $diff;
  103. }
  104. /**
  105. *
  106. * @param Sequence $sequence1
  107. * @param Sequence $sequence2
  108. */
  109. public function diffSequence(Sequence $sequence1, Sequence $sequence2)
  110. {
  111. if($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) {
  112. return true;
  113. }
  114. if($sequence1->getInitialValue() != $sequence2->getInitialValue()) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Returns the difference between the tables $table1 and $table2.
  121. *
  122. * If there are no differences this method returns the boolean false.
  123. *
  124. * @param Table $table1
  125. * @param Table $table2
  126. *
  127. * @return bool|TableDiff
  128. */
  129. public function diffTable(Table $table1, Table $table2)
  130. {
  131. $changes = 0;
  132. $tableDifferences = new TableDiff($table1->getName());
  133. $table1Columns = $table1->getColumns();
  134. $table2Columns = $table2->getColumns();
  135. /* See if all the fields in table 1 exist in table 2 */
  136. foreach ( $table2Columns as $columnName => $column ) {
  137. if ( !$table1->hasColumn($columnName) ) {
  138. $tableDifferences->addedColumns[$columnName] = $column;
  139. $changes++;
  140. }
  141. }
  142. /* See if there are any removed fields in table 2 */
  143. foreach ( $table1Columns as $columnName => $column ) {
  144. if ( !$table2->hasColumn($columnName) ) {
  145. $tableDifferences->removedColumns[$columnName] = $column;
  146. $changes++;
  147. }
  148. }
  149. foreach ( $table1Columns as $columnName => $column ) {
  150. if ( $table2->hasColumn($columnName) ) {
  151. $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
  152. if (count($changedProperties) ) {
  153. $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
  154. $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
  155. $changes++;
  156. }
  157. }
  158. }
  159. $this->detectColumnRenamings($tableDifferences);
  160. $table1Indexes = $table1->getIndexes();
  161. $table2Indexes = $table2->getIndexes();
  162. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  163. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  164. if ($this->diffIndex($index1Definition, $index2Definition) === false) {
  165. unset($table1Indexes[$index1Name]);
  166. unset($table2Indexes[$index2Name]);
  167. } else {
  168. if ($index1Name == $index2Name) {
  169. $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name];
  170. unset($table1Indexes[$index1Name]);
  171. unset($table2Indexes[$index2Name]);
  172. $changes++;
  173. }
  174. }
  175. }
  176. }
  177. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  178. $tableDifferences->removedIndexes[$index1Name] = $index1Definition;
  179. $changes++;
  180. }
  181. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  182. $tableDifferences->addedIndexes[$index2Name] = $index2Definition;
  183. $changes++;
  184. }
  185. $fromFkeys = $table1->getForeignKeys();
  186. $toFkeys = $table2->getForeignKeys();
  187. foreach ($fromFkeys AS $key1 => $constraint1) {
  188. foreach ($toFkeys AS $key2 => $constraint2) {
  189. if($this->diffForeignKey($constraint1, $constraint2) === false) {
  190. unset($fromFkeys[$key1]);
  191. unset($toFkeys[$key2]);
  192. } else {
  193. if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) {
  194. $tableDifferences->changedForeignKeys[] = $constraint2;
  195. $changes++;
  196. unset($fromFkeys[$key1]);
  197. unset($toFkeys[$key2]);
  198. }
  199. }
  200. }
  201. }
  202. foreach ($fromFkeys AS $key1 => $constraint1) {
  203. $tableDifferences->removedForeignKeys[] = $constraint1;
  204. $changes++;
  205. }
  206. foreach ($toFkeys AS $key2 => $constraint2) {
  207. $tableDifferences->addedForeignKeys[] = $constraint2;
  208. $changes++;
  209. }
  210. return $changes ? $tableDifferences : false;
  211. }
  212. /**
  213. * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
  214. * however ambiguouties between different possibilites should not lead to renaming at all.
  215. *
  216. * @param TableDiff $tableDifferences
  217. */
  218. private function detectColumnRenamings(TableDiff $tableDifferences)
  219. {
  220. $renameCandidates = array();
  221. foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) {
  222. foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) {
  223. if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) {
  224. $renameCandidates[$addedColumn->getName()][] = array($removedColumn, $addedColumn, $addedColumnName);
  225. }
  226. }
  227. }
  228. foreach ($renameCandidates AS $candidate => $candidateColumns) {
  229. if (count($candidateColumns) == 1) {
  230. list($removedColumn, $addedColumn) = $candidateColumns[0];
  231. $removedColumnName = strtolower($removedColumn->getName());
  232. $addedColumnName = strtolower($addedColumn->getName());
  233. $tableDifferences->renamedColumns[$removedColumnName] = $addedColumn;
  234. unset($tableDifferences->addedColumns[$addedColumnName]);
  235. unset($tableDifferences->removedColumns[$removedColumnName]);
  236. }
  237. }
  238. }
  239. /**
  240. * @param ForeignKeyConstraint $key1
  241. * @param ForeignKeyConstraint $key2
  242. * @return bool
  243. */
  244. public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
  245. {
  246. if (array_map('strtolower', $key1->getLocalColumns()) != array_map('strtolower', $key2->getLocalColumns())) {
  247. return true;
  248. }
  249. if (array_map('strtolower', $key1->getForeignColumns()) != array_map('strtolower', $key2->getForeignColumns())) {
  250. return true;
  251. }
  252. if ($key1->onUpdate() != $key2->onUpdate()) {
  253. return true;
  254. }
  255. if ($key1->onDelete() != $key2->onDelete()) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Returns the difference between the fields $field1 and $field2.
  262. *
  263. * If there are differences this method returns $field2, otherwise the
  264. * boolean false.
  265. *
  266. * @param Column $column1
  267. * @param Column $column2
  268. *
  269. * @return array
  270. */
  271. public function diffColumn(Column $column1, Column $column2)
  272. {
  273. $changedProperties = array();
  274. if ( $column1->getType() != $column2->getType() ) {
  275. $changedProperties[] = 'type';
  276. }
  277. if ($column1->getNotnull() != $column2->getNotnull()) {
  278. $changedProperties[] = 'notnull';
  279. }
  280. if ($column1->getDefault() != $column2->getDefault()) {
  281. $changedProperties[] = 'default';
  282. }
  283. if ($column1->getUnsigned() != $column2->getUnsigned()) {
  284. $changedProperties[] = 'unsigned';
  285. }
  286. if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
  287. // check if value of length is set at all, default value assumed otherwise.
  288. $length1 = $column1->getLength() ?: 255;
  289. $length2 = $column2->getLength() ?: 255;
  290. if ($length1 != $length2) {
  291. $changedProperties[] = 'length';
  292. }
  293. if ($column1->getFixed() != $column2->getFixed()) {
  294. $changedProperties[] = 'fixed';
  295. }
  296. }
  297. if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
  298. if (($column1->getPrecision()?:10) != ($column2->getPrecision()?:10)) {
  299. $changedProperties[] = 'precision';
  300. }
  301. if ($column1->getScale() != $column2->getScale()) {
  302. $changedProperties[] = 'scale';
  303. }
  304. }
  305. if ($column1->getAutoincrement() != $column2->getAutoincrement()) {
  306. $changedProperties[] = 'autoincrement';
  307. }
  308. // only allow to delete comment if its set to '' not to null.
  309. if ($column1->getComment() !== null && $column1->getComment() != $column2->getComment()) {
  310. $changedProperties[] = 'comment';
  311. }
  312. return $changedProperties;
  313. }
  314. /**
  315. * Finds the difference between the indexes $index1 and $index2.
  316. *
  317. * Compares $index1 with $index2 and returns $index2 if there are any
  318. * differences or false in case there are no differences.
  319. *
  320. * @param Index $index1
  321. * @param Index $index2
  322. * @return bool
  323. */
  324. public function diffIndex(Index $index1, Index $index2)
  325. {
  326. if ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)) {
  327. return false;
  328. }
  329. return true;
  330. }
  331. }