Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cache] Remove database server version detection #52720

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Cache
-----

* Add parameter `\Closure $isSameDatabase` to `DoctrineDbalAdapter::configureSchema()`
* Drop support for Postgres < 9.5 and SQL Server < 2008 in `DoctrineDbalAdapter`

Config
------
Expand Down
31 changes: 6 additions & 25 deletions src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\ServerVersionProvider;
use Doctrine\DBAL\Tools\DsnParser;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
Expand All @@ -35,7 +33,6 @@
private MarshallerInterface $marshaller;
private Connection $conn;
private string $platformName;
private string $serverVersion;
private string $table = 'cache_items';
private string $idCol = 'item_id';
private string $dataCol = 'item_data';
Expand Down Expand Up @@ -236,27 +233,27 @@
$platformName = $this->getPlatformName();
$insertSql = "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?)";

switch (true) {
case 'mysql' === $platformName:
switch ($platformName) {
case 'mysql':
$sql = $insertSql." ON DUPLICATE KEY UPDATE $this->dataCol = VALUES($this->dataCol), $this->lifetimeCol = VALUES($this->lifetimeCol), $this->timeCol = VALUES($this->timeCol)";
break;
case 'oci' === $platformName:
case 'oci':
// DUAL is Oracle specific dummy table
$sql = "MERGE INTO $this->table USING DUAL ON ($this->idCol = ?) ".
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?) ".
"WHEN MATCHED THEN UPDATE SET $this->dataCol = ?, $this->lifetimeCol = ?, $this->timeCol = ?";
break;
case 'sqlsrv' === $platformName && version_compare($this->getServerVersion(), '10', '>='):
case 'sqlsrv':
// MERGE is only available since SQL Server 2008 and must be terminated by semicolon
// It also requires HOLDLOCK according to http://weblogs.sqlteam.com/dang/archive/2009/01/31/UPSERT-Race-Condition-With-MERGE.aspx
$sql = "MERGE INTO $this->table WITH (HOLDLOCK) USING (SELECT 1 AS dummy) AS src ON ($this->idCol = ?) ".
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?) ".
"WHEN MATCHED THEN UPDATE SET $this->dataCol = ?, $this->lifetimeCol = ?, $this->timeCol = ?;";
break;
case 'sqlite' === $platformName:
case 'sqlite':
$sql = 'INSERT OR REPLACE'.substr($insertSql, 6);
break;
case 'pgsql' === $platformName && version_compare($this->getServerVersion(), '9.5', '>='):
case 'pgsql':
$sql = $insertSql." ON CONFLICT ($this->idCol) DO UPDATE SET ($this->dataCol, $this->lifetimeCol, $this->timeCol) = (EXCLUDED.$this->dataCol, EXCLUDED.$this->lifetimeCol, EXCLUDED.$this->timeCol)";
break;
default:
Expand Down Expand Up @@ -332,7 +329,7 @@
return $failed;
}

/**

Check failure on line 332 in src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

NullableReturnStatement

src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php:332:16: NullableReturnStatement: The declared return type 'array<array-key, mixed>|bool' for Symfony\Component\Cache\Adapter\DoctrineDbalAdapter::doSave is not nullable, but the function returns 'array<array-key, mixed>|null' (see https://psalm.dev/139)

Check failure on line 332 in src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

NullableReturnStatement

src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php:332:16: NullableReturnStatement: The declared return type 'array<array-key, mixed>|bool' for Symfony\Component\Cache\Adapter\DoctrineDbalAdapter::doSave is not nullable, but the function returns 'array<array-key, mixed>|null' (see https://psalm.dev/139)
* @internal
*/
protected function getId(mixed $key): string
Expand Down Expand Up @@ -366,22 +363,6 @@
};
}

private function getServerVersion(): string
{
if (isset($this->serverVersion)) {
return $this->serverVersion;
}

if ($this->conn instanceof ServerVersionProvider || $this->conn instanceof ServerInfoAwareConnection) {
return $this->serverVersion = $this->conn->getServerVersion();
}

// The condition should be removed once support for DBAL <3.3 is dropped
$conn = method_exists($this->conn, 'getNativeConnection') ? $this->conn->getNativeConnection() : $this->conn->getWrappedConnection();

return $this->serverVersion = $conn->getAttribute(\PDO::ATTR_SERVER_VERSION);
}

private function addTableToSchema(Schema $schema): void
{
$types = [
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add parameter `$isSameDatabase` to `DoctrineDbalAdapter::configureSchema()`
* Drop support for Postgres < 9.5 and SQL Server < 2008 in `DoctrineDbalAdapter`

6.4
---
Expand Down