‪TYPO3CMS  ‪main
DatabaseUpgradeWizardsService.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\MySQLPlatform;
21 use Doctrine\DBAL\Schema\Column;
22 use Doctrine\DBAL\Schema\Table;
27 
32 class DatabaseUpgradeWizardsService
33 {
34  public function __construct(
35  private readonly SchemaMigrator $schemaMigrator,
36  ) {}
37 
47  public function getBlockingDatabaseAdds(): array
48  {
49  $sqlReader = GeneralUtility::makeInstance(SqlReader::class);
50  $databaseDefinitions = $sqlReader->getCreateTableStatementArray($sqlReader->getTablesDefinitionString());
51  $databaseDifferences = $this->schemaMigrator->getSchemaDiffs($databaseDefinitions);
52  $adds = [];
53  foreach ($databaseDifferences as $schemaDiff) {
54  foreach ($schemaDiff->getCreatedTables() as $newTable) {
56  if (!is_array($adds['tables'] ?? false)) {
57  $adds['tables'] = [];
58  }
59  $adds['tables'][] = [
60  'table' => $newTable->getName(),
61  ];
62  }
63  foreach ($schemaDiff->getAlteredTables() as $changedTable) {
64  foreach ($changedTable->getAddedColumns() as $addedColumn) {
66  if (!is_array($adds['columns'] ?? false)) {
67  $adds['columns'] = [];
68  }
69  $adds['columns'][] = [
70  'table' => $changedTable->getOldTable()->getName(),
71  'field' => $addedColumn->getName(),
72  ];
73  }
74  foreach ($changedTable->getAddedIndexes() as $addedIndex) {
76  if (!is_array($adds['indexes'] ?? false)) {
77  $adds['indexes'] = [];
78  }
79  $adds['indexes'][] = [
80  'table' => $changedTable->getOldTable()->getName(),
81  'index' => $addedIndex->getName(),
82  ];
83  }
84  }
85  }
86 
87  return $adds;
88  }
89 
95  public function addMissingTablesAndFields(): array
96  {
97  $sqlReader = GeneralUtility::makeInstance(SqlReader::class);
98  $databaseDefinitions = $sqlReader->getCreateTableStatementArray($sqlReader->getTablesDefinitionString());
99  return $this->schemaMigrator->install($databaseDefinitions, true);
100  }
101 
107  public function isDatabaseCharsetUtf8(): bool
108  {
109  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
110  ->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME);
111 
112  $isDefaultConnectionMysql = ($connection->getDatabasePlatform() instanceof MySQLPlatform);
113 
114  if (!$isDefaultConnectionMysql) {
115  // Not tested on non mysql
116  $charsetOk = true;
117  } else {
118  $queryBuilder = $connection->createQueryBuilder();
119  $charset = (string)$queryBuilder->select('DEFAULT_CHARACTER_SET_NAME')
120  ->from('information_schema.SCHEMATA')
121  ->where(
122  $queryBuilder->expr()->eq(
123  'SCHEMA_NAME',
124  $queryBuilder->createNamedParameter($connection->getDatabase())
125  )
126  )
127  ->setMaxResults(1)
128  ->executeQuery()
129  ->fetchOne();
130  // check if database charset is utf-8, also allows utf8mb3 and utf8mb4
131  $charsetOk = str_starts_with($charset, 'utf8');
132  }
133  return $charsetOk;
134  }
135 
140  public function setDatabaseCharsetUtf8()
141  {
142  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
143  ->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME);
144  $sql = 'ALTER DATABASE ' . $connection->quoteIdentifier($connection->getDatabase()) . ' CHARACTER SET utf8';
145  $connection->executeStatement($sql);
146  }
147 }
‪TYPO3\CMS\Core\Database\Schema\SqlReader
Definition: SqlReader.php:31
‪TYPO3\CMS\Core\Database\ConnectionPool\DEFAULT_CONNECTION_NAME
‪const DEFAULT_CONNECTION_NAME
Definition: ConnectionPool.php:50
‪TYPO3\CMS\Core\Database\Schema\SchemaMigrator
Definition: SchemaMigrator.php:36
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16