Skip to content

Commit

Permalink
Add DBAL 2.11 primary/replica compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pwbdod committed Sep 25, 2020
1 parent 474dae6 commit 2dcaf44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Command/CreateDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$params = $params['master'];
}

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

// Cannot inject `shard` option in parent::getDoctrineConnection
// cause it will try to connect to a non-existing database
if (isset($params['shards'])) {
Expand Down
5 changes: 5 additions & 0 deletions Command/DropDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$params = $params['master'];
}

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

if (isset($params['shards'])) {
$shards = $params['shards'];
// Default select global
Expand Down
34 changes: 24 additions & 10 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder

protected function getConnectionOptions($connection)
{
$usePrimaryReadReplicaConnection = class_exists('Doctrine\DBAL\Connections\PrimaryReadReplicaConnection');

$options = $connection;

if (isset($options['platform_service'])) {
Expand All @@ -236,7 +238,7 @@ protected function getConnectionOptions($connection)
unset($options['shard_choser_service']);
}

foreach ([
$replacementOptions = [
'options' => 'driverOptions',
'driver_class' => 'driverClass',
'wrapper_class' => 'wrapperClass',
Expand All @@ -245,7 +247,19 @@ protected function getConnectionOptions($connection)
'shard_manager_class' => 'shardManagerClass',
'server_version' => 'serverVersion',
'default_table_options' => 'defaultTableOptions',
] as $old => $new) {
];

$masterOrPrimary = 'master';
$slavesOrReplica = 'slaves';
if ($usePrimaryReadReplicaConnection) {
$replacementOptions['master'] = 'primary';
$replacementOptions['slaves'] = 'replica';

$masterOrPrimary = 'primary';
$slavesOrReplica = 'replica';
}

foreach ($replacementOptions as $old => $new) {
if (! isset($options[$old])) {
continue;
}
Expand All @@ -254,11 +268,11 @@ protected function getConnectionOptions($connection)
unset($options[$old]);
}

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

if (! empty($options['slaves'])) {
if (! empty($options[$slavesOrReplica])) {
$nonRewrittenKeys = [
'driver' => true,
'driverOptions' => true,
Expand All @@ -267,8 +281,8 @@ protected function getConnectionOptions($connection)
'keepSlave' => true,
'shardChoser' => true,
'platform' => true,
'slaves' => true,
'master' => true,
$slavesOrReplica => true,
$masterOrPrimary => true,
'shards' => true,
'serverVersion' => true,
'defaultTableOptions' => true,
Expand All @@ -282,15 +296,15 @@ protected function getConnectionOptions($connection)
if (isset($nonRewrittenKeys[$key])) {
continue;
}
$options['master'][$key] = $value;
$options[$masterOrPrimary][$key] = $value;
unset($options[$key]);
}
if (empty($options['wrapperClass'])) {
// Change the wrapper class only if the user does not already forced using a custom one.
$options['wrapperClass'] = 'Doctrine\\DBAL\\Connections\\MasterSlaveConnection';
$options['wrapperClass'] = $usePrimaryReadReplicaConnection ? 'Doctrine\\DBAL\\Connections\\PrimaryReadReplicaConnection' : 'Doctrine\\DBAL\\Connections\\MasterSlaveConnection';
}
} else {
unset($options['slaves']);
unset($options[$slavesOrReplica]);
}

if (! empty($options['shards'])) {
Expand All @@ -302,7 +316,7 @@ protected function getConnectionOptions($connection)
'keepSlave' => true,
'shardChoser' => true,
'platform' => true,
'slaves' => true,
$slavesOrReplica => true,
'global' => true,
'shards' => true,
'serverVersion' => true,
Expand Down

0 comments on commit 2dcaf44

Please sign in to comment.