/application/Espo/Core/Utils/Database/DBAL/Schema/Comparator.php

https://gitlab.com/johanlindberg/irvato-crm · PHP · 119 lines · 61 code · 22 blank · 36 comment · 25 complexity · 92ae8730ea1ea9033332f5c3abd6e8c8 MD5 · raw file

  1. <?php
  2. /************************************************************************
  3. * This file is part of EspoCRM.
  4. *
  5. * EspoCRM - Open Source CRM application.
  6. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  7. * Website: http://www.espocrm.com
  8. *
  9. * EspoCRM is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * EspoCRM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  21. *
  22. * The interactive user interfaces in modified source and object code versions
  23. * of this program must display Appropriate Legal Notices, as required under
  24. * Section 5 of the GNU General Public License version 3.
  25. *
  26. * In accordance with Section 7(b) of the GNU General Public License version 3,
  27. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  28. ************************************************************************/
  29. namespace Espo\Core\Utils\Database\DBAL\Schema;
  30. use Doctrine\DBAL\Schema\Column;
  31. class Comparator extends \Doctrine\DBAL\Schema\Comparator
  32. {
  33. public function diffColumn(Column $column1, Column $column2)
  34. {
  35. $changedProperties = array();
  36. if ( $column1->getType() != $column2->getType() ) {
  37. //espo: fix problem with executing query for custom types
  38. $column1DbTypeName = method_exists($column1->getType(), 'getDbTypeName') ? $column1->getType()->getDbTypeName() : $column1->getType()->getName();
  39. $column2DbTypeName = method_exists($column2->getType(), 'getDbTypeName') ? $column2->getType()->getDbTypeName() : $column2->getType()->getName();
  40. if (strtolower($column1DbTypeName) != strtolower($column2DbTypeName)) {
  41. $changedProperties[] = 'type';
  42. }
  43. //END: espo
  44. }
  45. if ($column1->getNotnull() != $column2->getNotnull()) {
  46. $changedProperties[] = 'notnull';
  47. }
  48. if ($column1->getDefault() != $column2->getDefault()) {
  49. $changedProperties[] = 'default';
  50. }
  51. if ($column1->getUnsigned() != $column2->getUnsigned()) {
  52. $changedProperties[] = 'unsigned';
  53. }
  54. if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
  55. // check if value of length is set at all, default value assumed otherwise.
  56. $length1 = $column1->getLength() ?: 255;
  57. $length2 = $column2->getLength() ?: 255;
  58. /** Espo: column length can be increased only */
  59. /*if ($length1 != $length2) {
  60. $changedProperties[] = 'length';
  61. }*/
  62. if ($length2 > $length1) {
  63. $changedProperties[] = 'length';
  64. }
  65. /** Espo: end */
  66. if ($column1->getFixed() != $column2->getFixed()) {
  67. $changedProperties[] = 'fixed';
  68. }
  69. }
  70. if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
  71. if (($column1->getPrecision()?:10) != ($column2->getPrecision()?:10)) {
  72. $changedProperties[] = 'precision';
  73. }
  74. if ($column1->getScale() != $column2->getScale()) {
  75. $changedProperties[] = 'scale';
  76. }
  77. }
  78. if ($column1->getAutoincrement() != $column2->getAutoincrement()) {
  79. $changedProperties[] = 'autoincrement';
  80. }
  81. // only allow to delete comment if its set to '' not to null.
  82. if ($column1->getComment() !== null && $column1->getComment() != $column2->getComment()) {
  83. $changedProperties[] = 'comment';
  84. }
  85. $options1 = $column1->getCustomSchemaOptions();
  86. $options2 = $column2->getCustomSchemaOptions();
  87. $commonKeys = array_keys(array_intersect_key($options1, $options2));
  88. foreach ($commonKeys as $key) {
  89. if ($options1[$key] !== $options2[$key]) {
  90. $changedProperties[] = $key;
  91. }
  92. }
  93. $diffKeys = array_keys(array_diff_key($options1, $options2) + array_diff_key($options2, $options1));
  94. $changedProperties = array_merge($changedProperties, $diffKeys);
  95. return $changedProperties;
  96. }
  97. }