Skip to content

Commit

Permalink
Merge pull request #306 from alexislefebvre/3.x-feat-support-doctrine…
Browse files Browse the repository at this point in the history
…-dbal-4

[3.x] feat: support doctrine/dbal 4
  • Loading branch information
alexislefebvre authored May 4, 2024
2 parents 17124fe + 47f2a73 commit 6ddd3d5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ jobs:
- php-version: 8.1
# add a specific job to test ^5.4 for all Symfony packages
symfony-version: "^5.4"
# `theofidry/alice-data-fixtures:1.6.0` and `doctrine/dbal:^4.0` cause issues:
# Error: Call to undefined method Doctrine\DBAL\Connection::exec()
composer-flags: "require doctrine/dbal:^3.0"
- php-version: 8.2
symfony-version: "^7.0"
- php-version: 8.3
symfony-version: "^7.0"

services:
mysql:
image: mysql:5.7
mariadb:
image: mariadb:11.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: acme
MARIADB_ROOT_PASSWORD: root
MARIADB_DATABASE: acme
ports:
- 3306:3306
postgresql:
image: postgres:9.6
image: postgres:15-alpine
env:
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"conflict": {
"doctrine/annotations": "<1.13.1 || >=3.0",
"doctrine/dbal": "<2.13.1 || ~3.0.0 || >=4.0",
"doctrine/dbal": "<2.13.1 || ~3.0.0 || >=5.0",
"doctrine/mongodb-odm": "<2.2 || >=3.0",
"doctrine/orm": "<2.14 || >=4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.1'
services:
mariadb:
image: 'mariadb:11.0'
image: 'mariadb:11'
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=acme
Expand Down
19 changes: 18 additions & 1 deletion src/Services/DatabaseToolCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function __construct(ContainerInterface $container, mixed $annotationRead

public function add(AbstractDatabaseTool $databaseTool): void
{
$this->items[$databaseTool->getType()][$databaseTool->getDriverName()] = $databaseTool;
$driverName = self::normalizeDriverName($databaseTool->getDriverName());

$this->items[$databaseTool->getType()][$driverName] = $databaseTool;
}

public function get($omName = null, $registryName = 'doctrine', ?int $purgeMode = null): AbstractDatabaseTool
Expand All @@ -49,6 +51,8 @@ public function get($omName = null, $registryName = 'doctrine', ?int $purgeMode
$registry = $this->container->get($registryName);
$driverName = ('ORM' === $registry->getName()) ? \get_class($registry->getConnection()->getDatabasePlatform()) : 'default';

$driverName = self::normalizeDriverName($driverName);

$databaseTool = $this->items[$registry->getName()][$driverName] ?? $this->items[$registry->getName()]['default'];

$databaseTool->setRegistry($registry);
Expand All @@ -57,4 +61,17 @@ public function get($omName = null, $registryName = 'doctrine', ?int $purgeMode

return $databaseTool;
}

/**
* On doctrine/dbal ^4.0, the class is named `SQLitePlatform`.
* On doctrine/dbal < 4.0, the class is named `SqlitePlatform`.
*/
private static function normalizeDriverName(string $driverName): string
{
if ('Doctrine\DBAL\Platforms\SqlitePlatform' === $driverName) {
return 'Doctrine\DBAL\Platforms\SQLitePlatform';
}

return $driverName;
}
}
1 change: 1 addition & 0 deletions tests/AppConfigMysql/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ doctrine:
dbname: acme
user: root
password: root
server_version: '11'
2 changes: 1 addition & 1 deletion tests/AppConfigMysqlUrl/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

doctrine:
dbal:
url: 'mysql://root:root@mariadb:3306/foobar'
url: 'mysql://root:root@mariadb:3306/foobar?serverVersion=11'
driver: pdo_mysql
1 change: 1 addition & 0 deletions tests/AppConfigPgsql/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ doctrine:
dbname: postgres
user: postgres
password: postgres
server_version: '15'

0 comments on commit 6ddd3d5

Please sign in to comment.