bug #37358 Directly use the driverConnection executeUpdate method (TristanPouliquen)

This PR was merged into the 4.4 branch.

Discussion
----------

Directly use the driverConnection executeUpdate method

executeUpdate & executeQuery methods do not throw a TableNotFoundException. No need for the try/catch as it is done for executeQuery

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix https://github.com/symfony/symfony/issues/37355
| License       | MIT

As explained in https://github.com/symfony/symfony/issues/37355, when doing a write operation, one should avoid using the `executeQuery` method of a Connection, as Doctrine's MasterSlaveConnection can pick a slave instance (usually read-only) for these operations.

Commits
-------

eec12ecd23 Use the driverConnection executeUpdate method
This commit is contained in:
Fabien Potencier 2020-06-25 10:28:26 +02:00
commit 73596ef4f7
1 changed files with 21 additions and 2 deletions

View File

@ -126,7 +126,7 @@ class Connection
'available_at' => '?',
]);
$this->executeQuery($queryBuilder->getSQL(), [
$this->executeUpdate($queryBuilder->getSQL(), [
$body,
json_encode($headers),
$this->configuration['queue_name'],
@ -179,7 +179,7 @@ class Connection
->set('delivered_at', '?')
->where('id = ?');
$now = new \DateTime();
$this->executeQuery($queryBuilder->getSQL(), [
$this->executeUpdate($queryBuilder->getSQL(), [
$now,
$doctrineEnvelope['id'],
], [
@ -329,6 +329,25 @@ class Connection
return $stmt;
}
private function executeUpdate(string $sql, array $parameters = [], array $types = [])
{
try {
$stmt = $this->driverConnection->executeUpdate($sql, $parameters, $types);
} catch (TableNotFoundException $e) {
if ($this->driverConnection->isTransactionActive()) {
throw $e;
}
// create table
if ($this->autoSetup) {
$this->setup();
}
$stmt = $this->driverConnection->executeUpdate($sql, $parameters, $types);
}
return $stmt;
}
private function getSchema(): Schema
{
$schema = new Schema([], [], $this->driverConnection->getSchemaManager()->createSchemaConfig());