Skip to content

[Lock] Remove support of Doctrine DBAL in PdoStore #43585

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
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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`

5.4
---
Expand Down
9 changes: 3 additions & 6 deletions src/Symfony/Component/Lock/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class DoctrineDbalStore implements PersistingStoreInterface
use DatabaseTableTrait;
use ExpiringStoreTrait;

private $conn;
private $dsn;
private Connection $conn;

/**
* List of available options:
Expand All @@ -58,19 +57,17 @@ class DoctrineDbalStore implements PersistingStoreInterface
* @throws InvalidArgumentException When namespace contains invalid characters
* @throws InvalidArgumentException When the initial ttl is not valid
*/
public function __construct($connOrUrl, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
public function __construct(Connection|string $connOrUrl, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
{
$this->init($options, $gcProbability, $initialTtl);

if ($connOrUrl instanceof Connection) {
$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' => $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: 1 addition & 56 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Lock\Store;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
Expand Down Expand Up @@ -44,8 +42,6 @@ class PdoStore implements PersistingStoreInterface
private string $password = '';
private array $connectionOptions = [];

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
Expand All @@ -68,15 +64,8 @@ class PdoStore implements PersistingStoreInterface
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
* @throws InvalidArgumentException When the initial ttl is not valid
*/
public function __construct(\PDO|Connection|string $connOrDsn, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
public function __construct(\PDO|string $connOrDsn, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
{
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__, DoctrineDbalStore::class);
$this->dbalStore = new DoctrineDbalStore($connOrDsn, $options, $gcProbability, $initialTtl);

return;
}

$this->init($options, $gcProbability, $initialTtl);

if ($connOrDsn instanceof \PDO) {
Expand All @@ -99,12 +88,6 @@ public function __construct(\PDO|Connection|string $connOrDsn, array $options =
*/
public function save(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->save($key);

return;
}

$key->reduceLifetime($this->initialTtl);

$sql = "INSERT INTO $this->table ($this->idCol, $this->tokenCol, $this->expirationCol) VALUES (:id, :token, {$this->getCurrentTimestampStatement()} + $this->initialTtl)";
Expand Down Expand Up @@ -137,12 +120,6 @@ public function save(Key $key)
*/
public function putOffExpiration(Key $key, float $ttl)
{
if (isset($this->dbalStore)) {
$this->dbalStore->putOffExpiration($key, $ttl);

return;
}

if ($ttl < 1) {
throw new InvalidTtlException(sprintf('"%s()" expects a TTL greater or equals to 1 second. Got "%s".', __METHOD__, $ttl));
}
Expand Down Expand Up @@ -171,12 +148,6 @@ public function putOffExpiration(Key $key, float $ttl)
*/
public function delete(Key $key)
{
if (isset($this->dbalStore)) {
$this->dbalStore->delete($key);

return;
}

$sql = "DELETE FROM $this->table WHERE $this->idCol = :id AND $this->tokenCol = :token";
$stmt = $this->getConnection()->prepare($sql);

Expand All @@ -190,10 +161,6 @@ public function delete(Key $key)
*/
public function exists(Key $key): bool
{
if (isset($this->dbalStore)) {
return $this->dbalStore->exists($key);
}

$sql = "SELECT 1 FROM $this->table WHERE $this->idCol = :id AND $this->tokenCol = :token AND $this->expirationCol > {$this->getCurrentTimestampStatement()}";
$stmt = $this->getConnection()->prepare($sql);

Expand Down Expand Up @@ -222,12 +189,6 @@ private function getConnection(): \PDO
*/
public function createTable(): void
{
if (isset($this->dbalStore)) {
$this->dbalStore->createTable();

return;
}

// connect if we are not yet
$conn = $this->getConnection();
$driver = $this->getDriver();
Expand Down Expand Up @@ -255,22 +216,6 @@ public function createTable(): void
$conn->exec($sql);
}

/**
* Adds the Table to the Schema if it doesn't exist.
*
* @deprecated since symfony/lock 5.4 use DoctrineDbalStore instead
*/
public function configureSchema(Schema $schema): void
{
if (isset($this->dbalStore)) {
$this->dbalStore->configureSchema($schema);

return;
}

throw new \BadMethodCallException(sprintf('"%s::%s()" is only supported when using a doctrine/dbal Connection.', __CLASS__, __METHOD__));
}

/**
* Cleans up the table by removing all expired locks.
*/
Expand Down
109 changes: 0 additions & 109 deletions src/Symfony/Component/Lock/Tests/Store/PdoDbalStoreTest.php

This file was deleted.