diff --git a/src/Phinx/Db/Adapter/AdapterInterface.php b/src/Phinx/Db/Adapter/AdapterInterface.php index b4e5d944c..fc7359994 100644 --- a/src/Phinx/Db/Adapter/AdapterInterface.php +++ b/src/Phinx/Db/Adapter/AdapterInterface.php @@ -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'; diff --git a/src/Phinx/Db/Adapter/MysqlAdapter.php b/src/Phinx/Db/Adapter/MysqlAdapter.php index 4d1e168d4..79d0e61f2 100644 --- a/src/Phinx/Db/Adapter/MysqlAdapter.php +++ b/src/Phinx/Db/Adapter/MysqlAdapter.php @@ -34,6 +34,7 @@ class MysqlAdapter extends PdoAdapter self::PHINX_TYPE_SET, self::PHINX_TYPE_YEAR, self::PHINX_TYPE_JSON, + self::PHINX_TYPE_BINARYUUID, ]; /** @@ -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: @@ -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 { diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index 1df30c5d0..a76abfb8e 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -32,6 +32,7 @@ class PostgresAdapter extends PdoAdapter self::PHINX_TYPE_INET, self::PHINX_TYPE_MACADDR, self::PHINX_TYPE_INTERVAL, + self::PHINX_TYPE_BINARYUUID, ]; /** @@ -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']; diff --git a/src/Phinx/Db/Adapter/SQLiteAdapter.php b/src/Phinx/Db/Adapter/SQLiteAdapter.php index 44431cb86..a3fc230e7 100644 --- a/src/Phinx/Db/Adapter/SQLiteAdapter.php +++ b/src/Phinx/Db/Adapter/SQLiteAdapter.php @@ -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', ]; diff --git a/src/Phinx/Db/Table/Column.php b/src/Phinx/Db/Table/Column.php index bbcf14fab..bd4d6ab5a 100644 --- a/src/Phinx/Db/Table/Column.php +++ b/src/Phinx/Db/Table/Column.php @@ -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 */ diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index fabc3be53..6ddd6341e 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -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); diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 54217c511..03a7a3e45 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -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); diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index dcdfa2c9a..20be247df 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -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); @@ -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, ];