Skip to content

Commit

Permalink
Merge pull request #1734 from cakephp/binaryuuid
Browse files Browse the repository at this point in the history
Start adding binaryuuid support.
  • Loading branch information
dereuromark committed Apr 11, 2020
2 parents fbe74fb + 0e953b5 commit c1b7145
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
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',
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

0 comments on commit c1b7145

Please sign in to comment.