TYPO3 CMS  TYPO3_8-7
Comparator.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
24 
30 class Comparator extends \Doctrine\DBAL\Schema\Comparator
31 {
35  protected $databasePlatform;
36 
42  public function __construct(AbstractPlatform $platform = null)
43  {
44  $this->databasePlatform = $platform;
45  }
46 
57  public function diffTable(Table $fromTable, Table $toTable)
58  {
59  $newTableOptions = array_merge($fromTable->getOptions(), $toTable->getOptions());
60  $optionDiff = array_diff_assoc($newTableOptions, $fromTable->getOptions());
61  $tableDifferences = parent::diffTable($fromTable, $toTable);
62 
63  // No changed table options, return parent result
64  if (count($optionDiff) === 0) {
65  return $tableDifferences;
66  }
67 
68  if ($tableDifferences === false) {
69  $tableDifferences = GeneralUtility::makeInstance(TableDiff::class, $fromTable->getName());
70  $tableDifferences->fromTable = $fromTable;
71  } else {
72  // Rebuild TableDiff with enhanced TYPO3 TableDiff class
73  $tableDifferences = GeneralUtility::makeInstance(
74  TableDiff::class,
75  $tableDifferences->name,
76  $tableDifferences->addedColumns,
77  $tableDifferences->changedColumns,
78  $tableDifferences->removedColumns,
79  $tableDifferences->addedIndexes,
80  $tableDifferences->changedIndexes,
81  $tableDifferences->removedIndexes,
82  $tableDifferences->fromTable
83  );
84  }
85 
86  // Set the table options to be parsed in the AlterTable event.
87  $tableDifferences->setTableOptions($optionDiff);
88 
89  return $tableDifferences;
90  }
91 
101  public function diffColumn(Column $column1, Column $column2)
102  {
103  $changedProperties = parent::diffColumn($column1, $column2);
104 
105  // Only MySQL has variable length versions of TEXT/BLOB
106  if (!$this->databasePlatform instanceof MySqlPlatform) {
107  return $changedProperties;
108  }
109 
110  $properties1 = $column1->toArray();
111  $properties2 = $column2->toArray();
112 
113  if ($properties1['type'] instanceof Types\BlobType || $properties1['type'] instanceof Types\TextType) {
114  // Doctrine does not provide a length for LONGTEXT/LONGBLOB columns
115  $length1 = $properties1['length'] ?: 2147483647;
116  $length2 = $properties2['length'] ?: 2147483647;
117 
118  if ($length1 !== $length2) {
119  $changedProperties[] = 'length';
120  }
121  }
122 
123  return array_unique($changedProperties);
124  }
125 }
diffTable(Table $fromTable, Table $toTable)
Definition: Comparator.php:57
static makeInstance($className,... $constructorArguments)
__construct(AbstractPlatform $platform=null)
Definition: Comparator.php:42
diffColumn(Column $column1, Column $column2)
Definition: Comparator.php:101