Skip to content

[Lock][Cache] Allows URL DSN in PDO adapters #34057

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 29, 2019
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
29 changes: 29 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,33 @@ public function testCleanupExpiredItems()
$this->assertFalse($newItem->isHit());
$this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
}

/**
* @dataProvider provideDsn
*/
public function testDsn(string $dsn, string $file = null)
{
try {
$pool = new PdoAdapter($dsn);
$pool->createTable();

$item = $pool->getItem('key');
$item->set('value');
$this->assertTrue($pool->save($item));
} finally {
if (null !== $file) {
@unlink($file);
}
}
}

public function provideDsn()
{
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
yield ['sqlite:'.$dbFile, ''.$dbFile];
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
yield ['sqlite://localhost/:memory:'];
yield ['sqlite::memory:'];
}
}
12 changes: 10 additions & 2 deletions src/Symfony/Component/Cache/Traits/PdoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -370,8 +371,15 @@ protected function doSave(array $values, $lifetime)
private function getConnection()
{
if (null === $this->conn) {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (strpos($this->dsn, '://')) {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
}
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
} else {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}
if (null === $this->driver) {
if ($this->conn instanceof \PDO) {
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
Expand Down Expand Up @@ -229,8 +230,15 @@ private function getUniqueToken(Key $key): string
private function getConnection()
{
if (null === $this->conn) {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (strpos($this->dsn, '://')) {
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
}
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
} else {
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}

return $this->conn;
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,34 @@ public function testInvalidTtlConstruct()

return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
}

/**
* @dataProvider provideDsn
*/
public function testDsn(string $dsn, string $file = null)
{
$key = new Key(uniqid(__METHOD__, true));

try {
$store = new PdoStore($dsn);
$store->createTable();

$store->save($key);
$this->assertTrue($store->exists($key));
} finally {
if (null !== $file) {
@unlink($file);
}
}
}

public function provideDsn()
{
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
yield ['sqlite:'.$dbFile, ''.$dbFile];
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
yield ['sqlite://localhost/:memory:'];
yield ['sqlite::memory:'];
}
}
5 changes: 4 additions & 1 deletion src/Symfony/Component/Lock/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"psr/log": "~1.0"
},
"require-dev": {
"doctrine/dbal": "~2.4",
"doctrine/dbal": "~2.5",
"mongodb/mongodb": "~1.1",
"predis/predis": "~1.0"
},
"conflict": {
"doctrine/dbal": "<2.5"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Lock\\": "" },
"exclude-from-classmap": [
Expand Down