‪TYPO3CMS  ‪main
ColumnDiff.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\ColumnDiff as DoctrineColumnDiff;
22 
28 class ‪ColumnDiff extends DoctrineColumnDiff
29 {
30  public function ‪__construct(public Column $oldColumn, public Column $newColumn)
31  {
32  // NOTE: parent::__construct() not called by intention.
33  }
34 
35  public function ‪getOldColumn(): Column
36  {
37  return $this->oldColumn;
38  }
39 
40  public function ‪getNewColumn(): Column
41  {
42  return $this->newColumn;
43  }
44 
45  public function ‪hasTypeChanged(): bool
46  {
47  return $this->newColumn->getType()::class !== $this->oldColumn->getType()::class;
48  }
49 
50  public function ‪hasLengthChanged(): bool
51  {
52  return $this->‪hasPropertyChanged(static function (Column $column): ?int {
53  return $column->getLength();
54  });
55  }
56 
57  public function ‪hasPrecisionChanged(): bool
58  {
59  return $this->‪hasPropertyChanged(static function (Column $column): ?int {
60  return $column->getPrecision();
61  });
62  }
63 
64  public function ‪hasScaleChanged(): bool
65  {
66  return $this->‪hasPropertyChanged(static function (Column $column): int {
67  return $column->getScale();
68  });
69  }
70 
71  public function ‪hasUnsignedChanged(): bool
72  {
73  return $this->‪hasPropertyChanged(static function (Column $column): bool {
74  return $column->getUnsigned();
75  });
76  }
77 
78  public function ‪hasFixedChanged(): bool
79  {
80  return $this->‪hasPropertyChanged(static function (Column $column): bool {
81  return $column->getFixed();
82  });
83  }
84 
85  public function ‪hasNotNullChanged(): bool
86  {
87  return $this->‪hasPropertyChanged(static function (Column $column): bool {
88  return $column->getNotnull();
89  });
90  }
91 
92  public function ‪hasDefaultChanged(): bool
93  {
94  $oldDefault = $this->oldColumn->getDefault();
95  $newDefault = $this->newColumn->getDefault();
96  // Null values need to be checked additionally as they tell whether to create or drop a default value.
97  // null != 0, null != false, null != '' etc. This affects platform's table alteration SQL generation.
98  if (($newDefault === null) xor ($oldDefault === null)) {
99  return true;
100  }
101  return $newDefault != $oldDefault;
102  }
103 
104  public function ‪hasAutoIncrementChanged(): bool
105  {
106  return $this->‪hasPropertyChanged(static function (Column $column): bool {
107  return $column->getAutoincrement();
108  });
109  }
110 
111  public function ‪hasCommentChanged(): bool
112  {
113  return $this->‪hasPropertyChanged(static function (Column $column): string {
114  return $column->getComment();
115  });
116  }
117 
118  private function ‪hasPropertyChanged(callable $property): bool
119  {
120  return $property($this->newColumn) !== $property($this->oldColumn);
121  }
122 }
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\getOldColumn
‪getOldColumn()
Definition: ColumnDiff.php:35
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\__construct
‪__construct(public Column $oldColumn, public Column $newColumn)
Definition: ColumnDiff.php:30
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasCommentChanged
‪hasCommentChanged()
Definition: ColumnDiff.php:111
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasPrecisionChanged
‪hasPrecisionChanged()
Definition: ColumnDiff.php:57
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasTypeChanged
‪hasTypeChanged()
Definition: ColumnDiff.php:45
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\getNewColumn
‪getNewColumn()
Definition: ColumnDiff.php:40
‪TYPO3\CMS\Core\Database\Schema
Definition: ColumnDiff.php:18
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasFixedChanged
‪hasFixedChanged()
Definition: ColumnDiff.php:78
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasLengthChanged
‪hasLengthChanged()
Definition: ColumnDiff.php:50
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasUnsignedChanged
‪hasUnsignedChanged()
Definition: ColumnDiff.php:71
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff
Definition: ColumnDiff.php:29
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasDefaultChanged
‪hasDefaultChanged()
Definition: ColumnDiff.php:92
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasAutoIncrementChanged
‪hasAutoIncrementChanged()
Definition: ColumnDiff.php:104
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasPropertyChanged
‪hasPropertyChanged(callable $property)
Definition: ColumnDiff.php:118
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasNotNullChanged
‪hasNotNullChanged()
Definition: ColumnDiff.php:85
‪TYPO3\CMS\Core\Database\Schema\ColumnDiff\hasScaleChanged
‪hasScaleChanged()
Definition: ColumnDiff.php:64