Skip to content

Commit

Permalink
[TASK] Pass connection to ConnectionMigrator and factory method
Browse files Browse the repository at this point in the history
Only the connection name has been passed to the `ConnectionMigrator`
and factory method `ConnectionMigrator::create()` and the connection
retrieved using `GeneralUtility::makeInstance(ConnectionPool::class)`.

This change modifies the constructor and factory method to hand over
the connection along with current arguments and adjusts usages and
tests.

Resolves: #102867
Related: #102866
Releases: main
Change-Id: I410b051ba54fef892a5bc7978fe9a8b2460e9c5f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82515
Reviewed-by: Garvin Hicking <gh@faktor-e.de>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Garvin Hicking <gh@faktor-e.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: core-ci <typo3@b13.com>
  • Loading branch information
sbuerk committed Jan 20, 2024
1 parent 0eb7cac commit 20fa9dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 41 deletions.
36 changes: 11 additions & 25 deletions typo3/sysext/core/Classes/Database/Schema/ConnectionMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,28 @@ class ConnectionMigrator
*/
protected $deletedPrefix = 'zzz_deleted_';

/**
* @var Typo3Connection
*/
protected $connection;

/**
* @var string
*/
protected $connectionName;

/**
* @var Table[]
*/
protected $tables;

/**
* @param Table[] $tables
*/
public function __construct(string $connectionName, array $tables)
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$this->connection = $connectionPool->getConnectionByName($connectionName);
$this->connectionName = $connectionName;
$this->tables = $tables;
}
public function __construct(
private readonly string $connectionName,
private readonly Typo3Connection $connection,
private array $tables,
) {}

/**
* @param Table[] $tables
* @param non-empty-string $connectionName
* @param Typo3Connection $connection
* @param Table[] $tables
* @return ConnectionMigrator
*/
public static function create(string $connectionName, array $tables)
public static function create(string $connectionName, Typo3Connection $connection, array $tables)
{
return GeneralUtility::makeInstance(
static::class,
$connectionName,
$tables
$connection,
$tables,
);
}

Expand Down
21 changes: 7 additions & 14 deletions typo3/sysext/core/Classes/Database/Schema/SchemaMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ public function getUpdateSuggestions(array $statements, bool $remove = false): a
$tables = $this->parseCreateTableStatements($statements);
$updateSuggestions = [];
foreach ($this->connectionPool->getConnectionNames() as $connectionName) {
$this->adoptDoctrineAutoincrementDetectionForSqlite($tables, $this->connectionPool->getConnectionByName($connectionName));
$connectionMigrator = ConnectionMigrator::create(
$connectionName,
$tables
);
$connection = $this->connectionPool->getConnectionByName($connectionName);
$this->adoptDoctrineAutoincrementDetectionForSqlite($tables, $connection);
$connectionMigrator = ConnectionMigrator::create($connectionName, $connection, $tables);
$updateSuggestions[$connectionName] = $connectionMigrator->getUpdateSuggestions($remove);
}
return $updateSuggestions;
Expand All @@ -87,10 +85,8 @@ public function getSchemaDiffs(array $statements): array
$tables = $this->parseCreateTableStatements($statements);
$schemaDiffs = [];
foreach ($this->connectionPool->getConnectionNames() as $connectionName) {
$connectionMigrator = ConnectionMigrator::create(
$connectionName,
$tables
);
$connection = $this->connectionPool->getConnectionByName($connectionName);
$connectionMigrator = ConnectionMigrator::create($connectionName, $connection, $tables);
$schemaDiffs[$connectionName] = $connectionMigrator->getSchemaDiff();
}
return $schemaDiffs;
Expand Down Expand Up @@ -157,11 +153,8 @@ public function install(array $statements, bool $createOnly = false): array
$result = [];

foreach ($this->connectionPool->getConnectionNames() as $connectionName) {
$connectionMigrator = ConnectionMigrator::create(
$connectionName,
$tables
);

$connection = $this->connectionPool->getConnectionByName($connectionName);
$connectionMigrator = ConnectionMigrator::create($connectionName, $connection, $tables);
$lastResult = $connectionMigrator->install($createOnly);
$result = array_merge($result, $lastResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ protected function setUp(): void

$this->maxIdentifierLength = PlatformInformation::getMaxIdentifierLength($this->platform);

$this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, [], '', false);
$this->subject->_set('connection', $connectionMock);
$this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, ['Default', $connectionMock, []]);
}

/**
Expand Down

0 comments on commit 20fa9dc

Please sign in to comment.