Skip to content
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

Default to charset utf8mb4 for mysql and utf8 otherwise #1098

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Messenger\Transport\Doctrine\DoctrineTransportFactory;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use function class_exists;
use function in_array;
use function sprintf;

/**
Expand Down Expand Up @@ -199,6 +200,14 @@ protected function getConnectionOptions($connection)
{
$options = $connection;

if (! isset($options['charset'])) {
if (isset($options['url'])) {
$options['charset'] = strpos($options['url'], 'mysql') === 0 ? 'utf8mb4' : 'utf8';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scheme might also be pdo_mysql or mysqli as that's the name of the DBAL drivers, which are also supported as schemes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scheme mysqli also starts with mysql, so that should be fine.

I didn't account for pdo_mysql, I will look into that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think pdo_mysql can be use as a scheme, parse_url doesn't work with it. So that leaves just mysql, mysql2 and mysqli as a scheme.

$url = 'pdo_mysql://dbuser:dbpassword@127.0.0.1:3306/dbname?charset=utf8';
var_dump(parse_url($url));

array(2) {
  ["path"]=>  string(51) "pdo_mysql://dbuser:dbpassword@127.0.0.1:3306/dbname"
  ["query"]=>  string(12) "charset=utf8"
}

} else {
$options['charset'] = in_array($options['driver'], ['mysqli', 'pdo_mysql'], true) ? 'utf8mb4' : 'utf8';
}
}

if (isset($options['platform_service'])) {
$options['platform'] = new Reference($options['platform_service']);
unset($options['platform_service']);
Expand Down
5 changes: 5 additions & 0 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Expand Up @@ -123,6 +123,7 @@ public function testDbalLoadSingleMasterSlaveConnection()
'dbname' => 'mysql_db',
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'charset' => 'utf8mb4',
],
$param['master']
);
Expand Down Expand Up @@ -157,6 +158,7 @@ public function testDbalLoadPoolShardingConnection()
'dbname' => 'mysql_db',
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'charset' => 'utf8mb4',
],
$param['global']
);
Expand Down Expand Up @@ -207,6 +209,7 @@ public function testLoadSimpleSingleConnection()
'driver' => 'pdo_mysql',
'driverOptions' => [],
'defaultTableOptions' => [],
'charset' => 'utf8mb4',
],
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -242,6 +245,7 @@ public function testLoadSimpleSingleConnectionWithoutDbName()
'driver' => 'pdo_mysql',
'driverOptions' => [],
'defaultTableOptions' => [],
'charset' => 'utf8mb4',
],
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -278,6 +282,7 @@ public function testLoadSingleConnection()
'dbname' => 'sqlite_db',
'memory' => true,
'defaultTableOptions' => [],
'charset' => 'utf8',
],
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down