Skip to content

Commit 9008ac1

Browse files
committed
Support using mysql int constants on same types
1 parent 4b9170a commit 9008ac1

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Phinx/Db/Adapter/MysqlAdapter.php

+16
Original file line numberDiff line numberDiff line change
@@ -1019,12 +1019,28 @@ public function getSqlType($type, $limit = null)
10191019
case static::PHINX_TYPE_BIT:
10201020
return ['name' => 'bit', 'limit' => $limit ?: 64];
10211021
case static::PHINX_TYPE_BIG_INTEGER:
1022+
if ($limit === static::INT_BIG) {
1023+
$limit = 20;
1024+
}
1025+
10221026
return ['name' => 'bigint', 'limit' => $limit ?: 20];
10231027
case static::PHINX_TYPE_MEDIUM_INTEGER:
1028+
if ($limit === static::INT_MEDIUM) {
1029+
$limit = 8;
1030+
}
1031+
10241032
return ['name' => 'mediumint', 'limit' => $limit ?: 8];
10251033
case static::PHINX_TYPE_SMALL_INTEGER:
1034+
if ($limit === static::INT_SMALL) {
1035+
$limit = 6;
1036+
}
1037+
10261038
return ['name' => 'smallint', 'limit' => $limit ?: 6];
10271039
case static::PHINX_TYPE_TINY_INTEGER:
1040+
if ($limit === static::INT_TINY) {
1041+
$limit = 4;
1042+
}
1043+
10281044
return ['name' => 'tinyint', 'limit' => $limit ?: 4];
10291045
case static::PHINX_TYPE_INTEGER:
10301046
if ($limit && $limit >= static::INT_TINY) {

tests/Phinx/Db/Adapter/MysqlAdapterTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,48 @@ public function testChangeColumnDefaultToNull()
884884
$this->assertNull($rows[1]['Default']);
885885
}
886886

887+
public function sqlTypeProvider()
888+
{
889+
// TODO: add tests for missing types
890+
return [
891+
// tinyint
892+
[AdapterInterface::PHINX_TYPE_TINY_INTEGER, null, 'tinyint', 4],
893+
[AdapterInterface::PHINX_TYPE_TINY_INTEGER, 2, 'tinyint', 2],
894+
[AdapterInterface::PHINX_TYPE_TINY_INTEGER, MysqlAdapter::INT_TINY, 'tinyint', 4],
895+
// smallint
896+
[AdapterInterface::PHINX_TYPE_SMALL_INTEGER, null, 'smallint', 6],
897+
[AdapterInterface::PHINX_TYPE_SMALL_INTEGER, 3, 'smallint', 3],
898+
[AdapterInterface::PHINX_TYPE_SMALL_INTEGER, MysqlAdapter::INT_SMALL, 'smallint', 6],
899+
// medium
900+
[AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER, null, 'mediumint', 8],
901+
[AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER, 2, 'mediumint', 2],
902+
[AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER, MysqlAdapter::INT_MEDIUM, 'mediumint', 8],
903+
// integer
904+
[AdapterInterface::PHINX_TYPE_INTEGER, null, 'int', 11],
905+
[AdapterInterface::PHINX_TYPE_INTEGER, 4, 'int', 4],
906+
[AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_TINY, 'tinyint', 4],
907+
[AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_SMALL, 'smallint', 6],
908+
[AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_MEDIUM, 'mediumint', 8],
909+
[AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_REGULAR, 'int', 11],
910+
[AdapterInterface::PHINX_TYPE_INTEGER, MysqlAdapter::INT_BIG, 'bigint', 20],
911+
// bigint
912+
[AdapterInterface::PHINX_TYPE_BIG_INTEGER, null, 'bigint', 20],
913+
[AdapterInterface::PHINX_TYPE_BIG_INTEGER, 4, 'bigint', 4],
914+
[AdapterInterface::PHINX_TYPE_BIG_INTEGER, MysqlAdapter::INT_BIG, 'bigint', 20],
915+
];
916+
}
917+
918+
/**
919+
* @dataProvider sqlTypeProvider
920+
* The second argument is not typed as MysqlAdapter::INT_BIG is a float, and all other values are integers
921+
*/
922+
public function testGetSqlType(string $type, $limit, string $expectedType, int $expectedLimit)
923+
{
924+
$sqlType = $this->adapter->getSqlType($type, $limit);
925+
$this->assertEquals($expectedType, $sqlType['name']);
926+
$this->assertEquals($expectedLimit, $sqlType['limit']);
927+
}
928+
887929
public function testLongTextColumn()
888930
{
889931
$table = new \Phinx\Db\Table('t', [], $this->adapter);

0 commit comments

Comments
 (0)