forked from doctrine/dbal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edbf307
commit 9f0e65a
Showing
16 changed files
with
473 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL; | ||
|
||
/** @internal */ | ||
interface CharsetMetadataProvider | ||
{ | ||
public function normalizeCharset(string $charset): string; | ||
|
||
public function getDefaultCharsetCollation(string $charset): ?string; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Platforms/MySQL/CharsetMetadataProvider/CachingCharsetMetadataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
use Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
use function array_key_exists; | ||
|
||
/** @internal */ | ||
final class CachingCharsetMetadataProvider implements CharsetMetadataProvider | ||
{ | ||
/** @var array<string,?string> */ | ||
private array $cache = []; | ||
|
||
public function __construct(private readonly CharsetMetadataProvider $charsetMetadataProvider) | ||
{ | ||
} | ||
|
||
public function normalizeCharset(string $charset): string | ||
{ | ||
return $this->charsetMetadataProvider->normalizeCharset($charset); | ||
} | ||
|
||
public function getDefaultCharsetCollation(string $charset): ?string | ||
{ | ||
if (array_key_exists($charset, $this->cache)) { | ||
return $this->cache[$charset]; | ||
} | ||
|
||
return $this->cache[$charset] = $this->charsetMetadataProvider->getDefaultCharsetCollation($charset); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Platforms/MySQL/CharsetMetadataProvider/ConnectionCharsetMetadataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception; | ||
use Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; | ||
|
||
/** @internal */ | ||
final class ConnectionCharsetMetadataProvider implements CharsetMetadataProvider | ||
{ | ||
/** @var Connection */ | ||
private $connection; | ||
|
||
/** @var bool */ | ||
private $useUtf8mb3; | ||
|
||
public function __construct(Connection $connection, bool $useUtf8mb3) | ||
{ | ||
$this->connection = $connection; | ||
$this->useUtf8mb3 = $useUtf8mb3; | ||
} | ||
|
||
public function normalizeCharset(string $charset): string | ||
{ | ||
if ($this->useUtf8mb3 && $charset === 'utf8') { | ||
return 'utf8mb3'; | ||
} | ||
|
||
if (! $this->useUtf8mb3 && $charset === 'utf8mb3') { | ||
return 'utf8'; | ||
} | ||
|
||
return $charset; | ||
} | ||
|
||
/** @throws Exception */ | ||
public function getDefaultCharsetCollation(string $charset): ?string | ||
{ | ||
$charset = $this->normalizeCharset($charset); | ||
|
||
$collation = $this->connection->fetchOne( | ||
<<<'SQL' | ||
SELECT DEFAULT_COLLATE_NAME | ||
FROM information_schema.CHARACTER_SETS | ||
WHERE CHARACTER_SET_NAME = ?; | ||
SQL | ||
, | ||
[$charset], | ||
); | ||
|
||
if ($collation !== false) { | ||
return $collation; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Driver; | ||
|
||
use Doctrine\DBAL\Schema\Column; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\TestUtil; | ||
use Doctrine\DBAL\Types\StringType; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
class DBAL6146Test extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (TestUtil::isDriverOneOf('pdo_mysql', 'mysqli')) { | ||
return; | ||
} | ||
|
||
self::markTestSkipped('This test requires the pdo_mysql or the mysqli driver.'); | ||
} | ||
|
||
/** @return iterable<array{array<string,string>,array<string,string>}> */ | ||
public static function equivalentCharsetAndCollationProvider(): iterable | ||
{ | ||
yield [[], []]; | ||
yield [['charset' => 'utf8'], ['charset' => 'utf8']]; | ||
yield [['charset' => 'utf8'], ['charset' => 'utf8mb3']]; | ||
yield [['charset' => 'utf8mb3'], ['charset' => 'utf8']]; | ||
yield [['charset' => 'utf8mb3'], ['charset' => 'utf8mb3']]; | ||
yield [['collation' => 'utf8_unicode_ci'], ['collation' => 'utf8_unicode_ci']]; | ||
yield [['collation' => 'utf8_unicode_ci'], ['collation' => 'utf8mb3_unicode_ci']]; | ||
yield [['collation' => 'utf8mb3_unicode_ci'], ['collation' => 'utf8mb3_unicode_ci']]; | ||
yield [['collation' => 'utf8mb3_unicode_ci'], ['collation' => 'utf8_unicode_ci']]; | ||
yield [ | ||
['charset' => 'utf8', 'collation' => 'utf8_unicode_ci'], | ||
['charset' => 'utf8', 'collation' => 'utf8_unicode_ci'], | ||
]; | ||
|
||
yield [ | ||
['charset' => 'utf8', 'collation' => 'utf8_unicode_ci'], | ||
['charset' => 'utf8mb3', 'collation' => 'utf8mb3_unicode_ci'], | ||
]; | ||
|
||
yield [ | ||
['charset' => 'utf8mb3', 'collation' => 'utf8mb3_unicode_ci'], | ||
['charset' => 'utf8', 'collation' => 'utf8_unicode_ci'], | ||
]; | ||
|
||
yield [ | ||
['charset' => 'utf8mb3', 'collation' => 'utf8mb3_unicode_ci'], | ||
['charset' => 'utf8mb3', 'collation' => 'utf8mb3_unicode_ci'], | ||
]; | ||
} | ||
|
||
/** | ||
* @param array<string,string> $options1 | ||
* @param array<string,string> $options2 | ||
* | ||
* @dataProvider equivalentCharsetAndCollationProvider | ||
*/ | ||
public function testThereAreNoRedundantAlterTableStatements(array $options1, array $options2): void | ||
{ | ||
$column1 = new Column('bar', new StringType(), ['length' => 32, 'platformOptions' => $options1]); | ||
$table1 = new Table('foo6146', [$column1]); | ||
|
||
$column2 = new Column('bar', new StringType(), ['length' => 32, 'platformOptions' => $options2]); | ||
$table2 = new Table('foo6146', [$column2]); | ||
|
||
$this->dropAndCreateTable($table1); | ||
|
||
$schemaManager = $this->connection->createSchemaManager(); | ||
$oldSchema = $schemaManager->introspectSchema(); | ||
$newSchema = new Schema([$table2]); | ||
$comparator = $schemaManager->createComparator(); | ||
$schemaDiff = $comparator->compareSchemas($oldSchema, $newSchema); | ||
$alteredTables = $schemaDiff->getAlteredTables(); | ||
|
||
self::assertEmpty($alteredTables); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.