Skip to content

Commit

Permalink
Merge branch '5.4' into 6.2
Browse files Browse the repository at this point in the history
* 5.4:
  Fix Doctrine deprecations
  [Validator] Remove internal from methods on non-internal interfaces
  [PhpUnitBridge] Fix support for the NO_COLOR env var
  • Loading branch information
nicolas-grekas committed Jun 2, 2023
2 parents fe45278 + b103cb0 commit d8364cf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
27 changes: 26 additions & 1 deletion Store/DoctrineDbalPostgreSqlStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Symfony\Component\Lock\Store;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
use Doctrine\DBAL\Tools\DsnParser;
use Doctrine\ORM\ORMSetup;
use Symfony\Component\Lock\BlockingSharedLockStoreInterface;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -49,7 +53,28 @@ public function __construct(Connection|string $connOrUrl)
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
}
$this->conn = DriverManager::getConnection(['url' => $this->filterDsn($connOrUrl)]);
if (class_exists(DsnParser::class)) {
$params = (new DsnParser([
'db2' => 'ibm_db2',
'mssql' => 'pdo_sqlsrv',
'mysql' => 'pdo_mysql',
'mysql2' => 'pdo_mysql',
'postgres' => 'pdo_pgsql',
'postgresql' => 'pdo_pgsql',
'pgsql' => 'pdo_pgsql',
'sqlite' => 'pdo_sqlite',
'sqlite3' => 'pdo_sqlite',
]))->parse($this->filterDsn($connOrUrl));
} else {
$params = ['url' => $this->filterDsn($connOrUrl)];
}

$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration() : new Configuration();
if (class_exists(DefaultSchemaManagerFactory::class)) {
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
}

$this->conn = DriverManager::getConnection($params, $config);
}
}

Expand Down
27 changes: 26 additions & 1 deletion Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Symfony\Component\Lock\Store;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Tools\DsnParser;
use Doctrine\ORM\ORMSetup;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
Expand Down Expand Up @@ -68,7 +72,28 @@ public function __construct(Connection|string $connOrUrl, array $options = [], f
if (!class_exists(DriverManager::class)) {
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
}
$this->conn = DriverManager::getConnection(['url' => $connOrUrl]);
if (class_exists(DsnParser::class)) {
$params = (new DsnParser([
'db2' => 'ibm_db2',
'mssql' => 'pdo_sqlsrv',
'mysql' => 'pdo_mysql',
'mysql2' => 'pdo_mysql',
'postgres' => 'pdo_pgsql',
'postgresql' => 'pdo_pgsql',
'pgsql' => 'pdo_pgsql',
'sqlite' => 'pdo_sqlite',
'sqlite3' => 'pdo_sqlite',
]))->parse($connOrUrl);
} else {
$params = ['url' => $connOrUrl];
}

$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration() : new Configuration();
if (class_exists(DefaultSchemaManagerFactory::class)) {
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
}

$this->conn = DriverManager::getConnection($params, $config);
}
}

Expand Down
19 changes: 17 additions & 2 deletions Tests/Store/DoctrineDbalPostgreSqlStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Symfony\Component\Lock\Tests\Store;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
use Doctrine\DBAL\Tools\DsnParser;
use Doctrine\ORM\ORMSetup;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
Expand All @@ -38,7 +42,7 @@ public function createPostgreSqlConnection(): Connection
$this->markTestSkipped('Missing POSTGRES_HOST env variable');
}

return DriverManager::getConnection(['url' => 'pgsql://postgres:password@'.getenv('POSTGRES_HOST')]);
return self::getDbalConnection('pdo-pgsql://postgres:password@'.getenv('POSTGRES_HOST'));
}

public function getStore(): PersistingStoreInterface
Expand All @@ -65,7 +69,7 @@ public function testInvalidDriver($connOrDsn)
public static function getInvalidDrivers()
{
yield ['sqlite:///tmp/foo.db'];
yield [DriverManager::getConnection(['url' => 'sqlite:///tmp/foo.db'])];
yield [self::getDbalConnection('sqlite:///tmp/foo.db')];
}

public function testSaveAfterConflict()
Expand Down Expand Up @@ -165,4 +169,15 @@ public function testWaitAndSaveReadAfterConflictReleasesLockFromInternalStore()

$this->assertTrue($store2->exists($store2Key));
}

private static function getDbalConnection(string $dsn): Connection
{
$params = class_exists(DsnParser::class) ? (new DsnParser(['sqlite' => 'pdo_sqlite']))->parse($dsn) : ['url' => $dsn];
$config = class_exists(ORMSetup::class) ? ORMSetup::createConfiguration(true) : new Configuration();
if (class_exists(DefaultSchemaManagerFactory::class)) {
$config->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
}

return DriverManager::getConnection($params, $config);
}
}

0 comments on commit d8364cf

Please sign in to comment.