‪TYPO3CMS  ‪main
TableDiff.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\Schema\Column;
21 use Doctrine\DBAL\Schema\ForeignKeyConstraint;
22 use Doctrine\DBAL\Schema\Index;
23 use Doctrine\DBAL\Schema\Table;
24 use Doctrine\DBAL\Schema\TableDiff as DoctrineTableDiff;
25 
32 class ‪TableDiff extends DoctrineTableDiff
33 {
37  public ?string ‪$newName = null;
38 
59  public function ‪__construct(
60  public Table $oldTable,
61  public array $addedColumns,
62  public array $modifiedColumns,
63  public array $droppedColumns,
64  public array $renamedColumns,
65  public array $addedIndexes,
66  public array $modifiedIndexes,
67  public array $droppedIndexes,
68  public array $renamedIndexes,
69  public array $addedForeignKeys,
70  public array $modifiedForeignKeys,
71  public array $droppedForeignKeys,
72  public array $tableOptions = [],
73  ) {
74  // NOTE: parent::__construct() not called by intention.
75  }
76 
82  public function getTableOptions(): array
83  {
84  return $this->tableOptions;
85  }
86 
92  public function ‪setTableOptions(array $tableOptions): self
93  {
94  $this->‪tableOptions = $tableOptions;
95  return $this;
96  }
97 
101  public function ‪hasTableOption(string $optionName): bool
102  {
103  return array_key_exists($optionName, $this->‪tableOptions);
104  }
105 
106  public function ‪getTableOption(string $optionName): string
107  {
108  if ($this->‪hasTableOption($optionName)) {
109  return (string)$this->‪tableOptions[$optionName];
110  }
111 
112  return '';
113  }
114 
115  public function ‪getOldTable(): Table
116  {
117  return $this->oldTable;
118  }
119 
121  public function getAddedColumns(): array
122  {
123  return $this->addedColumns;
124  }
125 
127  public function getModifiedColumns(): array
128  {
129  return $this->modifiedColumns;
130  }
131 
133  public function getDroppedColumns(): array
134  {
135  return $this->droppedColumns;
136  }
137 
139  public function getRenamedColumns(): array
140  {
141  return $this->renamedColumns;
142  }
143 
145  public function getAddedIndexes(): array
146  {
147  return $this->addedIndexes;
148  }
149 
151  public function getModifiedIndexes(): array
152  {
153  return $this->modifiedIndexes;
154  }
155 
157  public function getDroppedIndexes(): array
158  {
159  return $this->droppedIndexes;
160  }
161 
163  public function ‪getRenamedIndexes(): array
164  {
165  return $this->renamedIndexes;
166  }
167 
169  public function ‪getAddedForeignKeys(): array
170  {
171  return $this->addedForeignKeys;
172  }
173 
175  public function ‪getModifiedForeignKeys(): array
176  {
177  return $this->modifiedForeignKeys;
178  }
179 
181  public function ‪getDroppedForeignKeys(): array
182  {
183  return $this->droppedForeignKeys;
184  }
185 
186  public function ‪isEmpty(): bool
187  {
188  return count($this->getAddedColumns()) === 0
189  && count($this->getModifiedColumns()) === 0
190  && count($this->getDroppedColumns()) === 0
191  && count($this->getRenamedColumns()) === 0
192  && count($this->getAddedIndexes()) === 0
193  && count($this->getModifiedIndexes()) === 0
194  && count($this->getDroppedIndexes()) === 0
195  && count($this->‪getRenamedIndexes()) === 0
196  && count($this->‪getAddedForeignKeys()) === 0
197  && count($this->‪getModifiedForeignKeys()) === 0
198  && count($this->‪getDroppedForeignKeys()) === 0
199  // doctrine/dbal 4.x removed the newName. TYPO3 needs that to provide a rename to prefix logic before
200  // really dropping tables instead. Therefore, we need to add here an empty check for the reintroduced
201  // property.See for example: ConnectionMigrator->migrateUnprefixedRemovedTablesToRenames
202  && $this->‪getNewName() !== null && $this->‪getNewName() !== ''
203  // @todo doctrine/dbal 3.5 deprecated schema events, thus a new way to provide table option has to
204  // be found and implemented. Recheck this afterwards.
205  && $this->getTableOptions() === [];
206  }
207 
208  public function ‪getNewName(): ?string
209  {
210  return ‪$this->newName;
211  }
212 
213  public static function ‪ensure(DoctrineTableDiff|‪TableDiff $tableDiff): self
214  {
215  $diff = new self(
216  // oldTable
217  $tableDiff->‪getOldTable(),
218  // addedColumns
219  $tableDiff->getAddedColumns(),
220  // modifiedColumns
221  [],
222  // droppedColumns
223  $tableDiff->getDroppedColumns(),
224  // renamedColumns
225  $tableDiff->getRenamedColumns(),
226  // addedIndexes
227  $tableDiff->getAddedIndexes(),
228  // modifiedIndexes
229  [],
230  // droppedIndexes
231  $tableDiff->getDroppedIndexes(),
232  // renamedIndexes
233  $tableDiff->‪getRenamedIndexes(),
234  // addedForeignKeys
235  $tableDiff->‪getAddedForeignKeys(),
236  // modifiedForeignKeys
237  $tableDiff->‪getModifiedForeignKeys(),
238  // droppedForeignKeys
239  $tableDiff->‪getDroppedForeignKeys(),
240  // tableOptions
241  ($tableDiff instanceof ‪TableDiff ? $tableDiff->‪tableOptions : []),
242  );
243 
244  // doctrine/dbal 4+ removed the column name as array index for modified column definitions,
245  // but we rely on it. Restore it !
246  // Ensure to use custom ColumnDiff instance with more data and
247  foreach ($tableDiff->getModifiedColumns() as $modifiedColumn) {
248  $diff->modifiedColumns[$modifiedColumn->getOldColumn()->getName()] = new ‪ColumnDiff(
249  // oldColumn
250  $modifiedColumn->getOldColumn(),
251  // newColumn
252  $modifiedColumn->getNewColumn(),
253  );
254  }
255 
256  // doctrine/dbal 4+ removed the index name as array index for modified index definitions,
257  // but we rely on it. Restore it !.
258  foreach ($tableDiff->getModifiedIndexes() as $modifiedIndex) {
259  $diff->modifiedIndexes[$modifiedIndex->getName()] = $modifiedIndex;
260  }
261 
262  return $diff;
263  }
264 
269  public function ‪unsetAddedIndex(Index $index): void
270  {
271  $this->‪addedIndexes = array_filter(
272  $this->‪addedIndexes,
273  static function (Index $addedIndex) use ($index): bool {
274  return $addedIndex !== $index;
275  },
276  );
277  }
278 
283  public function ‪unsetDroppedIndex(Index $index): void
284  {
285  $this->‪droppedIndexes = array_filter(
286  $this->‪droppedIndexes,
287  static function (Index $droppedIndex) use ($index): bool {
288  return $droppedIndex !== $index;
289  },
290  );
291  }
292 }
‪TYPO3\CMS\Core\Database\Schema\TableDiff\unsetDroppedIndex
‪unsetDroppedIndex(Index $index)
Definition: TableDiff.php:283
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getAddedForeignKeys
‪array< ForeignKeyConstraint > getAddedForeignKeys()
Definition: TableDiff.php:169
‪TYPO3\CMS\Core\Database\Schema\TableDiff\tableOptions
‪array< int|string, function getTableOptions():array { return $this-> tableOptions
Definition: TableDiff.php:84
‪TYPO3\CMS\Core\Database\Schema\TableDiff\hasTableOption
‪hasTableOption(string $optionName)
Definition: TableDiff.php:101
‪TYPO3\CMS\Core\Database\Schema\TableDiff\setTableOptions
‪setTableOptions(array $tableOptions)
Definition: TableDiff.php:92
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getOldTable
‪getOldTable()
Definition: TableDiff.php:115
‪TYPO3\CMS\Core\Database\Schema\TableDiff\addedIndexes
‪array< string, function getAddedIndexes():array { return $this-> addedIndexes
Definition: TableDiff.php:147
‪TYPO3\CMS\Core\Database\Schema\TableDiff\unsetAddedIndex
‪unsetAddedIndex(Index $index)
Definition: TableDiff.php:269
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getRenamedIndexes
‪array< string, Index > getRenamedIndexes()
Definition: TableDiff.php:163
‪TYPO3\CMS\Core\Database\Schema
Definition: ColumnDiff.php:18
‪TYPO3\CMS\Core\Database\Schema\TableDiff\__construct
‪__construct(public Table $oldTable, public array $addedColumns, public array $modifiedColumns, public array $droppedColumns, public array $renamedColumns, public array $addedIndexes, public array $modifiedIndexes, public array $droppedIndexes, public array $renamedIndexes, public array $addedForeignKeys, public array $modifiedForeignKeys, public array $droppedForeignKeys, public array $tableOptions=[],)
Definition: TableDiff.php:59
‪TYPO3\CMS\Core\Database\Schema\TableDiff\ensure
‪static ensure(DoctrineTableDiff|TableDiff $tableDiff)
Definition: TableDiff.php:213
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getTableOption
‪getTableOption(string $optionName)
Definition: TableDiff.php:106
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff
Definition: ColumnDiff.php:29
‪TYPO3\CMS\Core\Database\Schema\TableDiff\droppedIndexes
‪array< string, function getDroppedIndexes():array { return $this-> droppedIndexes
Definition: TableDiff.php:159
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getNewName
‪getNewName()
Definition: TableDiff.php:208
‪TYPO3\CMS\Core\Database\Schema\TableDiff\$newName
‪string $newName
Definition: TableDiff.php:37
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getModifiedForeignKeys
‪array< ForeignKeyConstraint > getModifiedForeignKeys()
Definition: TableDiff.php:175
‪TYPO3\CMS\Core\Database\Schema\TableDiff\isEmpty
‪isEmpty()
Definition: TableDiff.php:186
‪TYPO3\CMS\Core\Database\Schema\TableDiff
Definition: TableDiff.php:33
‪TYPO3\CMS\Core\Database\Schema\TableDiff\getDroppedForeignKeys
‪array< ForeignKeyConstraint > getDroppedForeignKeys()
Definition: TableDiff.php:181