Skip to content

Commit

Permalink
Merge release 2.6.3 into 2.7.x (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Apr 22, 2022
2 parents 614e95d + 527970d commit c00c668
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 deletions.
16 changes: 2 additions & 14 deletions Command/CreateDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$ifNotExists = $input->getOption('if-not-exists');

$driverOptions = [];
$params = $connection->getParams();
$params = $connection->getParams();

if (isset($params['driverOptions'])) {
$driverOptions = $params['driverOptions'];
}

// Since doctrine/dbal 2.11 master has been replaced by primary
if (isset($params['primary'])) {
$params = $params['primary'];
$params['driverOptions'] = $driverOptions;
}

if (isset($params['master'])) {
$params = $params['master'];
$params['driverOptions'] = $driverOptions;
$params = $params['primary'];
}

// Cannot inject `shard` option in parent::getDoctrineConnection
Expand Down
16 changes: 2 additions & 14 deletions Command/DropDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$ifExists = $input->getOption('if-exists');

$driverOptions = [];
$params = $connection->getParams();
$params = $connection->getParams();

if (isset($params['driverOptions'])) {
$driverOptions = $params['driverOptions'];
}

// Since doctrine/dbal 2.11 master has been replaced by primary
if (isset($params['primary'])) {
$params = $params['primary'];
$params['driverOptions'] = $driverOptions;
}

if (isset($params['master'])) {
$params = $params['master'];
$params['driverOptions'] = $driverOptions;
$params = $params['primary'];
}

if (isset($params['shards'])) {
Expand Down
16 changes: 13 additions & 3 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,31 @@ protected function getConnectionOptions(array $connection): array
}

if (! empty($options['slaves']) && ! empty($options['replica']) && ! empty($options['shards'])) {
throw new InvalidArgumentException('Sharding and master-slave connection cannot be used together');
throw new InvalidArgumentException('Sharding and primary-replica connection cannot be used together');
}

foreach (['shards', 'replica', 'slaves'] as $connectionKey) {
foreach ($options[$connectionKey] as $name => $value) {
$driverOptions = $value['driverOptions'] ?? [];
$parentDriverOptions = $options['driverOptions'] ?? [];
if ($driverOptions === [] && $parentDriverOptions === []) {
continue;
}

$options[$connectionKey][$name]['driverOptions'] = $driverOptions + $parentDriverOptions;
}
}

if (! empty($options['slaves']) || ! empty($options['replica'])) {
$nonRewrittenKeys = [
'driver' => true,
'driverOptions' => true,
'driverClass' => true,
'wrapperClass' => true,
'keepSlave' => true,
'keepReplica' => true,
'shardChoser' => true,
'platform' => true,
'slaves' => true,
'master' => true,
'primary' => true,
'replica' => true,
'shards' => true,
Expand Down
10 changes: 9 additions & 1 deletion Repository/ContainerRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
final class ContainerRepositoryFactory implements RepositoryFactory
{
/** @var ObjectRepository[] */
/** @var array<string, ObjectRepository> */
private $managedRepositories = [];

/** @var ContainerInterface */
Expand Down Expand Up @@ -68,6 +68,13 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName
return $this->getOrCreateRepository($entityManager, $metadata);
}

/**
* @param ClassMetadata<TEntity> $metadata
*
* @return ObjectRepository<TEntity>
*
* @template TEntity of object
*/
private function getOrCreateRepository(
EntityManagerInterface $entityManager,
ClassMetadata $metadata
Expand All @@ -79,6 +86,7 @@ private function getOrCreateRepository(

$repositoryClassName = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();

/** @psalm-var ObjectRepository<TEntity> */
return $this->managedRepositories[$repositoryHash] = new $repositoryClassName($entityManager, $metadata);
}
}
2 changes: 1 addition & 1 deletion Repository/ServiceEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* }
* }
*
* @template T
* @template T of object
* @template-extends EntityRepository<T>
*/
class ServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
Expand Down
9 changes: 6 additions & 3 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Generator;
use InvalidArgumentException;
use PDO;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator;
Expand Down Expand Up @@ -184,9 +185,9 @@ public function testDbalDbnameSuffix(): void
$this->assertSame('_test', $config['dbname_suffix']);
}

public function testDbalLoadSingleMasterSlaveConnection(): void
public function testDbalLoadSinglePrimaryReplicaConnection(): void
{
$container = $this->loadContainer('dbal_service_single_master_slave_connection');
$container = $this->loadContainer('dbal_service_single_primary_replica_connection');
$param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);

$this->assertEquals(PrimaryReadReplicaConnection::class, $param['wrapperClass']);
Expand All @@ -199,8 +200,9 @@ public function testDbalLoadSingleMasterSlaveConnection(): void
'dbname' => 'mysql_db',
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'driverOptions' => [PDO::ATTR_STRINGIFY_FETCHES => 1],
],
$param['primary'] ?? $param['master'] // TODO: Remove 'master' support here when we require dbal >= 2.11
$param['primary']
);
$this->assertEquals(
[
Expand All @@ -210,6 +212,7 @@ public function testDbalLoadSingleMasterSlaveConnection(): void
'dbname' => 'replica_db',
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld_replica.sock',
'driverOptions' => [PDO::ATTR_STRINGIFY_FETCHES => 1],
],
$param['replica']['replica1']
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<dbal dbname="mysql_db" user="mysql_user" password="mysql_s3cr3t" unix-socket="/path/to/mysqld.sock" keep-replica="true">
<replica name="replica1" dbname="replica_db" user="replica_user" password="replica_s3cr3t" unix-socket="/path/to/mysqld_replica.sock" />
<default-table-option name="engine">InnoDB</default-table-option>
<option key="17" value="1" />
</dbal>
</config>
</srv:container>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ doctrine:
keep_replica: true
default_table_options:
engine: InnoDB
options:
!php/const:PDO::ATTR_STRINGIFY_FETCHES: 1
replicas:
replica1:
user: replica_user
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"doctrine/annotations": "^1",
"doctrine/cache": "^1.11 || ^2.0",
"doctrine/dbal": "^2.13.1|^3.3.2",
"doctrine/persistence": "^2.2",
"doctrine/persistence": "^2.2|^3",
"doctrine/sql-formatter": "^1.0.1",
"symfony/cache": "^4.3.3|^5.0|^6.0",
"symfony/config": "^4.4.3|^5.0|^6.0",
Expand Down
12 changes: 6 additions & 6 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@
<file name="Registry.php"/>
</projectFiles>
<issueHandlers>
<TooManyTemplateParams>
<errorLevel type="suppress">
<!-- This happens on PHP 7.1 because there is no 7.1 compatible doctrine/orm version having template annotations -->
<file name="Repository/ServiceEntityRepository.php"/>
</errorLevel>
</TooManyTemplateParams>
<InvalidArrayOffset>
<errorLevel type="suppress">
<!-- requires a release of https://github.com/doctrine/dbal/pull/5261 -->
<file name="Tests/ConnectionFactoryTest.php"/>
</errorLevel>
</InvalidArrayOffset>
<UndefinedClass>
<errorLevel type="suppress">
<!-- We use the "Foo" namespace in unit tests. We are aware that those classes don't exist. -->
<referencedClass name="Foo\*"/>
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<!-- https://github.com/symfony/symfony/issues/45609 -->
Expand Down

0 comments on commit c00c668

Please sign in to comment.