Skip to content

Commit

Permalink
[BUGFIX] Add createSchemaManager to DBAL Connection
Browse files Browse the repository at this point in the history
This change adds the new createSchemaManager() to
our Doctrine DBAL Connection subclass to make it
forward-compatible with Doctrine DBAL 3.x.

createSchemaManager() is introduced with DBAL 3.x and
is replacing getSchemaManager() which will be removed
in Doctrine DBAL 4.

Since this is a very lowlevel change, the method
can be added without most notice for extension authors.

Resolves: #96286
Releases: main, 11.5
Change-Id: Ifdccc2b0dff443bfe38d23a1bd1333b5c7c5db40
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72555
Tested-by: core-ci <typo3@b13.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
  • Loading branch information
bmack committed Dec 8, 2021
1 parent 88a4a85 commit 2cb7a08
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 23 deletions.
4 changes: 2 additions & 2 deletions typo3/sysext/core/Classes/DataHandling/DataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7263,7 +7263,7 @@ public function updateDB($table, $id, $fieldArray)
$platform = $connection->getDatabasePlatform();
if ($platform instanceof SQLServerPlatform) {
// mssql needs to set proper PARAM_LOB and others to update fields
$tableDetails = $connection->getSchemaManager()->listTableDetails($table);
$tableDetails = $connection->createSchemaManager()->listTableDetails($table);
foreach ($fieldArray as $columnName => $columnValue) {
$types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
}
Expand Down Expand Up @@ -8012,7 +8012,7 @@ public function compareFieldArrayWithCurrentAndUnset($table, $id, $fieldArray)
->fetchAssociative();
// If the current record exists (which it should...), begin comparison:
if (is_array($currentRecord)) {
$tableDetails = $connection->getSchemaManager()->listTableDetails($table);
$tableDetails = $connection->createSchemaManager()->listTableDetails($table);
$columnRecordTypes = [];
foreach ($currentRecord as $columnName => $_) {
$columnRecordTypes[$columnName] = '';
Expand Down
12 changes: 12 additions & 0 deletions typo3/sysext/core/Classes/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\DBAL\Platforms\PostgreSQL94Platform as PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
Expand Down Expand Up @@ -413,6 +414,17 @@ public function getServerVersion(): string
return $version;
}

/**
* Creates a SchemaManager that can be used to inspect or change the
* database schema through the connection.
*
* This is a copy from Doctrine DBAL 3.x, and can be removed once Doctrine DBAL 3.2 is included
*/
public function createSchemaManager(): AbstractSchemaManager
{
return $this->_driver->getSchemaManager($this);
}

/**
* Execute commands after initializing a new connection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static function ($columnName) {
protected function buildSchemaDiff(bool $renameUnused = true): SchemaDiff
{
// Build the schema definitions
$fromSchema = $this->connection->getSchemaManager()->createSchema();
$fromSchema = $this->connection->createSchemaManager()->createSchema();
$toSchema = $this->buildExpectedSchemaDefinitions($this->connectionName);

// Add current table options to the fromSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function update($fileUid, array $data)
$types = [];
if ($connection->getDatabasePlatform() instanceof SQLServerPlatform) {
// mssql needs to set proper PARAM_LOB and others to update fields
$tableDetails = $connection->getSchemaManager()->listTableDetails($this->tableName);
$tableDetails = $connection->createSchemaManager()->listTableDetails($this->tableName);
foreach ($updateRow as $columnName => $columnValue) {
$types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
}
Expand Down Expand Up @@ -230,7 +230,7 @@ protected function getTableFields(): array
if (empty($this->tableFields)) {
$this->tableFields = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->tableName)
->getSchemaManager()
->createSchemaManager()
->listTableColumns($this->tableName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ protected function cleanUnavailableColumns(array $data)
if (empty($this->tableColumns[$this->table])) {
$this->tableColumns[$this->table] = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->table)
->getSchemaManager()
->createSchemaManager()
->listTableColumns($this->table);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function setUp(): void
$this->subject = GeneralUtility::makeInstance(SchemaMigrator::class);
$this->sqlReader = GeneralUtility::makeInstance(SqlReader::class);
$this->connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$this->schemaManager = $this->connectionPool->getConnectionForTable($this->tableName)->getSchemaManager();
$this->schemaManager = $this->connectionPool->getConnectionForTable($this->tableName)->createSchemaManager();
$this->prepareTestTable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function addRow(string $tableName, array $fieldValues, bool $isRelation =
$platform = $connection->getDatabasePlatform();
if ($platform instanceof SQLServerPlatform) {
// mssql needs to set proper PARAM_LOB and others to update fields
$tableDetails = $connection->getSchemaManager()->listTableDetails($tableName);
$tableDetails = $connection->createSchemaManager()->listTableDetails($tableName);
foreach ($fieldValues as $columnName => $columnValue) {
$types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public function updateRow(string $tableName, array $fieldValues, bool $isRelatio
$platform = $connection->getDatabasePlatform();
if ($platform instanceof SQLServerPlatform) {
// mssql needs to set proper PARAM_LOB and others to update fields
$tableDetails = $connection->getSchemaManager()->listTableDetails($tableName);
$tableDetails = $connection->createSchemaManager()->listTableDetails($tableName);
foreach ($fieldValues as $columnName => $columnValue) {
$columnName = (string)$columnName;
$types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/extbase/Classes/Service/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function clearPageCacheForGivenRecord(string $tableName, int $uid): vo
if (!isset($this->hasPidColumn[$tableName])) {
$columns = $this->connectionPool
->getConnectionForTable($tableName)
->getSchemaManager()
->createSchemaManager()
->listTableColumns($tableName);
$this->hasPidColumn[$tableName] = array_key_exists('pid', $columns);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ protected function getDatabaseConnectionInformation(): array
'host' => $connectionParameters['host'] ?? '',
'port' => $connectionParameters['port'] ?? '',
'socket' => $connectionParameters['unix_socket'] ?? '',
'numberOfTables' => count($connection->getSchemaManager()->listTableNames()),
'numberOfTables' => count($connection->createSchemaManager()->listTableNames()),
'numberOfMappedTables' => 0,
];
if (isset($GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public function checkDatabaseRequirementsAction(ServerRequestInterface $request)
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$connection
->getSchemaManager()
->createSchemaManager()
->dropDatabase($connection->quoteIdentifier($databaseName));
}

Expand Down Expand Up @@ -876,7 +876,7 @@ public function checkDatabaseDataAction(): ResponseInterface
{
$existingTables = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME)
->getSchemaManager()
->createSchemaManager()
->listTableNames();
return new JsonResponse([
'success' => !empty($existingTables),
Expand Down Expand Up @@ -1274,6 +1274,7 @@ protected function getDatabaseList()
// an invalid database name would lead to exceptions which would prevent
// changing the currently configured database.
$connection = DriverManager::getConnection($connectionParams);
// @todo: migrate to createSchemaManager() with Doctrine DBAL 3.2 requirement in TYPO3 v12.0
$databaseArray = $connection->getSchemaManager()->listDatabases();
$connection->close();

Expand All @@ -1292,6 +1293,7 @@ protected function getDatabaseList()
$connectionParams['dbname'] = $databaseName;
$connection = DriverManager::getConnection($connectionParams);

// @todo: migrate to createSchemaManager() with Doctrine DBAL 3.2 requirement in TYPO3 v12.0
$databases[] = [
'name' => $databaseName,
'tables' => count($connection->getSchemaManager()->listTableNames()),
Expand Down Expand Up @@ -1369,7 +1371,7 @@ protected function checkExistingDatabase($dbName)
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);

if (!empty($connection->getSchemaManager()->listTableNames())) {
if (!empty($connection->createSchemaManager()->listTableNames())) {
$result = new FlashMessage(
sprintf('Cannot use database "%s"', $dbName)
. ', because it already contains tables. Please select a different database or choose to create one!',
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/install/Classes/Database/PermissionsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ private function getConnection(): Connection

private function getSchemaManager(): AbstractSchemaManager
{
return $this->getConnection()->getSchemaManager();
return $this->getConnection()->createSchemaManager();
}
}
2 changes: 1 addition & 1 deletion typo3/sysext/install/Classes/Service/ClearTableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getTableStatistics(): array
$tableStatistics = [];
foreach ($this->tableList as $table) {
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table['name']);
if ($connection->getSchemaManager()->tablesExist([$table['name']])) {
if ($connection->createSchemaManager()->tablesExist([$table['name']])) {
$table['rowCount'] = $connection->count(
'*',
$table['name'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function updateNecessary(): bool
// Check if database table exist
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionByName('Default');
$tableNames = $connection->getSchemaManager()->listTableNames();
$tableNames = $connection->createSchemaManager()->listTableNames();
if (in_array('sys_collection', $tableNames, true)) {
// table is available, now check if there are entries in it
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function executeUpdate(): bool

protected function columnsExistInTable(): bool
{
$schemaManager = $this->getConnectionPool()->getConnectionForTable(self::TABLE_NAME)->getSchemaManager();
$schemaManager = $this->getConnectionPool()->getConnectionForTable(self::TABLE_NAME)->createSchemaManager();

if ($schemaManager === null) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/install/Classes/Updates/SysLogChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function executeUpdate(): bool
}

// Ensure the level field is a varchar, otherwise we are in trouble when logging into TYPO3 Backend.
$schema = $this->sysLogTable->getSchemaManager();
$schema = $this->sysLogTable->createSchemaManager();
$table = $schema->listTableDetails('sys_log');
if (!$table->getColumn('level')->getType() instanceof StringType) {
$schema->alterTable(new TableDiff(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function shortcutRecordsUpdated(): void

$schemaManager = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable(self::TABLE_NAME)
->getSchemaManager();
->createSchemaManager();

$schemaManager->alterTable(
new TableDiff(
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/lowlevel/Classes/Database/QueryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public function search()
continue;
}
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
$tableColumns = $connection->getSchemaManager()->listTableColumns($table);
$tableColumns = $connection->createSchemaManager()->listTableColumns($table);
$fieldsInDatabase = [];
foreach ($tableColumns as $column) {
$fieldsInDatabase[] = $column->getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public function selectNonEmptyRecordsWithFkeys(): void
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
foreach ($fkey_arrays as $table => $fields) {
$connection = $connectionPool->getConnectionForTable($table);
$schemaManager = $connection->getSchemaManager();
$schemaManager = $connection->createSchemaManager();
$tableColumns = $schemaManager->listTableColumns($table);

$queryBuilder = $connectionPool->getQueryBuilderForTable($table);
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/workspaces/Classes/Hook/DataHandlerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ protected function version_swap($table, $id, $swapWith, DataHandler $dataHandler
$tableDetails = null;
if ($platform instanceof SQLServerPlatform) {
// mssql needs to set proper PARAM_LOB and others to update fields
$tableDetails = $connection->getSchemaManager()->listTableDetails($table);
$tableDetails = $connection->createSchemaManager()->listTableDetails($table);
}

try {
Expand Down

0 comments on commit 2cb7a08

Please sign in to comment.