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

Start adding binaryuuid support. #1734

Merged
merged 4 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Phinx/Db/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface AdapterInterface
public const PHINX_TYPE_DATE = 'date';
public const PHINX_TYPE_BINARY = 'binary';
public const PHINX_TYPE_VARBINARY = 'varbinary';
public const PHINX_TYPE_BINARYUUID = 'binaryuuid';
public const PHINX_TYPE_BLOB = 'blob';
public const PHINX_TYPE_BOOLEAN = 'boolean';
public const PHINX_TYPE_JSON = 'json';
Expand Down
7 changes: 7 additions & 0 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MysqlAdapter extends PdoAdapter
self::PHINX_TYPE_SET,
self::PHINX_TYPE_YEAR,
self::PHINX_TYPE_JSON,
self::PHINX_TYPE_BINARYUUID,
];

/**
Expand Down Expand Up @@ -930,6 +931,8 @@ public function getSqlType($type, $limit = null)
return ['name' => 'text'];
case static::PHINX_TYPE_BINARY:
return ['name' => 'binary', 'limit' => $limit ?: 255];
case static::PHINX_TYPE_BINARYUUID:
return ['name' => 'binary', 'limit' => 16];
case static::PHINX_TYPE_VARBINARY:
return ['name' => 'varbinary', 'limit' => $limit ?: 255];
case static::PHINX_TYPE_BLOB:
Expand Down Expand Up @@ -1104,6 +1107,10 @@ public function getPhinxType($sqlTypeDef)
$type = static::PHINX_TYPE_TEXT;
$limit = static::TEXT_LONG;
break;
case 'binary':
if ($limit === 16) {
$type = static::PHINX_TYPE_BINARYUUID;
}
}

try {
Expand Down
3 changes: 3 additions & 0 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PostgresAdapter extends PdoAdapter
self::PHINX_TYPE_INET,
self::PHINX_TYPE_MACADDR,
self::PHINX_TYPE_INTERVAL,
self::PHINX_TYPE_BINARYUUID,
];

/**
Expand Down Expand Up @@ -979,6 +980,8 @@ public function getSqlType($type, $limit = null)
return ['name' => 'real'];
case static::PHINX_TYPE_DATETIME:
return ['name' => 'timestamp'];
case static::PHINX_TYPE_BINARYUUID:
return ['name' => 'uuid'];
case static::PHINX_TYPE_BLOB:
case static::PHINX_TYPE_BINARY:
return ['name' => 'bytea'];
Expand Down
1 change: 1 addition & 0 deletions src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class SQLiteAdapter extends PdoAdapter
self::PHINX_TYPE_TEXT => 'text',
self::PHINX_TYPE_TIME => 'time_text',
self::PHINX_TYPE_UUID => 'uuid_text',
self::PHINX_TYPE_BINARYUUID => 'binary_blob',
Copy link
Member Author

Choose a reason for hiding this comment

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

That seems to be the problem/wrong. See https://github.com/cakephp/migrations/issues/660

Copy link
Member Author

Choose a reason for hiding this comment

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

uuid_blob is the correct one it seems

self::PHINX_TYPE_TIMESTAMP => 'timestamp_text',
self::PHINX_TYPE_VARBINARY => 'varbinary_blob',
];
Expand Down
1 change: 1 addition & 0 deletions src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Column
public const TIME = AdapterInterface::PHINX_TYPE_TIME;
public const TIMESTAMP = AdapterInterface::PHINX_TYPE_TIMESTAMP;
public const UUID = AdapterInterface::PHINX_TYPE_UUID;
public const BINARYUUID = AdapterInterface::PHINX_TYPE_BINARYUUID;
/** MySQL-only column type */
public const ENUM = AdapterInterface::PHINX_TYPE_ENUM;
/** MySQL-only column type */
Expand Down
34 changes: 34 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,40 @@ public function testCreateTableWithMultiplePrimaryKeys()
$this->assertFalse($this->adapter->hasIndex('table1', ['tag_id', 'user_email']));
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsUuid()
{
$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'uuid')->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'));
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsBinaryUuid()
{
$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'binaryuuid')->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 \Phinx\Db\Table('table1', [], $this->adapter);
Expand Down
34 changes: 34 additions & 0 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,40 @@ public function testCreateTableWithMultiplePrimaryKeysWithSchema()
$this->adapter->dropSchema('schema1');
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsUuid()
{
$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'uuid')->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'));
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsBinaryUuid()
{
$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'binaryuuid')->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 \Phinx\Db\Table('table1', [], $this->adapter);
Expand Down
31 changes: 31 additions & 0 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ public function testCreateTableWithMultiplePrimaryKeys()
$this->assertFalse($this->adapter->hasIndex('table1', ['tag_id', 'user_email']));
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsUuid()
{
$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'uuid')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
}

/**
* @return void
*/
public function testCreateTableWithPrimaryKeyAsBinaryUuid()
{
$options = [
'id' => false,
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'binaryuuid')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
}

public function testCreateTableWithMultipleIndexes()
{
$table = new \Phinx\Db\Table('table1', [], $this->adapter);
Expand Down Expand Up @@ -1649,6 +1679,7 @@ public function testGetColumnTypes()
SQLiteAdapter::PHINX_TYPE_TEXT,
SQLiteAdapter::PHINX_TYPE_TIME,
SQLiteAdapter::PHINX_TYPE_UUID,
SQLiteAdapter::PHINX_TYPE_BINARYUUID,
SQLiteAdapter::PHINX_TYPE_TIMESTAMP,
SQLiteAdapter::PHINX_TYPE_VARBINARY,
];
Expand Down