diff --git a/src/Db/Adapter/AdapterInterface.php b/src/Db/Adapter/AdapterInterface.php index de7025c0..c4cc1d54 100644 --- a/src/Db/Adapter/AdapterInterface.php +++ b/src/Db/Adapter/AdapterInterface.php @@ -15,7 +15,6 @@ use Cake\Database\Query\InsertQuery; use Cake\Database\Query\SelectQuery; use Cake\Database\Query\UpdateQuery; -use Migrations\Db\Literal; use Migrations\Db\Table\Column; use Migrations\Db\Table\Table; use Migrations\MigrationInterface; @@ -446,15 +445,6 @@ public function getColumnTypes(): array; */ public function isValidColumnType(Column $column): bool; - /** - * Converts the Phinx logical type to the adapter's SQL type. - * - * @param \Migrations\Db\Literal|string $type Type - * @param int|null $limit Limit - * @return array - */ - public function getSqlType(Literal|string $type, ?int $limit = null): array; - /** * Creates a new database. * diff --git a/src/Db/Adapter/AdapterWrapper.php b/src/Db/Adapter/AdapterWrapper.php index 7fe5ba63..1e065619 100644 --- a/src/Db/Adapter/AdapterWrapper.php +++ b/src/Db/Adapter/AdapterWrapper.php @@ -15,7 +15,6 @@ use Cake\Database\Query\InsertQuery; use Cake\Database\Query\SelectQuery; use Cake\Database\Query\UpdateQuery; -use Migrations\Db\Literal; use Migrations\Db\Table\Column; use Migrations\Db\Table\Table; use Migrations\MigrationInterface; @@ -365,14 +364,6 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint = return $this->getAdapter()->hasForeignKey($tableName, $columns, $constraint); } - /** - * @inheritDoc - */ - public function getSqlType(Literal|string $type, ?int $limit = null): array - { - return $this->getAdapter()->getSqlType($type, $limit); - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/MysqlAdapter.php b/src/Db/Adapter/MysqlAdapter.php index e03e4ea2..54156672 100644 --- a/src/Db/Adapter/MysqlAdapter.php +++ b/src/Db/Adapter/MysqlAdapter.php @@ -14,7 +14,6 @@ use Cake\Database\Schema\TableSchema; use InvalidArgumentException; use Migrations\Db\AlterInstructions; -use Migrations\Db\Literal; use Migrations\Db\Table\Column; use Migrations\Db\Table\ForeignKey; use Migrations\Db\Table\Index; @@ -59,7 +58,7 @@ class MysqlAdapter extends AbstractAdapter // except for the `_LONG` and `_BIG` variants, which are maxed at 32-bit // PHP_INT_MAX value. The `INT_REGULAR` field is just arbitrarily half of INT_BIG // as its actual value is its regular value is larger than PHP_INT_MAX. We do this - // to keep consistent the type hints for getSqlType and Column::$limit being integers. + // to keep consistent the type hints for Column::$limit being integers. public const TEXT_TINY = 255; public const TEXT_SMALL = 255; /* deprecated, alias of TEXT_TINY */ /** @deprecated Use length of null instead **/ @@ -875,186 +874,6 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr return $instructions; } - /** - * {@inheritDoc} - * - * @throws \Migrations\Db\Adapter\UnsupportedColumnTypeException - */ - public function getSqlType(Literal|string $type, ?int $limit = null): array - { - $type = (string)$type; - switch ($type) { - case static::PHINX_TYPE_FLOAT: - case static::PHINX_TYPE_DOUBLE: - case static::PHINX_TYPE_DECIMAL: - case static::PHINX_TYPE_DATE: - case static::PHINX_TYPE_ENUM: - case static::PHINX_TYPE_SET: - case static::PHINX_TYPE_JSON: - // Geospatial database types - case static::PHINX_TYPE_GEOMETRY: - case static::PHINX_TYPE_POINT: - case static::PHINX_TYPE_LINESTRING: - case static::PHINX_TYPE_POLYGON: - return ['name' => $type]; - case static::PHINX_TYPE_DATETIME: - case static::PHINX_TYPE_TIMESTAMP: - case static::PHINX_TYPE_TIME: - return ['name' => $type, 'limit' => $limit]; - case static::PHINX_TYPE_STRING: - return ['name' => 'varchar', 'limit' => $limit ?: 255]; - case static::PHINX_TYPE_CHAR: - return ['name' => 'char', 'limit' => $limit ?: 255]; - case static::PHINX_TYPE_TEXT: - if ($limit) { - $sizes = [ - // Order matters! Size must always be tested from longest to shortest! - 'longtext' => static::TEXT_LONG, - 'mediumtext' => static::TEXT_MEDIUM, - 'text' => static::TEXT_REGULAR, - 'tinytext' => static::TEXT_SMALL, - ]; - foreach ($sizes as $name => $length) { - if ($limit >= $length) { - return ['name' => $name]; - } - } - } - - return ['name' => 'text']; - case static::PHINX_TYPE_BINARY: - if ($limit === null) { - $limit = 255; - } - - if ($limit > 255) { - return $this->getSqlType(static::PHINX_TYPE_BLOB, $limit); - } - - return ['name' => 'binary', 'limit' => $limit]; - case static::PHINX_TYPE_BINARYUUID: - return ['name' => 'binary', 'limit' => 16]; - case static::PHINX_TYPE_VARBINARY: - if ($limit === null) { - $limit = 255; - } - - if ($limit > 255) { - return $this->getSqlType(static::PHINX_TYPE_BLOB, $limit); - } - - return ['name' => 'varbinary', 'limit' => $limit]; - case static::PHINX_TYPE_BLOB: - if ($limit !== null) { - // Rework this part as the chosen types were always UNDER the required length - $sizes = [ - 'tinyblob' => static::BLOB_SMALL, - 'blob' => static::BLOB_REGULAR, - 'mediumblob' => static::BLOB_MEDIUM, - ]; - - foreach ($sizes as $name => $length) { - if ($limit <= $length) { - return ['name' => $name]; - } - } - - // For more length requirement, the longblob is used - return ['name' => 'longblob']; - } - - // If not limit is provided, fallback on blob - return ['name' => 'blob']; - case static::PHINX_TYPE_TINYBLOB: - // Automatically reprocess blob type to ensure that correct blob subtype is selected given provided limit - return $this->getSqlType(static::PHINX_TYPE_BLOB, $limit ?: static::BLOB_TINY); - case static::PHINX_TYPE_MEDIUMBLOB: - // Automatically reprocess blob type to ensure that correct blob subtype is selected given provided limit - return $this->getSqlType(static::PHINX_TYPE_BLOB, $limit ?: static::BLOB_MEDIUM); - case static::PHINX_TYPE_LONGBLOB: - // Automatically reprocess blob type to ensure that correct blob subtype is selected given provided limit - return $this->getSqlType(static::PHINX_TYPE_BLOB, $limit ?: static::BLOB_LONG); - case static::PHINX_TYPE_BIT: - return ['name' => 'bit', 'limit' => $limit ?: 64]; - case static::PHINX_TYPE_BIG_INTEGER: - if ($limit === static::INT_BIG) { - $limit = static::INT_DISPLAY_BIG; - } - - return ['name' => 'bigint', 'limit' => $limit ?: 20]; - case static::PHINX_TYPE_MEDIUM_INTEGER: - if ($limit === static::INT_MEDIUM) { - $limit = static::INT_DISPLAY_MEDIUM; - } - - return ['name' => 'mediumint', 'limit' => $limit ?: 8]; - case static::PHINX_TYPE_SMALL_INTEGER: - if ($limit === static::INT_SMALL) { - $limit = static::INT_DISPLAY_SMALL; - } - - return ['name' => 'smallint', 'limit' => $limit ?: 6]; - case static::PHINX_TYPE_TINY_INTEGER: - if ($limit === static::INT_TINY) { - $limit = static::INT_DISPLAY_TINY; - } - - return ['name' => 'tinyint', 'limit' => $limit ?: 4]; - case static::PHINX_TYPE_INTEGER: - if ($limit && $limit >= static::INT_TINY) { - $sizes = [ - // Order matters! Size must always be tested from longest to shortest! - 'bigint' => static::INT_BIG, - 'int' => static::INT_REGULAR, - 'mediumint' => static::INT_MEDIUM, - 'smallint' => static::INT_SMALL, - 'tinyint' => static::INT_TINY, - ]; - $limits = [ - 'tinyint' => static::INT_DISPLAY_TINY, - 'smallint' => static::INT_DISPLAY_SMALL, - 'mediumint' => static::INT_DISPLAY_MEDIUM, - 'int' => static::INT_DISPLAY_REGULAR, - 'bigint' => static::INT_DISPLAY_BIG, - ]; - foreach ($sizes as $name => $length) { - if ($limit >= $length) { - $def = ['name' => $name]; - if (isset($limits[$name])) { - $def['limit'] = $limits[$name]; - } - - return $def; - } - } - } elseif (!$limit) { - $limit = static::INT_DISPLAY_REGULAR; - } - - return ['name' => 'int', 'limit' => $limit]; - case static::PHINX_TYPE_BOOLEAN: - return ['name' => 'tinyint', 'limit' => 1]; - case static::PHINX_TYPE_UUID: - return ['name' => 'char', 'limit' => 36]; - case static::PHINX_TYPE_NATIVEUUID: - if (!$this->hasNativeUuid()) { - throw new UnsupportedColumnTypeException( - 'Column type "' . $type . '" is not supported by this version of MySQL.', - ); - } - - return ['name' => 'uuid']; - case static::PHINX_TYPE_YEAR: - if (!$limit || in_array($limit, [2, 4])) { - $limit = 4; - } - - return ['name' => 'year', 'limit' => $limit]; - default: - throw new UnsupportedColumnTypeException('Column type "' . $type . '" is not supported by MySQL.'); - } - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index 0cb16f66..14793cb4 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -819,75 +819,6 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr return $instructions; } - /** - * {@inheritDoc} - * - * @throws \Migrations\Db\Adapter\UnsupportedColumnTypeException - */ - public function getSqlType(Literal|string $type, ?int $limit = null): array - { - $type = (string)$type; - switch ($type) { - case static::PHINX_TYPE_TEXT: - case static::PHINX_TYPE_TIME: - case static::PHINX_TYPE_DATE: - case static::PHINX_TYPE_BOOLEAN: - case static::PHINX_TYPE_JSON: - case static::PHINX_TYPE_JSONB: - case static::PHINX_TYPE_UUID: - case static::PHINX_TYPE_CIDR: - case static::PHINX_TYPE_INET: - case static::PHINX_TYPE_MACADDR: - case static::PHINX_TYPE_TIMESTAMP: - case static::PHINX_TYPE_INTEGER: - return ['name' => $type]; - case static::PHINX_TYPE_TINY_INTEGER: - return ['name' => 'smallint']; - case static::PHINX_TYPE_SMALL_INTEGER: - return ['name' => 'smallint']; - case static::PHINX_TYPE_DECIMAL: - return ['name' => $type, 'precision' => 18, 'scale' => 0]; - case static::PHINX_TYPE_DOUBLE: - return ['name' => 'double precision']; - case static::PHINX_TYPE_STRING: - return ['name' => 'character varying', 'limit' => 255]; - case static::PHINX_TYPE_CHAR: - return ['name' => 'character', 'limit' => 255]; - case static::PHINX_TYPE_BIG_INTEGER: - return ['name' => 'bigint']; - case static::PHINX_TYPE_FLOAT: - return ['name' => 'real']; - case static::PHINX_TYPE_DATETIME: - return ['name' => 'timestamp']; - case static::PHINX_TYPE_BINARYUUID: - case static::PHINX_TYPE_NATIVEUUID: - return ['name' => 'uuid']; - case static::PHINX_TYPE_BLOB: - case static::PHINX_TYPE_BINARY: - return ['name' => 'bytea']; - case static::PHINX_TYPE_INTERVAL: - return ['name' => 'interval']; - // Geospatial database types - // Spatial storage in Postgres is done via the PostGIS extension, - // which enables the use of the "geography" type in combination - // with SRID 4326. - case static::PHINX_TYPE_GEOMETRY: - return ['name' => 'geography', 'type' => 'geometry', 'srid' => 4326]; - case static::PHINX_TYPE_POINT: - return ['name' => 'geography', 'type' => 'point', 'srid' => 4326]; - case static::PHINX_TYPE_LINESTRING: - return ['name' => 'geography', 'type' => 'linestring', 'srid' => 4326]; - case static::PHINX_TYPE_POLYGON: - return ['name' => 'geography', 'type' => 'polygon', 'srid' => 4326]; - default: - if ($this->isArrayType($type)) { - return ['name' => $type]; - } - // Return array type - throw new UnsupportedColumnTypeException('Column type `' . $type . '` is not supported by Postgresql.'); - } - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/SqliteAdapter.php b/src/Db/Adapter/SqliteAdapter.php index 012e0f2e..f0c52592 100644 --- a/src/Db/Adapter/SqliteAdapter.php +++ b/src/Db/Adapter/SqliteAdapter.php @@ -1627,30 +1627,6 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr return $this->endAlterByCopyTable($instructions, $tableName); } - /** - * {@inheritDoc} - * - * @throws \Migrations\Db\Adapter\UnsupportedColumnTypeException - */ - public function getSqlType(Literal|string $type, ?int $limit = null): array - { - if ($type instanceof Literal) { - $name = $type; - } else { - $typeLC = strtolower($type); - - if (isset(static::$supportedColumnTypes[$typeLC])) { - $name = static::$supportedColumnTypes[$typeLC]; - } elseif (in_array($typeLC, static::$unsupportedColumnTypes, true)) { - throw new UnsupportedColumnTypeException('Column type "' . $type . '" is not supported by SQLite.'); - } else { - throw new UnsupportedColumnTypeException('Column type "' . $type . '" is not known by SQLite.'); - } - } - - return ['name' => $name, 'limit' => $limit]; - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/SqlserverAdapter.php b/src/Db/Adapter/SqlserverAdapter.php index 5bb98df6..5a399d2e 100644 --- a/src/Db/Adapter/SqlserverAdapter.php +++ b/src/Db/Adapter/SqlserverAdapter.php @@ -848,64 +848,6 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr return $instructions; } - /** - * {@inheritDoc} - * - * @throws \Migrations\Db\Adapter\UnsupportedColumnTypeException - */ - public function getSqlType(Literal|string $type, ?int $limit = null): array - { - $type = (string)$type; - switch ($type) { - case static::PHINX_TYPE_FLOAT: - case static::PHINX_TYPE_DECIMAL: - case static::PHINX_TYPE_DATETIME: - case static::PHINX_TYPE_TIME: - case static::PHINX_TYPE_DATE: - return ['name' => $type]; - case static::PHINX_TYPE_STRING: - return ['name' => 'nvarchar', 'limit' => 255]; - case static::PHINX_TYPE_CHAR: - return ['name' => 'nchar', 'limit' => 255]; - case static::PHINX_TYPE_TEXT: - return ['name' => 'ntext']; - case static::PHINX_TYPE_INTEGER: - return ['name' => 'int']; - case static::PHINX_TYPE_TINY_INTEGER: - return ['name' => 'tinyint']; - case static::PHINX_TYPE_SMALL_INTEGER: - return ['name' => 'smallint']; - case static::PHINX_TYPE_BIG_INTEGER: - return ['name' => 'bigint']; - case static::PHINX_TYPE_TIMESTAMP: - return ['name' => 'datetime']; - case static::PHINX_TYPE_BLOB: - case static::PHINX_TYPE_BINARY: - return ['name' => 'varbinary']; - case static::PHINX_TYPE_BOOLEAN: - return ['name' => 'bit']; - case static::PHINX_TYPE_BINARYUUID: - case static::PHINX_TYPE_UUID: - case static::PHINX_TYPE_NATIVEUUID: - return ['name' => 'uniqueidentifier']; - case static::PHINX_TYPE_FILESTREAM: - return ['name' => 'varbinary', 'limit' => 'max']; - // Geospatial database types - case static::PHINX_TYPE_GEOGRAPHY: - case static::PHINX_TYPE_POINT: - case static::PHINX_TYPE_LINESTRING: - case static::PHINX_TYPE_POLYGON: - // SQL Server stores all spatial data using a single data type. - // Specific types (point, polygon, etc) are set at insert time. - return ['name' => 'geography']; - // Geometry specific type - case static::PHINX_TYPE_GEOMETRY: - return ['name' => 'geometry']; - default: - throw new UnsupportedColumnTypeException('Column type "' . $type . '" is not supported by SqlServer.'); - } - } - /** * @inheritDoc */ diff --git a/tests/TestCase/Db/Adapter/DefaultAdapterTrait.php b/tests/TestCase/Db/Adapter/DefaultAdapterTrait.php index e62826a9..b353f4bc 100644 --- a/tests/TestCase/Db/Adapter/DefaultAdapterTrait.php +++ b/tests/TestCase/Db/Adapter/DefaultAdapterTrait.php @@ -4,7 +4,6 @@ namespace Migrations\Test\TestCase\Db\Adapter; use Migrations\Db\AlterInstructions; -use Migrations\Db\Literal; use Migrations\Db\Table\Column; use Migrations\Db\Table\ForeignKey; use Migrations\Db\Table\Index; @@ -82,11 +81,6 @@ public function hasForeignKey(string $tableName, array|string $columns, ?string return false; } - public function getSqlType(Literal|string $type, ?int $limit = null): array - { - return []; - } - public function createDatabase(string $name, array $options = []): void { } diff --git a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php index 7586225f..f5739c80 100644 --- a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php +++ b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php @@ -12,7 +12,6 @@ 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; @@ -965,258 +964,6 @@ public function testChangeColumnDefaultToNull() $this->assertNull($rows[1]['Default']); } - public static function sqlTypeIntConversionProvider() - { - return [ - // tinyint - [AdapterInterface::PHINX_TYPE_TINY_INTEGER, null, 'tinyint', 4], - [AdapterInterface::PHINX_TYPE_TINY_INTEGER, 2, 'tinyint', 2], - [AdapterInterface::PHINX_TYPE_TINY_INTEGER, MysqlAdapter::INT_TINY, 'tinyint', 4], - // smallint - [AdapterInterface::PHINX_TYPE_SMALL_INTEGER, null, 'smallint', 6], - [AdapterInterface::PHINX_TYPE_SMALL_INTEGER, 3, 'smallint', 3], - [AdapterInterface::PHINX_TYPE_SMALL_INTEGER, MysqlAdapter::INT_SMALL, 'smallint', 6], - // medium - [AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER, null, 'mediumint', 8], - [AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER, 2, 'mediumint', 2], - [AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER, MysqlAdapter::INT_MEDIUM, 'mediumint', 8], - // integer - [AdapterInterface::PHINX_TYPE_INTEGER, null, 'int', 11], - [AdapterInterface::PHINX_TYPE_INTEGER, 4, 'int', 4], - [AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_TINY, 'tinyint', 4], - [AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_SMALL, 'smallint', 6], - [AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_MEDIUM, 'mediumint', 8], - [AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_REGULAR, 'int', 11], - [AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_BIG, 'bigint', 20], - // bigint - [AdapterInterface::PHINX_TYPE_BIG_INTEGER, null, 'bigint', 20], - [AdapterInterface::PHINX_TYPE_BIG_INTEGER, 4, 'bigint', 4], - [AdapterInterface::PHINX_TYPE_BIG_INTEGER, MysqlAdapter::INT_BIG, 'bigint', 20], - ]; - } - - /** - * The second argument is not typed as MysqlAdapter::INT_BIG is a float, and all other values are integers - */ - #[DataProvider('sqlTypeIntConversionProvider')] - public function testGetSqlTypeIntegerConversion(string $type, $limit, string $expectedType, int $expectedLimit) - { - $sqlType = $this->adapter->getSqlType($type, $limit); - $this->assertSame($expectedType, $sqlType['name']); - $this->assertSame($expectedLimit, $sqlType['limit']); - } - - public function testLongTextColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'text', ['limit' => MysqlAdapter::TEXT_LONG]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('longtext', $sqlType['name']); - } - - public function testMediumTextColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'text', ['limit' => MysqlAdapter::TEXT_MEDIUM]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('mediumtext', $sqlType['name']); - } - - public function testTinyTextColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'text', ['limit' => MysqlAdapter::TEXT_TINY]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('tinytext', $sqlType['name']); - } - - public static function binaryToBlobAutomaticConversionData() - { - return [ - // limit, expected type, expected limit - [null, 'binary', null], - [64, 'binary', 255], - [MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM], - [MysqlAdapter::BLOB_REGULAR, 'binary', null], - [MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM], - [MysqlAdapter::BLOB_MEDIUM, 'mediumblob', MysqlAdapter::BLOB_MEDIUM], - [MysqlAdapter::BLOB_MEDIUM + 20, 'longblob', MysqlAdapter::BLOB_LONG], - [MysqlAdapter::BLOB_LONG, 'longblob', MysqlAdapter::BLOB_LONG], - ]; - } - - #[DataProvider('binaryToBlobAutomaticConversionData')] - public function testBinaryToBlobAutomaticConversion(?int $limit, string $expectedType, ?int $expectedLimit) - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'binary', ['limit' => $limit]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertSame($expectedType, $sqlType['name']); - $this->assertSame($expectedLimit, $columns[1]->getLimit()); - } - - public static function varbinaryToBlobAutomaticConversionData() - { - return [ - // limit, expected type, expected limit - [null, 'binary', null], - [64, 'binary', 255], - [MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM], - [MysqlAdapter::BLOB_REGULAR, 'binary', null], - [MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM], - [MysqlAdapter::BLOB_MEDIUM, 'mediumblob', MysqlAdapter::BLOB_MEDIUM], - [MysqlAdapter::BLOB_MEDIUM + 20, 'longblob', MysqlAdapter::BLOB_LONG], - [MysqlAdapter::BLOB_LONG, 'longblob', MysqlAdapter::BLOB_LONG], - ]; - } - - #[DataProvider('varbinaryToBlobAutomaticConversionData')] - public function testVarbinaryToBlobAutomaticConversion(?int $limit, string $expectedType, ?int $expectedLimit) - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'varbinary', ['limit' => $limit]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertSame($expectedType, $sqlType['name']); - $this->assertSame($expectedLimit, $columns[1]->getLimit()); - } - - public static function blobColumnsData() - { - return [ - // type, expected type, limit, expected limit - // Tiny blobs - ['tinyblob', 'binary', null, MysqlAdapter::BLOB_TINY], - ['tinyblob', 'binary', MysqlAdapter::BLOB_TINY, MysqlAdapter::BLOB_TINY], - ['tinyblob', 'mediumblob', MysqlAdapter::BLOB_TINY + 20, MysqlAdapter::BLOB_MEDIUM], - ['tinyblob', 'mediumblob', MysqlAdapter::BLOB_MEDIUM, MysqlAdapter::BLOB_MEDIUM], - ['tinyblob', 'longblob', MysqlAdapter::BLOB_LONG, MysqlAdapter::BLOB_LONG], - // // Regular blobs - ['blob', 'binary', MysqlAdapter::BLOB_TINY, MysqlAdapter::BLOB_TINY], - ['blob', 'binary', null, null], - ['blob', 'binary', MysqlAdapter::BLOB_REGULAR, null], - ['blob', 'mediumblob', MysqlAdapter::BLOB_MEDIUM, MysqlAdapter::BLOB_MEDIUM], - ['blob', 'longblob', MysqlAdapter::BLOB_LONG, MysqlAdapter::BLOB_LONG], - // // medium blobs - ['mediumblob', 'binary', MysqlAdapter::BLOB_TINY, MysqlAdapter::BLOB_TINY], - ['mediumblob', 'binary', MysqlAdapter::BLOB_REGULAR, null], - ['mediumblob', 'mediumblob', null, MysqlAdapter::BLOB_MEDIUM], - ['mediumblob', 'mediumblob', MysqlAdapter::BLOB_MEDIUM, MysqlAdapter::BLOB_MEDIUM], - ['mediumblob', 'longblob', MysqlAdapter::BLOB_LONG, MysqlAdapter::BLOB_LONG], - // long blobs - ['longblob', 'binary', MysqlAdapter::BLOB_TINY, MysqlAdapter::BLOB_TINY], - ['longblob', 'binary', MysqlAdapter::BLOB_REGULAR, null], - ['longblob', 'mediumblob', MysqlAdapter::BLOB_MEDIUM, MysqlAdapter::BLOB_MEDIUM], - ['longblob', 'longblob', null, MysqlAdapter::BLOB_LONG], - ['longblob', 'longblob', MysqlAdapter::BLOB_LONG, MysqlAdapter::BLOB_LONG], - ]; - } - - #[DataProvider('blobColumnsData')] - public function testblobColumns(string $type, string $expectedType, ?int $limit, ?int $expectedLimit) - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', $type, ['limit' => $limit]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertSame($expectedType, $sqlType['name']); - $this->assertSame($expectedLimit, $columns[1]->getLimit()); - } - - public function testBigIntegerColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'integer', ['limit' => MysqlAdapter::INT_BIG]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('bigint', $sqlType['name']); - } - - public function testMediumIntegerColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'integer', ['limit' => MysqlAdapter::INT_MEDIUM]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('int', $sqlType['name']); - } - - public function testSmallIntegerColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'integer', ['limit' => MysqlAdapter::INT_SMALL]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('int', $sqlType['name']); - } - - public function testTinyIntegerColumn() - { - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'integer', ['limit' => MysqlAdapter::INT_TINY]) - ->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals('int', $sqlType['name']); - } - - public function testDatetimeColumn() - { - $this->adapter->connect(); - $version = $this->adapter->getConnection()->getDriver()->version(); - if (version_compare($version, '5.6.4') === -1) { - $this->markTestSkipped('Cannot test datetime limit on versions less than 5.6.4'); - } - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'datetime')->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertNull($sqlType['limit']); - } - - public function testDatetimeColumnLimit() - { - $this->adapter->connect(); - $version = $this->adapter->getConnection()->getDriver()->version(); - if (version_compare($version, '5.6.4') === -1) { - $this->markTestSkipped('Cannot test datetime limit on versions less than 5.6.4'); - } - $limit = 6; - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'datetime', ['limit' => $limit])->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals($limit, $sqlType['limit']); - } - - public function testTimestampColumnLimit() - { - $this->adapter->connect(); - $version = $this->adapter->getConnection()->getDriver()->version(); - if (version_compare($version, '5.6.4') === -1) { - $this->markTestSkipped('Cannot test datetime limit on versions less than 5.6.4'); - } - $limit = 1; - $table = new Table('t', [], $this->adapter); - $table->addColumn('column1', 'timestamp', ['limit' => $limit])->save(); - $columns = $table->getColumns(); - $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); - $this->assertEquals($limit, $sqlType['limit']); - } - public function testTimestampInvalidLimit() { $this->adapter->connect(); @@ -2433,12 +2180,4 @@ public function testCreateTableWithPrecisionCurrentTimestamp() $colDef = $rows[0]; $this->assertEqualsIgnoringCase('CURRENT_TIMESTAMP(3)', $colDef['COLUMN_DEFAULT']); } - - public function testGetSqlType() - { - if (!$this->usingMariaDbWithUuid()) { - $this->expectException(UnsupportedColumnTypeException::class); - } - $this->assertSame(['name' => 'uuid'], $this->adapter->getSqlType('nativeuuid')); - } } diff --git a/tests/TestCase/Db/Adapter/PostgresAdapterTest.php b/tests/TestCase/Db/Adapter/PostgresAdapterTest.php index dbf72452..00cda5e5 100644 --- a/tests/TestCase/Db/Adapter/PostgresAdapterTest.php +++ b/tests/TestCase/Db/Adapter/PostgresAdapterTest.php @@ -11,7 +11,6 @@ use InvalidArgumentException; use Migrations\Db\Adapter\AdapterInterface; use Migrations\Db\Adapter\PostgresAdapter; -use Migrations\Db\Adapter\UnsupportedColumnTypeException; use Migrations\Db\Literal; use Migrations\Db\Table; use Migrations\Db\Table\Column; @@ -1862,14 +1861,6 @@ public function testDropAllSchemas() $this->assertFalse($this->adapter->hasSchema('bar')); } - public function testInvalidSqlType() - { - $this->expectException(UnsupportedColumnTypeException::class); - $this->expectExceptionMessage('Column type `idontexist` is not supported by Postgresql.'); - - $this->adapter->getSqlType('idontexist'); - } - public function testCreateTableWithComment() { $tableComment = 'Table comment'; diff --git a/tests/TestCase/Db/Adapter/SqliteAdapterTest.php b/tests/TestCase/Db/Adapter/SqliteAdapterTest.php index 71f92b90..99aa98c3 100644 --- a/tests/TestCase/Db/Adapter/SqliteAdapterTest.php +++ b/tests/TestCase/Db/Adapter/SqliteAdapterTest.php @@ -9,10 +9,8 @@ use Cake\Console\TestSuite\StubConsoleOutput; use Cake\Database\Connection; use Cake\Datasource\ConnectionManager; -use Exception; use InvalidArgumentException; use Migrations\Db\Adapter\SqliteAdapter; -use Migrations\Db\Adapter\UnsupportedColumnTypeException; use Migrations\Db\Expression; use Migrations\Db\Literal; use Migrations\Db\Table; @@ -2650,95 +2648,6 @@ public static function hasNamedForeignKeyProvider(): array ]; } - #[DataProvider('providePhinxTypes')] - public function testGetSqlType($phinxType, $limit, $exp) - { - if ($exp instanceof Exception) { - $this->expectException(get_class($exp)); - - $this->adapter->getSqlType($phinxType, $limit); - } else { - $exp = ['name' => $exp, 'limit' => $limit]; - $this->assertEquals($exp, $this->adapter->getSqlType($phinxType, $limit)); - } - } - - public static function providePhinxTypes() - { - $unsupported = new UnsupportedColumnTypeException(); - - return [ - [SqliteAdapter::PHINX_TYPE_BIG_INTEGER, null, SqliteAdapter::PHINX_TYPE_BIG_INTEGER], - [SqliteAdapter::PHINX_TYPE_BINARY, null, SqliteAdapter::PHINX_TYPE_BINARY . '_blob'], - [SqliteAdapter::PHINX_TYPE_BIT, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_BLOB, null, SqliteAdapter::PHINX_TYPE_BLOB], - [SqliteAdapter::PHINX_TYPE_BOOLEAN, null, SqliteAdapter::PHINX_TYPE_BOOLEAN . '_integer'], - [SqliteAdapter::PHINX_TYPE_CHAR, null, SqliteAdapter::PHINX_TYPE_CHAR], - [SqliteAdapter::PHINX_TYPE_CIDR, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_DATE, null, SqliteAdapter::PHINX_TYPE_DATE . '_text'], - [SqliteAdapter::PHINX_TYPE_DATETIME, null, SqliteAdapter::PHINX_TYPE_DATETIME . '_text'], - [SqliteAdapter::PHINX_TYPE_DECIMAL, null, SqliteAdapter::PHINX_TYPE_DECIMAL], - [SqliteAdapter::PHINX_TYPE_DOUBLE, null, SqliteAdapter::PHINX_TYPE_DOUBLE], - [SqliteAdapter::PHINX_TYPE_ENUM, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_FILESTREAM, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_FLOAT, null, SqliteAdapter::PHINX_TYPE_FLOAT], - [SqliteAdapter::PHINX_TYPE_GEOMETRY, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_INET, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_INTEGER, null, SqliteAdapter::PHINX_TYPE_INTEGER], - [SqliteAdapter::PHINX_TYPE_INTERVAL, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_JSON, null, SqliteAdapter::PHINX_TYPE_JSON . '_text'], - [SqliteAdapter::PHINX_TYPE_JSONB, null, SqliteAdapter::PHINX_TYPE_JSONB . '_text'], - [SqliteAdapter::PHINX_TYPE_LINESTRING, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_MACADDR, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_POINT, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_POLYGON, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_SET, null, $unsupported], - [SqliteAdapter::PHINX_TYPE_SMALL_INTEGER, null, SqliteAdapter::PHINX_TYPE_SMALL_INTEGER], - [SqliteAdapter::PHINX_TYPE_STRING, null, 'varchar'], - [SqliteAdapter::PHINX_TYPE_TEXT, null, SqliteAdapter::PHINX_TYPE_TEXT], - [SqliteAdapter::PHINX_TYPE_TIME, null, SqliteAdapter::PHINX_TYPE_TIME . '_text'], - [SqliteAdapter::PHINX_TYPE_TIMESTAMP, null, SqliteAdapter::PHINX_TYPE_TIMESTAMP . '_text'], - [SqliteAdapter::PHINX_TYPE_UUID, null, SqliteAdapter::PHINX_TYPE_UUID . '_text'], - [SqliteAdapter::PHINX_TYPE_VARBINARY, null, SqliteAdapter::PHINX_TYPE_VARBINARY . '_blob'], - [SqliteAdapter::PHINX_TYPE_STRING, 5, 'varchar'], - [Literal::from('someType'), 5, Literal::from('someType')], - ['notAType', null, $unsupported], - ]; - } - - public function testGetColumnTypes() - { - $columnTypes = $this->adapter->getColumnTypes(); - $expected = [ - SqliteAdapter::PHINX_TYPE_BIG_INTEGER, - SqliteAdapter::PHINX_TYPE_BINARY, - SqliteAdapter::PHINX_TYPE_BLOB, - SqliteAdapter::PHINX_TYPE_BOOLEAN, - SqliteAdapter::PHINX_TYPE_CHAR, - SqliteAdapter::PHINX_TYPE_DATE, - SqliteAdapter::PHINX_TYPE_DATETIME, - SqliteAdapter::PHINX_TYPE_DECIMAL, - SqliteAdapter::PHINX_TYPE_DOUBLE, - SqliteAdapter::PHINX_TYPE_FLOAT, - SqliteAdapter::PHINX_TYPE_INTEGER, - SqliteAdapter::PHINX_TYPE_JSON, - SqliteAdapter::PHINX_TYPE_JSONB, - SqliteAdapter::PHINX_TYPE_SMALL_INTEGER, - SqliteAdapter::PHINX_TYPE_STRING, - SqliteAdapter::PHINX_TYPE_TEXT, - SqliteAdapter::PHINX_TYPE_TIME, - SqliteAdapter::PHINX_TYPE_UUID, - SqliteAdapter::PHINX_TYPE_BINARYUUID, - SqliteAdapter::PHINX_TYPE_TIMESTAMP, - SqliteAdapter::PHINX_TYPE_TINY_INTEGER, - SqliteAdapter::PHINX_TYPE_VARBINARY, - ]; - sort($columnTypes); - sort($expected); - - $this->assertEquals($expected, $columnTypes); - } - #[DataProvider('provideColumnTypesForValidation')] public function testIsValidColumnType($phinxType, $exp) { diff --git a/tests/TestCase/Db/Adapter/SqlserverAdapterTest.php b/tests/TestCase/Db/Adapter/SqlserverAdapterTest.php index 8c68e580..68b3959e 100644 --- a/tests/TestCase/Db/Adapter/SqlserverAdapterTest.php +++ b/tests/TestCase/Db/Adapter/SqlserverAdapterTest.php @@ -19,7 +19,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; -use RuntimeException; class SqlserverAdapterTest extends TestCase { @@ -1149,14 +1148,6 @@ public function testQuoteSchemaName() $this->assertEquals('[schema].[schema]', $this->adapter->quoteSchemaName('schema.schema')); } - public function testInvalidSqlType() - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Column type "idontexist" is not supported by SqlServer.'); - - $this->adapter->getSqlType('idontexist'); - } - public function testAddColumnComment() { $table = new Table('table1', [], $this->adapter);