Skip to content

Commit 4c13bab

Browse files
committed
[Lock] Fix PDO store not creating table
1 parent 58353d1 commit 4c13bab

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

src/Symfony/Component/Lock/Store/PdoStore.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* Lock metadata are stored in a table. You can use createTable() to initialize
2626
* a correctly defined table.
27-
27+
*
2828
* CAUTION: This store relies on all client and server nodes to have
2929
* synchronized clocks for lock expiry to occur at the correct time.
3030
* To ensure locks don't expire prematurely; the TTLs should be set with enough
@@ -40,8 +40,8 @@ class PdoStore implements PersistingStoreInterface
4040
private $conn;
4141
private $dsn;
4242
private $driver;
43-
private $username = '';
44-
private $password = '';
43+
private $username = null;
44+
private $password = null;
4545
private $connectionOptions = [];
4646

4747
private $dbalStore;
@@ -115,7 +115,7 @@ public function save(Key $key)
115115
try {
116116
$stmt = $conn->prepare($sql);
117117
} catch (\PDOException $e) {
118-
if (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true)) {
118+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
119119
$this->createTable();
120120
}
121121
$stmt = $conn->prepare($sql);
@@ -127,8 +127,19 @@ public function save(Key $key)
127127
try {
128128
$stmt->execute();
129129
} catch (\PDOException $e) {
130-
// the lock is already acquired. It could be us. Let's try to put off.
131-
$this->putOffExpiration($key, $this->initialTtl);
130+
if ($this->isTableMissing($e) && (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true))) {
131+
$this->createTable();
132+
133+
try {
134+
$stmt->execute();
135+
} catch (\PDOException $e) {
136+
// the lock is already acquired. It could be us. Let's try to put off.
137+
$this->putOffExpiration($key, $this->initialTtl);
138+
}
139+
} else {
140+
// the lock is already acquired. It could be us. Let's try to put off.
141+
$this->putOffExpiration($key, $this->initialTtl);
142+
}
132143
}
133144

134145
$this->randomlyPrune();
@@ -316,4 +327,21 @@ private function getCurrentTimestampStatement(): string
316327
return (string) time();
317328
}
318329
}
330+
331+
private function isTableMissing(\PDOException $exception): bool
332+
{
333+
$driver = $this->getDriver();
334+
$code = $exception->getCode();
335+
336+
switch (true) {
337+
case 'pgsql' === $driver && '42P01' === $code:
338+
case 'sqlite' === $driver && str_contains($exception->getMessage(), 'no such table:'):
339+
case 'oci' === $driver && 942 === $code:
340+
case 'sqlsrv' === $driver && 208 === $code:
341+
case 'mysql' === $driver && 1146 === $code:
342+
return true;
343+
default:
344+
return false;
345+
}
346+
}
319347
}

src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ public function testDsn(string $dsn, string $file = null)
9999
public static function provideDsn()
100100
{
101101
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
102-
yield ['sqlite:'.$dbFile.'2', $dbFile.'2'];
103-
yield ['sqlite::memory:'];
102+
yield 'SQLite file' => ['sqlite:'.$dbFile.'2', $dbFile.'2'];
103+
yield 'SQLite in memory' => ['sqlite::memory:'];
104+
105+
if ($host = getenv('POSTGRES_HOST')) {
106+
yield 'PostgreSQL' => ['pgsql:host='.$host.';user=postgres;password=password'];
107+
}
104108
}
105109
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy