Skip to content

Commit

Permalink
Merge branch '2.12.x' into 3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Nov 14, 2020
2 parents 041df42 + c2c8488 commit 0f55c0d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/DriverManager.php
Expand Up @@ -109,7 +109,6 @@ private function __construct()
*
* @throws Exception
*
* @phpstan-param mixed[] $params
* @psalm-return ($params is array{wrapperClass:mixed} ? T : Connection)
* @template T of Connection
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Platforms/SQLServer2012Platform.php
Expand Up @@ -1540,8 +1540,6 @@ public function appendLockHint(string $fromClause, int $lockMode): string
{
switch ($lockMode) {
case LockMode::NONE:
return $fromClause . ' WITH (NOLOCK)';

case LockMode::OPTIMISTIC:
return $fromClause;

Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionTest.php
Expand Up @@ -32,7 +32,7 @@ class ConnectionTest extends TestCase
/** @var Connection */
private $connection;

/** @var string[] */
/** @var array{wrapperClass?: class-string<Connection>} */
protected $params = [
'driver' => 'pdo_mysql',
'host' => 'localhost',
Expand Down
101 changes: 101 additions & 0 deletions tests/Functional/LockMode/NoneTest.php
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\LockMode;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\TransactionIsolationLevel;

class NoneTest extends FunctionalTestCase
{
/** @var Connection */
private $connection2;

public function setUp(): void
{
parent::setUp();

if ($this->connection->getDriver() instanceof OCI8\Driver) {
// https://github.com/doctrine/dbal/issues/4417
self::markTestSkipped('This test fails on OCI8 for a currently unknown reason');
}

if ($this->connection->getDatabasePlatform() instanceof SQLServer2012Platform) {
// Use row versioning instead of locking on SQL Server (if we don't, the second connection will block when
// attempting to read the row created by the first connection, instead of reading the previous version);
// for some reason we cannot set READ_COMMITTED_SNAPSHOT ON when not running this test in isolation,
// there may be another connection active at this point; temporarily forcing to SINGLE_USER does the trick.
$db = $this->connection->getDatabase();
$this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE');
$this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET READ_COMMITTED_SNAPSHOT ON');
$this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET MULTI_USER');
}

$table = new Table('users');
$table->addColumn('id', 'integer');
$table->setPrimaryKey(['id']);

$this->connection->getSchemaManager()->dropAndCreateTable($table);

$this->connection2 = TestUtil::getConnection();

if ($this->connection2->getSchemaManager()->tablesExist('users')) {
return;
}

if ($this->connection2->getDatabasePlatform() instanceof SqlitePlatform) {
self::markTestSkipped('This test cannot run on SQLite using an in-memory database');
}

self::fail('Separate connections do not seem to talk to the same database');
}

public function tearDown(): void
{
parent::tearDown();

if ($this->connection2->isTransactionActive()) {
$this->connection2->rollBack();
}

$this->connection2->close();

$this->connection->getSchemaManager()->dropTable('users');

if (! $this->connection->getDatabasePlatform() instanceof SQLServer2012Platform) {
return;
}

$db = $this->connection->getDatabase();
$this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET READ_COMMITTED_SNAPSHOT OFF');
}

public function testLockModeNoneDoesNotBreakTransactionIsolation(): void
{
try {
$this->connection->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED);
$this->connection2->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED);
} catch (Exception $e) {
self::markTestSkipped('This test must be able to set a transaction isolation level');
}

$this->connection->beginTransaction();
$this->connection2->beginTransaction();

$this->connection->insert('users', ['id' => 1]);

$query = 'SELECT id FROM users';
$query = $this->connection2->getDatabasePlatform()->appendLockHint($query, LockMode::NONE);

self::assertFalse($this->connection2->fetchOne($query));
}
}
4 changes: 2 additions & 2 deletions tests/Platforms/AbstractSQLServerPlatformTestCase.php
Expand Up @@ -386,14 +386,14 @@ public function testModifyLimitQueryWithExtraLongQuery(): void
public function testModifyLimitQueryWithOrderByClause(): void
{
$sql = 'SELECT m0_.NOMBRE AS NOMBRE0, m0_.FECHAINICIO AS FECHAINICIO1, m0_.FECHAFIN AS FECHAFIN2'
. ' FROM MEDICION m0_ WITH (NOLOCK)'
. ' FROM MEDICION m0_'
. ' INNER JOIN ESTUDIO e1_ ON m0_.ESTUDIO_ID = e1_.ID'
. ' INNER JOIN CLIENTE c2_ ON e1_.CLIENTE_ID = c2_.ID'
. ' INNER JOIN USUARIO u3_ ON c2_.ID = u3_.CLIENTE_ID'
. ' WHERE u3_.ID = ? ORDER BY m0_.FECHAINICIO DESC';

$expected = 'SELECT m0_.NOMBRE AS NOMBRE0, m0_.FECHAINICIO AS FECHAINICIO1, m0_.FECHAFIN AS FECHAFIN2'
. ' FROM MEDICION m0_ WITH (NOLOCK)'
. ' FROM MEDICION m0_'
. ' INNER JOIN ESTUDIO e1_ ON m0_.ESTUDIO_ID = e1_.ID'
. ' INNER JOIN CLIENTE c2_ ON e1_.CLIENTE_ID = c2_.ID'
. ' INNER JOIN USUARIO u3_ ON c2_.ID = u3_.CLIENTE_ID'
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/SQLServerPlatformTest.php
Expand Up @@ -30,7 +30,7 @@ public function testAppendsLockHint(int $lockMode, string $lockHint): void
public static function getLockHints(): iterable
{
return [
[LockMode::NONE, ' WITH (NOLOCK)'],
[LockMode::NONE, ''],
[LockMode::OPTIMISTIC, ''],
[LockMode::PESSIMISTIC_READ, ' WITH (HOLDLOCK, ROWLOCK)'],
[LockMode::PESSIMISTIC_WRITE, ' WITH (UPDLOCK, ROWLOCK)'],
Expand Down

0 comments on commit 0f55c0d

Please sign in to comment.