‪TYPO3CMS  9.5
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 
18 use Doctrine\DBAL\Platforms\AbstractPlatform;
19 use Doctrine\DBAL\Platforms\MySqlPlatform;
20 use Doctrine\DBAL\Schema\Column;
21 use Doctrine\DBAL\Schema\Table;
22 use Doctrine\DBAL\Types;
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  $renamedColumns = $tableDifferences->renamedColumns;
73  $renamedIndexes = $tableDifferences->renamedIndexes;
74  // Rebuild TableDiff with enhanced TYPO3 TableDiff class
75  $tableDifferences = GeneralUtility::makeInstance(
76  TableDiff::class,
77  $tableDifferences->name,
78  $tableDifferences->addedColumns,
79  $tableDifferences->changedColumns,
80  $tableDifferences->removedColumns,
81  $tableDifferences->addedIndexes,
82  $tableDifferences->changedIndexes,
83  $tableDifferences->removedIndexes,
84  $tableDifferences->fromTable
85  );
86  $tableDifferences->renamedColumns = $renamedColumns;
87  $tableDifferences->renamedIndexes = $renamedIndexes;
88  }
89 
90  // Set the table options to be parsed in the AlterTable event.
91  $tableDifferences->setTableOptions($optionDiff);
92 
93  return $tableDifferences;
94  }
95 
105  public function ‪diffColumn(Column $column1, Column $column2)
106  {
107  $changedProperties = parent::diffColumn($column1, $column2);
108 
109  // Only MySQL has variable length versions of TEXT/BLOB
110  if (!$this->databasePlatform instanceof MySqlPlatform) {
111  return $changedProperties;
112  }
113 
114  $properties1 = $column1->toArray();
115  $properties2 = $column2->toArray();
116 
117  if ($properties1['type'] instanceof Types\BlobType || $properties1['type'] instanceof Types\TextType) {
118  // Doctrine does not provide a length for LONGTEXT/LONGBLOB columns
119  $length1 = $properties1['length'] ?: 2147483647;
120  $length2 = $properties2['length'] ?: 2147483647;
121 
122  if ($length1 !== $length2) {
123  $changedProperties[] = 'length';
124  }
125  }
126 
127  return array_unique($changedProperties);
128  }
129 }
‪TYPO3\CMS\Core\Database\Schema\Comparator\__construct
‪__construct(AbstractPlatform $platform=null)
Definition: Comparator.php:41
‪TYPO3\CMS\Core\Database\Schema\Comparator\$databasePlatform
‪AbstractPlatform $databasePlatform
Definition: Comparator.php:34
‪TYPO3\CMS\Core\Database\Schema\Comparator
Definition: Comparator.php:31
‪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:56
‪TYPO3\CMS\Core\Database\Schema
Definition: Comparator.php:3
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Schema\TableDiff
Definition: TableDiff.php:25
‪TYPO3\CMS\Core\Database\Schema\Comparator\diffColumn
‪array diffColumn(Column $column1, Column $column2)
Definition: Comparator.php:104