Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e8d10cb

Browse filesBrowse files
committed
[Lock] Fix PDO store not creating table
1 parent 58353d1 commit e8d10cb
Copy full SHA for e8d10cb

File tree

1 file changed

+32
-4
lines changed
Filter options

1 file changed

+32
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/PdoStore.php
+32-4Lines changed: 32 additions & 4 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
@@ -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
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.