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

Added native UUID field type for MariaDB1070+ #6316

Open
wants to merge 9 commits into
base: 4.1.x
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\MariaDB1052Platform;
use Doctrine\DBAL\Platforms\MariaDB1060Platform;
use Doctrine\DBAL\Platforms\MariaDB1070Platform;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
Expand All @@ -35,6 +36,10 @@ public function getDatabasePlatform(ServerVersionProvider $versionProvider): Abs
$version = $versionProvider->getServerVersion();
if (stripos($version, 'mariadb') !== false) {
$mariaDbVersion = $this->getMariaDbMysqlVersionNumber($version);
if (version_compare($mariaDbVersion, '10.7.0', '>=')) {
return new MariaDB1070Platform();
}

if (version_compare($mariaDbVersion, '10.6.0', '>=')) {
return new MariaDB1060Platform();
}
Expand Down
28 changes: 28 additions & 0 deletions src/Platforms/MariaDB1070Platform.php
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';
}
}
5 changes: 4 additions & 1 deletion tests/Driver/VersionAwarePlatformDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDB1052Platform;
use Doctrine\DBAL\Platforms\MariaDB1060Platform;
use Doctrine\DBAL\Platforms\MariaDB1070Platform;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
Expand Down Expand Up @@ -41,7 +42,9 @@ public static function mySQLVersionProvider(): array
['10.2.8-MariaDB-1~lenny-log', MariaDBPlatform::class],
['10.5.2-MariaDB-1~lenny-log', MariaDB1052Platform::class],
['10.6.0-MariaDB-1~lenny-log', MariaDB1060Platform::class],
['11.0.2-MariaDB-1:11.0.2+maria~ubu2204', MariaDB1060Platform::class],
['10.7.0-MariaDB-1~lenny-log', MariaDB1070Platform::class],
['10.11.7-MariaDB-1:10.11.7+maria~ubu2204', MariaDB1070Platform::class],
['11.0.2-MariaDB-1:11.0.2+maria~ubu2204', MariaDB1070Platform::class],
];
}

Expand Down
167 changes: 167 additions & 0 deletions tests/Functional/Platform/AlterUuidColumnTest.php
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
Copy link
Member

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.

Copy link
Author

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.

{
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');
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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:
"General error: 1833 Cannot change column 'id': used in a foreign key constraint 'fk_parent_uuid_id' of table 'doctrine_tests.child_uuid_table'"

Is it intended?

Copy link
Member

Choose a reason for hiding this comment

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

No. This is precisely what needs to be fixed.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
18 changes: 18 additions & 0 deletions tests/Functional/Schema/MySQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use Doctrine\DBAL\Exception\DatabaseRequired;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDB1070Platform;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\Functional\Schema\MySQL\CustomType;
use Doctrine\DBAL\Tests\Functional\Schema\MySQL\PointType;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Types\JsonType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -571,6 +573,22 @@ public function testColumnIntrospection(): void
self::assertTrue($diff->isEmpty(), 'Tables should be identical.');
}

public function testIntrospectUuidColumn(): void
{
if (! $this->connection->getDatabasePlatform() instanceof MariaDB1070Platform) {
self::markTestSkipped('Only relevant for MariaDB 10.7 and newer.');
}

$this->connection->executeStatement('DROP TABLE IF EXISTS test_uuid_column_introspection');
$this->connection->executeStatement(
'CREATE TABLE test_uuid_column_introspection (col_uuid UUID NOT NULL PRIMARY KEY)',
);

$onlineTable = $this->schemaManager->introspectTable('test_uuid_column_introspection');

self::assertInstanceOf(GuidType::class, $onlineTable->getColumn('col_uuid')->getType());
}

public function testListTableColumnsThrowsDatabaseRequired(): void
{
$params = TestUtil::getConnectionParams();
Expand Down
21 changes: 21 additions & 0 deletions tests/Platforms/MariaDB1070PlatformTest.php
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([]));
}
}