Skip to content

[Lock] Remove support of Doctrine DBAL in PostgreSqlStore #43587

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

Merged
merged 1 commit into from
Oct 19, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CHANGELOG

* Remove the `NotSupportedException`. It shouldn't be thrown anymore
* Remove the `RetryTillSaveStore`. Logic has been moved in `Lock` and is not needed anymore
* Remove support of Doctrine DBAL in `PdoStore`
* Remove support of Doctrine DBAL in `PdoStore` and `PostgreSqlStore`

5.4
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,27 @@
*/
class DoctrineDbalPostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStoreInterface
{
private $conn;
private Connection $conn;
private static $storeRegistry = [];

/**
* You can either pass an existing database connection a Doctrine DBAL Connection
* or a URL that will be used to connect to the database.
*
* @param Connection|string $connOrUrl A Connection instance or Doctrine URL
*
* @throws InvalidArgumentException When first argument is not Connection nor string
*/
public function __construct($connOrUrl)
public function __construct(Connection|string $connOrUrl)
{
if ($connOrUrl instanceof Connection) {
if (!$connOrUrl->getDatabasePlatform() instanceof PostgreSQLPlatform) {
throw new InvalidArgumentException(sprintf('The adapter "%s" does not support the "%s" platform.', __CLASS__, \get_class($connOrUrl->getDatabasePlatform())));
}
$this->conn = $connOrUrl;
} elseif (\is_string($connOrUrl)) {
} else {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
}
$this->conn = DriverManager::getConnection(['url' => $this->filterDsn($connOrUrl)]);
} else {
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be "%s" or string, "%s" given.', Connection::class, __METHOD__, get_debug_type($connOrUrl)));
}
}

Expand Down
57 changes: 2 additions & 55 deletions src/Symfony/Component/Lock/Store/PostgreSqlStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Lock\Store;

use Doctrine\DBAL\Connection;
use Symfony\Component\Lock\BlockingSharedLockStoreInterface;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
Expand All @@ -27,15 +26,13 @@
*/
class PostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStoreInterface
{
private \PDO|Connection $conn;
private \PDO $conn;
private string $dsn;
private string $username = '';
private string $password = '';
private array $connectionOptions = [];
private static array $storeRegistry = [];

private $dbalStore;

/**
* You can either pass an existing database connection as PDO instance or
* a DSN string that will be used to lazy-connect to the database when the
Expand All @@ -52,23 +49,13 @@ class PostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStore
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
* @throws InvalidArgumentException When namespace contains invalid characters
*/
public function __construct(\PDO|Connection|string $connOrDsn, array $options = [])
public function __construct(\PDO|string $connOrDsn, array $options = [])
{
if ($connOrDsn instanceof Connection || (\is_string($connOrDsn) && str_contains($connOrDsn, '://'))) {
trigger_deprecation('symfony/lock', '5.4', 'Usage of a DBAL Connection with "%s" is deprecated and will be removed in symfony 6.0. Use "%s" instead.', __CLASS__, DoctrineDbalPostgreSqlStore::class);
$this->dbalStore = new DoctrineDbalPostgreSqlStore($connOrDsn);

return;
}

if ($connOrDsn instanceof \PDO) {
if (\PDO::ERRMODE_EXCEPTION !== $connOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) {
throw new InvalidArgumentException(sprintf('"%s" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)).', __METHOD__));
}

$this->conn = $connOrDsn;
$this->checkDriver();
} elseif ($connOrDsn instanceof Connection) {
$this->conn = $connOrDsn;
$this->checkDriver();
} else {
Expand All @@ -82,12 +69,6 @@ public function __construct(\PDO|Connection|string $connOrDsn, array $options =

public function save(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->save($key);

return;
}

// prevent concurrency within the same connection
$this->getInternalStore()->save($key);

Expand All @@ -110,12 +91,6 @@ public function save(Key $key)

public function saveRead(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->saveRead($key);

return;
}

// prevent concurrency within the same connection
$this->getInternalStore()->saveRead($key);

Expand All @@ -139,12 +114,6 @@ public function saveRead(Key $key)

public function putOffExpiration(Key $key, float $ttl)
{
if (isset($this->dbalStore)) {
$this->dbalStore->putOffExpiration($key);

return;
}

// postgresql locks forever.
// check if lock still exists
if (!$this->exists($key)) {
Expand All @@ -154,12 +123,6 @@ public function putOffExpiration(Key $key, float $ttl)

public function delete(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->delete($key);

return;
}

// Prevent deleting locks own by an other key in the same connection
if (!$this->exists($key)) {
return;
Expand All @@ -182,10 +145,6 @@ public function delete(Key $key)

public function exists(Key $key): bool
{
if (isset($this->dbalStore)) {
return $this->dbalStore->exists($key);
}

$sql = "SELECT count(*) FROM pg_locks WHERE locktype='advisory' AND objid=:key AND pid=pg_backend_pid()";
$stmt = $this->getConnection()->prepare($sql);

Expand All @@ -202,12 +161,6 @@ public function exists(Key $key): bool

public function waitAndSave(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->waitAndSave($key);

return;
}

// prevent concurrency within the same connection
// Internal store does not allow blocking mode, because there is no way to acquire one in a single process
$this->getInternalStore()->save($key);
Expand All @@ -224,12 +177,6 @@ public function waitAndSave(Key $key)

public function waitAndSaveRead(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->waitAndSaveRead($key);

return;
}

// prevent concurrency within the same connection
// Internal store does not allow blocking mode, because there is no way to acquire one in a single process
$this->getInternalStore()->saveRead($key);
Expand Down

This file was deleted.