TYPO3 CMS  TYPO3_8-7
FinalDatabaseSchemaUpdate.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
27 {
31  public function __construct()
32  {
33  parent::__construct();
34  $this->title = 'Update database schema: Modify tables and fields';
35  }
36 
51  public function checkForUpdate(&$description): bool
52  {
53  $contextService = GeneralUtility::makeInstance(ContextService::class);
54  $description = 'There are tables or fields in the database which need to be changed.<br /><br />' .
55  'This update wizard can be run only when there are no other update wizards left to make sure they have ' .
56  'all needed fields unchanged.<br /><br />If you want to apply changes selectively, ' .
57  '<a href="' . GeneralUtility::getIndpEnv('TYPO3_REQUEST_SCRIPT') . '?install[action]=importantActions&amp;install[context]=' .
58  $contextService->getContextString() .
59  '&amp;install[controller]=tool">go to Database Analyzer</a>.';
60 
61  $databaseDifferences = $this->getDatabaseDifferences();
62  foreach ($databaseDifferences as $schemaDiff) {
63  // A change for a table is required
64  if (count($schemaDiff->changedTables) !== 0) {
65  foreach ($schemaDiff->changedTables as $changedTable) {
66  if (!empty($changedTable->addedColumns) || !empty($changedTable->changedColumns)) {
67  return true;
68  }
69  }
70  }
71  }
72 
73  return false;
74  }
75 
90  public function getUserInput($inputPrefix)
91  {
92  $result = '';
93  $changedFieldItems = '';
94  $changedIndexItems = '';
95 
96  $databaseDifferences = $this->getDatabaseDifferences();
97  foreach ($databaseDifferences as $schemaDiff) {
98  $changedFieldItems .= $this->getChangedFieldInformation($schemaDiff);
99  $changedIndexItems .= $this->getChangedIndexInformation($schemaDiff);
100  }
101 
102  $result .= $this->renderList('Change the following fields in tables:', $changedFieldItems);
103  $result .= $this->renderList('Change the following keys in tables:', $changedIndexItems);
104 
105  return $result;
106  }
107 
115  public function performUpdate(array &$dbQueries, &$customMessage)
116  {
117  $statements = $this->getDatabaseDefinition();
118  $result = $this->schemaMigrationService->install($statements, false);
119 
120  // Extract all statements stored in the keys, independent of error status
121  $dbQueries = array_merge($dbQueries, array_keys($result));
122 
123  // Only keep error messages
124  $result = array_filter($result);
125 
126  $customMessage = implode(
127  LF,
128  array_map(
129  function (string $message) {
130  return 'SQL-ERROR: ' . htmlspecialchars($message);
131  },
132  $result
133  )
134  );
135 
136  return count($result) === 0;
137  }
138 
145  protected function getChangedFieldInformation(SchemaDiff $schemaDiff): string
146  {
147  $items = [];
148 
149  foreach ($schemaDiff->changedTables as $changedTable) {
150  $columns = array_map(
151  function (ColumnDiff $columnDiff) use ($changedTable) {
152  return $this->renderFieldListItem($changedTable->name, $columnDiff->column->getName());
153  },
154  $changedTable->changedColumns
155  );
156 
157  $items[] = implode(LF, $columns);
158  }
159 
160  return trim(implode(LF, $items));
161  }
162 
169  protected function getChangedIndexInformation(SchemaDiff $schemaDiff): string
170  {
171  $items = [];
172 
173  foreach ($schemaDiff->changedTables as $changedTable) {
174  $indexes = array_map(
175  function (Index $index) use ($changedTable) {
176  return $this->renderFieldListItem($changedTable->name, $index->getName());
177  },
178  $changedTable->changedIndexes
179  );
180 
181  $items[] = implode(LF, $indexes);
182  }
183 
184  return trim(implode(LF, $items));
185  }
186 }
static makeInstance($className,... $constructorArguments)