Skip to content

Commit

Permalink
Enable the nativeuuid type conditionally.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosp committed Dec 10, 2024
1 parent 5506ee0 commit 264332f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class MysqlAdapter extends PdoAdapter
self::PHINX_TYPE_YEAR,
self::PHINX_TYPE_JSON,
self::PHINX_TYPE_BINARYUUID,
self::PHINX_TYPE_NATIVEUUID,
self::PHINX_TYPE_TINYBLOB,
self::PHINX_TYPE_MEDIUMBLOB,
self::PHINX_TYPE_LONGBLOB,
Expand Down Expand Up @@ -1494,6 +1493,15 @@ public function describeTable(string $tableName): array
*/
public function getColumnTypes(): array
{
return array_merge(parent::getColumnTypes(), static::$specificColumnTypes);
$connection = $this->getConnection();
$version = $connection->getDriver()->version();

$types = array_merge(parent::getColumnTypes(), static::$specificColumnTypes);

if (version_compare($version, '10.7', '>=')) {
$types[] = self::PHINX_TYPE_NATIVEUUID;
}

return $types;
}
}
37 changes: 37 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use InvalidArgumentException;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Db\Adapter\MysqlAdapter;
use Migrations\Db\Adapter\UnsupportedColumnTypeException;
use Migrations\Db\Literal;
use Migrations\Db\Table;
use Migrations\Db\Table\Column;
Expand Down Expand Up @@ -82,6 +83,13 @@ private function usingMysql8(): bool
return version_compare($version, '8.0.0', '>=');
}

private function usingMariaDbWithUuid(): bool
{
$version = $this->adapter->getConnection()->getDriver()->version();

return version_compare($version, '10.7.0', '>=');
}

public function testConnection()
{
$this->assertInstanceOf(Connection::class, $this->adapter->getConnection());
Expand Down Expand Up @@ -329,6 +337,27 @@ public function testCreateTableWithPrimaryKeyAsBinaryUuid()
$this->assertTrue($this->adapter->hasColumn('ztable', 'user_id'));
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsNativeUuid()
{
if (!$this->usingMariaDbWithUuid()) {
$this->markTestSkipped('Database does not have a native uuid type');
}

$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'nativeuuid', ['null' => false])->save();
$table->addColumn('user_id', 'integer')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
$this->assertTrue($this->adapter->hasColumn('ztable', 'user_id'));
}

public function testCreateTableWithMultipleIndexes()
{
$table = new Table('table1', [], $this->adapter);
Expand Down Expand Up @@ -2522,4 +2551,12 @@ public function testGetPhinxTypeFromSQLDefinition(string $sqlDefinition, array $
$this->assertSame($expectedResponse['name'], $result['name'], "Type mismatch - got '{$result['name']}' when expecting '{$expectedResponse['name']}'");
$this->assertSame($expectedResponse['limit'], $result['limit'], "Field upper boundary mismatch - got '{$result['limit']}' when expecting '{$expectedResponse['limit']}'");
}

public function testGetPhinxType()
{
if (!$this->usingMariaDbWithUuid()) {
$this->expectException(UnsupportedColumnTypeException::class);
}
$this->assertSame('uuid', $this->adapter->getSqlType('nativeuuid'));
}
}

0 comments on commit 264332f

Please sign in to comment.