-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Added native UUID field type for MariaDB1070+ #6316
base: 4.1.x
Are you sure you want to change the base?
Changes from all commits
40b17c0
3db8b85
758f2ce
9150748
4abc8b4
5f15c7e
004a287
88b92a2
8956910
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Platforms; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
|
||
/** | ||
* Provides the behavior, features and SQL dialect of the MariaDB 10.7 (10.7.0 GA) database platform. | ||
*/ | ||
class MariaDB1070Platform extends MariaDB1060Platform | ||
{ | ||
protected function initializeDoctrineTypeMappings(): void | ||
{ | ||
parent::initializeDoctrineTypeMappings(); | ||
|
||
$this->doctrineTypeMapping['uuid'] = Types::GUID; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getGuidTypeDeclarationSQL(array $column): string | ||
{ | ||
return 'UUID'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Platform; | ||
|
||
use Doctrine\DBAL\Platforms\MariaDB1070Platform; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\GuidType; | ||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
use function sprintf; | ||
|
||
class AlterUuidColumnTest extends FunctionalTestCase | ||
{ | ||
public const DEFAULT_UUID4 = '85626c2f-2f63-4120-b814-ecc085eaaba0'; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if ($this->connection->getDatabasePlatform() instanceof MariaDB1070Platform) { | ||
return; | ||
} | ||
|
||
self::markTestSkipped('This test requires MariDB 10.7 or newer'); | ||
} | ||
|
||
public function testAlterToUuidColumn(): void | ||
{ | ||
$table = static::prepareLegacyUuidTable('simple_uuid_table', 'id'); | ||
$table->setPrimaryKey(['id']); | ||
$this->dropAndCreateTable($table); | ||
|
||
$this->connection->executeStatement( | ||
sprintf( | ||
'INSERT INTO simple_uuid_table VALUES ("%s")', | ||
static::DEFAULT_UUID4, | ||
), | ||
); | ||
|
||
$table->getColumn('id') | ||
->setType(Type::getType(Types::GUID)); | ||
|
||
$sm = $this->connection->createSchemaManager(); | ||
$diff = $sm->createComparator() | ||
->compareTables($sm->introspectTable('simple_uuid_table'), $table); | ||
|
||
$sm->alterTable($diff); | ||
|
||
$table = $sm->introspectTable('simple_uuid_table'); | ||
|
||
self::assertInstanceOf(GuidType::class, $table->getColumn('id')->getType()); | ||
|
||
// Verify data integrity | ||
$resultUuid = $this->connection | ||
->executeQuery('SELECT id from simple_uuid_table') | ||
->fetchOne(); | ||
|
||
static::assertEquals(static::DEFAULT_UUID4, $resultUuid); | ||
} | ||
|
||
public function testAlterUuidInForeignRelation(): void | ||
{ | ||
$sm = $this->connection->createSchemaManager(); | ||
|
||
$parentTable = static::prepareLegacyUuidTable('parent_uuid_table', 'id'); | ||
$parentTable->setPrimaryKey(['id']); | ||
$this->dropAndCreateTable($parentTable); | ||
|
||
$this->connection->executeStatement( | ||
sprintf( | ||
'INSERT INTO parent_uuid_table VALUES ("%s")', | ||
static::DEFAULT_UUID4, | ||
), | ||
); | ||
|
||
$childTable = static::prepareLegacyUuidTable('child_uuid_table', 'parent_uuid_id'); | ||
$childTable->addColumn('id', Types::INTEGER)->setAutoincrement(true); | ||
$childTable->setPrimaryKey(['id']); | ||
|
||
$childTable = static::prepareForeignKey($childTable, 'parent_uuid'); | ||
$this->dropAndCreateTable($childTable); | ||
|
||
$this->connection->executeStatement( | ||
sprintf( | ||
'INSERT INTO child_uuid_table (`parent_uuid_id`) VALUES ("%s")', | ||
static::DEFAULT_UUID4, | ||
), | ||
); | ||
|
||
$childTable->removeForeignKey('fk_parent_uuid_id'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Temporarily removing the foreign key is kind of what I would expect the comparator to do, tbh. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remove the foreign key the test will fail. The error received is: Is it intended? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. This is precisely what needs to be fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the test so that it must fail. I will try to allocate some time during next week in order to see how can I fix this issue. Any insight is welcome. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some research I figure out that in order to automatically to drop and create the foreign keys I will need that introspectTable and hereby the Table class will contains information about which tables and and foreign keys are affected. I also tried another approach that seems to work when I run in the MySQL console and it's using compound statements (MariaDB support it), however the "executeStatement" doesn't support this type of statements. Do you have any insight that can help? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid compound statements and I don't think we need them. |
||
|
||
$diff = $sm->createComparator() | ||
->compareTables($sm->introspectTable('child_uuid_table'), $childTable); | ||
|
||
$sm->alterTable($diff); | ||
|
||
$parentTable->getColumn('id') | ||
->setType(Type::getType(Types::GUID)); | ||
|
||
$diff = $sm->createComparator() | ||
->compareTables($sm->introspectTable('parent_uuid_table'), $parentTable); | ||
|
||
$sm->alterTable($diff); | ||
|
||
$childTable->getColumn('parent_uuid_id') | ||
->setType(Type::getType(Types::GUID)); | ||
|
||
$childTable = static::prepareForeignKey($childTable, 'parent_uuid'); | ||
|
||
$diff = $sm->createComparator() | ||
->compareTables($sm->introspectTable('child_uuid_table'), $childTable); | ||
|
||
$sm->alterTable($diff); | ||
|
||
self::assertInstanceOf(GuidType::class, $parentTable->getColumn('id')->getType()); | ||
self::assertInstanceOf(GuidType::class, $childTable->getColumn('parent_uuid_id')->getType()); | ||
|
||
// Verify data integrity | ||
$resultUuid = $this->connection | ||
->executeQuery('SELECT id from parent_uuid_table') | ||
->fetchOne(); | ||
|
||
static::assertEquals(static::DEFAULT_UUID4, $resultUuid); | ||
|
||
$resultUuid = $this->connection | ||
->executeQuery('SELECT parent_uuid_id from child_uuid_table') | ||
->fetchOne(); | ||
|
||
static::assertEquals(static::DEFAULT_UUID4, $resultUuid); | ||
} | ||
|
||
/** | ||
* Create legacy Uuid table. | ||
*/ | ||
protected function prepareLegacyUuidTable(string $tableName, string $uuidField): Table | ||
{ | ||
$this->connection | ||
->executeStatement( | ||
sprintf( | ||
'CREATE TABLE `%s` (`%s` CHAR(36) NOT NULL)', | ||
$tableName, | ||
$uuidField, | ||
), | ||
); | ||
|
||
return $this->connection | ||
->createSchemaManager() | ||
->introspectTable($tableName); | ||
} | ||
|
||
protected static function prepareForeignKey(Table $table, string $foreign): Table | ||
{ | ||
$table->addForeignKeyConstraint( | ||
$foreign . '_table', | ||
[$foreign . '_id'], | ||
['id'], | ||
[], | ||
'fk_' . $foreign . '_id', | ||
); | ||
|
||
return $table; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Platforms; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Platforms\MariaDB1070Platform; | ||
|
||
class MariaDB1070PlatformTest extends MariaDB1052PlatformTest | ||
{ | ||
public function createPlatform(): AbstractPlatform | ||
{ | ||
return new MariaDB1070Platform(); | ||
} | ||
|
||
public function testReturnsGuidTypeDeclarationSQL(): void | ||
{ | ||
self::assertSame('UUID', $this->platform->getGuidTypeDeclarationSQL([])); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want to limit this test to MariaDB 10.7 and higher because we're specifically testing the migration from old table definitions on that platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, other platforms different than MariaDB 10.7 are skipped.