‪TYPO3CMS  10.4
Comparator.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Doctrine\DBAL\Platforms\AbstractPlatform;
21 use Doctrine\DBAL\Platforms\MySqlPlatform;
22 use Doctrine\DBAL\Schema\Column;
23 use Doctrine\DBAL\Schema\Table;
24 use Doctrine\DBAL\Types\BlobType;
25 use Doctrine\DBAL\Types\TextType;
27 
33 class ‪Comparator extends \Doctrine\DBAL\Schema\Comparator
34 {
38  protected ‪$databasePlatform;
39 
45  public function ‪__construct(AbstractPlatform $platform = null)
46  {
47  $this->databasePlatform = $platform;
48  }
49 
60  public function ‪diffTable(Table $fromTable, Table $toTable)
61  {
62  $newTableOptions = array_merge($fromTable->getOptions(), $toTable->getOptions());
63  $optionDiff = array_diff_assoc($newTableOptions, $fromTable->getOptions());
64  $tableDifferences = parent::diffTable($fromTable, $toTable);
65 
66  // No changed table options, return parent result
67  if (count($optionDiff) === 0) {
68  return $tableDifferences;
69  }
70 
71  if ($tableDifferences === false) {
72  $tableDifferences = GeneralUtility::makeInstance(TableDiff::class, $fromTable->getName());
73  $tableDifferences->fromTable = $fromTable;
74  } else {
75  $renamedColumns = $tableDifferences->renamedColumns;
76  $renamedIndexes = $tableDifferences->renamedIndexes;
77  // Rebuild TableDiff with enhanced TYPO3 TableDiff class
78  $tableDifferences = GeneralUtility::makeInstance(
79  TableDiff::class,
80  $tableDifferences->name,
81  $tableDifferences->addedColumns,
82  $tableDifferences->changedColumns,
83  $tableDifferences->removedColumns,
84  $tableDifferences->addedIndexes,
85  $tableDifferences->changedIndexes,
86  $tableDifferences->removedIndexes,
87  $tableDifferences->fromTable
88  );
89  $tableDifferences->renamedColumns = $renamedColumns;
90  $tableDifferences->renamedIndexes = $renamedIndexes;
91  }
92 
93  // Set the table options to be parsed in the AlterTable event.
94  $tableDifferences->setTableOptions($optionDiff);
95 
96  return $tableDifferences;
97  }
98 
108  public function ‪diffColumn(Column $column1, Column $column2)
109  {
110  $changedProperties = parent::diffColumn($column1, $column2);
111 
112  // Only MySQL has variable length versions of TEXT/BLOB
113  if (!$this->databasePlatform instanceof MySqlPlatform) {
114  return $changedProperties;
115  }
116 
117  $properties1 = $column1->toArray();
118  $properties2 = $column2->toArray();
119 
120  if ($properties1['type'] instanceof BlobType || $properties1['type'] instanceof TextType) {
121  // Doctrine does not provide a length for LONGTEXT/LONGBLOB columns
122  $length1 = $properties1['length'] ?: 2147483647;
123  $length2 = $properties2['length'] ?: 2147483647;
124 
125  if ($length1 !== $length2) {
126  $changedProperties[] = 'length';
127  }
128  }
129 
130  return array_unique($changedProperties);
131  }
132 }
‪TYPO3\CMS\Core\Database\Schema\Comparator\__construct
‪__construct(AbstractPlatform $platform=null)
Definition: Comparator.php:44
‪TYPO3\CMS\Core\Database\Schema\Comparator\$databasePlatform
‪AbstractPlatform $databasePlatform
Definition: Comparator.php:37
‪TYPO3\CMS\Core\Database\Schema\Comparator
Definition: Comparator.php:34
‪TYPO3\CMS\Core\Database\Schema\Comparator\diffTable
‪bool Doctrine DBAL Schema TableDiff TYPO3 CMS Core Database Schema TableDiff diffTable(Table $fromTable, Table $toTable)
Definition: Comparator.php:59
‪TYPO3\CMS\Core\Database\Schema
Definition: Comparator.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Database\Schema\TableDiff
Definition: TableDiff.php:27
‪TYPO3\CMS\Core\Database\Schema\Comparator\diffColumn
‪array diffColumn(Column $column1, Column $column2)
Definition: Comparator.php:107