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

[Lock] Create tables in transaction only if supported by driver #44383

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
Dec 20, 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
36 changes: 34 additions & 2 deletions 36 src/Symfony/Component/Lock/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,21 @@ public function save(Key $key)
ParameterType::STRING,
]);
} catch (TableNotFoundException $e) {
$this->createTable();
$this->save($key);
if (!$this->conn->isTransactionActive() || $this->platformSupportsTableCreationInTransaction()) {
$this->createTable();
}

try {
$this->conn->executeStatement($sql, [
$this->getHashedKey($key),
$this->getUniqueToken($key),
], [
ParameterType::STRING,
ParameterType::STRING,
]);
} catch (DBALException $e) {
$this->putOffExpiration($key, $this->initialTtl);
}
} catch (DBALException $e) {
// the lock is already acquired. It could be us. Let's try to put off.
$this->putOffExpiration($key, $this->initialTtl);
Expand Down Expand Up @@ -235,4 +248,23 @@ private function getCurrentTimestampStatement(): string
return (string) time();
}
}

/**
* Checks wether current platform supports table creation within transaction.
fabpot marked this conversation as resolved.
Show resolved Hide resolved
*/
private function platformSupportsTableCreationInTransaction(): bool
{
$platform = $this->conn->getDatabasePlatform();

switch (true) {
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform:
martinssipenko marked this conversation as resolved.
Show resolved Hide resolved
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform:
case $platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform:
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform:
martinssipenko marked this conversation as resolved.
Show resolved Hide resolved
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform:
return true;
default:
return false;
}
}
}
127 changes: 127 additions & 0 deletions 127 src/Symfony/Component/Lock/Tests/Store/DoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@

namespace Symfony\Component\Lock\Tests\Store;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\DoctrineDbalStore;

class_exists(\Doctrine\DBAL\Platforms\PostgreSqlPlatform::class);
jderusse marked this conversation as resolved.
Show resolved Hide resolved

/**
* @author Jérémy Derussé <jeremy@derusse.com>
*
Expand Down Expand Up @@ -87,4 +92,126 @@ public function provideDsn()
yield ['sqlite3:///'.$dbFile.'3', $dbFile.'3'];
yield ['sqlite://localhost/:memory:'];
}

/**
* @dataProvider providePlatforms
*/
public function testCreatesTableInTransaction(string $platform)
{
$conn = $this->createMock(Connection::class);
$conn->expects($this->exactly(3))
->method('executeStatement')
->withConsecutive(
[$this->stringContains('INSERT INTO')],
[$this->matches('create sql stmt')],
[$this->stringContains('INSERT INTO')]
)
->will(
$this->onConsecutiveCalls(
$this->throwException(
$this->createMock(TableNotFoundException::class)
),
1,
1
)
);

$conn->method('isTransactionActive')
->willReturn(true);

$platform = $this->createMock($platform);
$platform->method('getCreateTableSQL')
->willReturn(['create sql stmt']);

$conn->method('getDatabasePlatform')
->willReturn($platform);

$store = new DoctrineDbalStore($conn);

$key = new Key(uniqid(__METHOD__, true));

$store->save($key);
}

public function providePlatforms()
{
yield [\Doctrine\DBAL\Platforms\PostgreSQLPlatform::class];
yield [\Doctrine\DBAL\Platforms\PostgreSQL94Platform::class];
yield [\Doctrine\DBAL\Platforms\SqlitePlatform::class];
yield [\Doctrine\DBAL\Platforms\SQLServerPlatform::class];
yield [\Doctrine\DBAL\Platforms\SQLServer2012Platform::class];
}

public function testTableCreationInTransactionNotSupported()
{
$conn = $this->createMock(Connection::class);
$conn->expects($this->exactly(2))
->method('executeStatement')
->withConsecutive(
[$this->stringContains('INSERT INTO')],
[$this->stringContains('INSERT INTO')]
)
->will(
$this->onConsecutiveCalls(
$this->throwException(
$this->createMock(TableNotFoundException::class)
),
1,
1
)
);

$conn->method('isTransactionActive')
->willReturn(true);

$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getCreateTableSQL')
->willReturn(['create sql stmt']);

$conn->expects($this->exactly(2))
->method('getDatabasePlatform');

$store = new DoctrineDbalStore($conn);

$key = new Key(uniqid(__METHOD__, true));

$store->save($key);
}

public function testCreatesTableOutsideTransaction()
{
$conn = $this->createMock(Connection::class);
$conn->expects($this->exactly(3))
->method('executeStatement')
->withConsecutive(
[$this->stringContains('INSERT INTO')],
[$this->matches('create sql stmt')],
[$this->stringContains('INSERT INTO')]
)
->will(
$this->onConsecutiveCalls(
$this->throwException(
$this->createMock(TableNotFoundException::class)
),
1,
1
)
);

$conn->method('isTransactionActive')
->willReturn(false);

$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getCreateTableSQL')
->willReturn(['create sql stmt']);

$conn->method('getDatabasePlatform')
->willReturn($platform);

$store = new DoctrineDbalStore($conn);

$key = new Key(uniqid(__METHOD__, true));

$store->save($key);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.