Skip to content

Commit e3a8b12

Browse files
authored
Improve precision and scale behavior across adapters (#2383)
1 parent 144b211 commit e3a8b12

File tree

7 files changed

+69
-25
lines changed

7 files changed

+69
-25
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@ parameters:
44
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
55
count: 1
66
path: src/Phinx/Db/Adapter/MysqlAdapter.php
7-
8-
-
9-
message: "#^Ternary operator condition is always true\\.$#"
10-
count: 2
11-
path: src/Phinx/Db/Adapter/SqlServerAdapter.php
12-

src/Phinx/Db/Adapter/SQLiteAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ protected function getColumnSqlDefinition(Column $column): string
18681868
$def .= '(' . ($column->getLimit() ?: $sqlType['limit']) . ')';
18691869
}
18701870
}
1871-
if ($column->getPrecision() && $column->getScale()) {
1871+
if ($column->getPrecision() && $column->getScale() !== null) {
18721872
$def .= '(' . $column->getPrecision() . ',' . $column->getScale() . ')';
18731873
}
18741874

src/Phinx/Db/Adapter/SqlServerAdapter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,17 @@ public function getColumns(string $tableName): array
476476
->setNull($columnInfo['null'] !== 'NO')
477477
->setDefault($this->parseDefault($columnInfo['default']))
478478
->setIdentity($columnInfo['identity'] === '1')
479+
->setScale($columnInfo['scale'] ? (int)$columnInfo['scale'] : null)
479480
->setComment($this->getColumnComment($columnInfo['table_name'], $columnInfo['name']));
480481

481482
if (!empty($columnInfo['char_length'])) {
482483
$column->setLimit((int)$columnInfo['char_length']);
483484
}
484485

486+
if ($type === self::PHINX_TYPE_DECIMAL) {
487+
$column->setPrecision((int)$columnInfo['precision']);
488+
}
489+
485490
$columns[$columnInfo['name']] = $column;
486491
}
487492

@@ -1063,8 +1068,9 @@ public function getSqlType(Literal|string $type, ?int $limit = null): array
10631068
{
10641069
$type = (string)$type;
10651070
switch ($type) {
1066-
case static::PHINX_TYPE_FLOAT:
10671071
case static::PHINX_TYPE_DECIMAL:
1072+
return ['name' => $type, 'precision' => 18, 'scale' => 0];
1073+
case static::PHINX_TYPE_FLOAT:
10681074
case static::PHINX_TYPE_DATETIME:
10691075
case static::PHINX_TYPE_TIME:
10701076
case static::PHINX_TYPE_DATE:
@@ -1237,7 +1243,7 @@ protected function getColumnSqlDefinition(Column $column, bool $create = true):
12371243
'tinyint',
12381244
'smallint',
12391245
];
1240-
if ($sqlType['name'] === static::PHINX_TYPE_DECIMAL && $column->getPrecision() && $column->getScale()) {
1246+
if ($sqlType['name'] === static::PHINX_TYPE_DECIMAL && ($column->getPrecision() || $column->getScale())) {
12411247
$buffer[] = sprintf(
12421248
'(%s, %s)',
12431249
$column->getPrecision() ?: $sqlType['precision'],

tests/Phinx/Db/Adapter/MysqlAdapterTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,7 @@ public function columnsProvider()
13891389
['column6', 'float', []],
13901390
['column7', 'decimal', []],
13911391
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
1392+
['decimal_precision_zero_scale', 'decimal', ['precision' => 15, 'scale' => 0]],
13921393
['decimal_limit', 'decimal', ['limit' => 10]],
13931394
['decimal_precision', 'decimal', ['precision' => 10]],
13941395
['column8', 'datetime', []],
@@ -2727,21 +2728,6 @@ public function testCreateTableWithPrecisionCurrentTimestamp()
27272728
$this->assertEqualsIgnoringCase('CURRENT_TIMESTAMP(3)', $colDef['COLUMN_DEFAULT']);
27282729
}
27292730

2730-
public function testCreateTableWithZeroScale(): void
2731-
{
2732-
$this->adapter->connect();
2733-
$table = new Table('test_table', ['id' => false], $this->adapter);
2734-
$table
2735-
->addColumn('col_1', 'decimal', ['null' => false, 'precision' => 15, 'scale' => 0])
2736-
->create();
2737-
2738-
$columns = $table->getColumns();
2739-
$this->assertCount(1, $columns);
2740-
$this->assertSame('col_1', $columns[0]->getName());
2741-
$this->assertSame(15, $columns[0]->getPrecision());
2742-
$this->assertSame(0, $columns[0]->getScale());
2743-
}
2744-
27452731
public function pdoAttributeProvider()
27462732
{
27472733
return [

tests/Phinx/Db/Adapter/PostgresAdapterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ public function columnsProvider()
12581258
['column13', 'string', ['limit' => 10]],
12591259
['column16', 'interval', []],
12601260
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
1261+
['decimal_precision_zero_scale', 'decimal', ['precision' => 12, 'scale' => 0]],
12611262
['decimal_limit', 'decimal', ['limit' => 10]],
12621263
['decimal_precision', 'decimal', ['precision' => 10]],
12631264
];
@@ -1284,6 +1285,18 @@ public function testGetColumns($colName, $type, $options, $actualType = null)
12841285
} else {
12851286
$this->assertEquals(['name' => $actualType] + $options, $columns[1]->getType());
12861287
}
1288+
1289+
if (isset($options['limit'])) {
1290+
$this->assertEquals($options['limit'], $columns[1]->getLimit());
1291+
}
1292+
1293+
if (isset($options['precision'])) {
1294+
$this->assertEquals($options['precision'], $columns[1]->getPrecision());
1295+
}
1296+
1297+
if (isset($options['scale'])) {
1298+
$this->assertEquals($options['scale'], $columns[1]->getScale());
1299+
}
12871300
}
12881301

12891302
/**

tests/Phinx/Db/Adapter/SQLiteAdapterTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,9 +1319,41 @@ public function columnsProvider()
13191319
['column15', 'smallinteger', []],
13201320
['column15', 'integer', []],
13211321
['column23', 'json', []],
1322+
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
1323+
['decimal_precision_zero_scale', 'decimal', ['precision' => 10, 'scale' => 0]],
13221324
];
13231325
}
13241326

1327+
/**
1328+
* @dataProvider columnsProvider
1329+
*/
1330+
public function testGetColumns($colName, $type, $options)
1331+
{
1332+
$table = new Table('t', [], $this->adapter);
1333+
$table->addColumn($colName, $type, $options)->save();
1334+
1335+
$columns = $this->adapter->getColumns('t');
1336+
$this->assertCount(2, $columns);
1337+
$this->assertEquals($colName, $columns[1]->getName());
1338+
$this->assertEquals($type, $columns[1]->getType());
1339+
1340+
if (isset($options['limit'])) {
1341+
$this->assertEquals($options['limit'], $columns[1]->getLimit());
1342+
}
1343+
1344+
if (isset($options['precision'])) {
1345+
$this->assertEquals($options['precision'], $columns[1]->getPrecision());
1346+
}
1347+
1348+
if (isset($options['scale'])) {
1349+
$this->assertEquals($options['scale'], $columns[1]->getScale());
1350+
}
1351+
1352+
if (isset($options['comment'])) {
1353+
$this->assertEquals($options['comment'], $columns[1]->getComment());
1354+
}
1355+
}
1356+
13251357
public function testAddIndex()
13261358
{
13271359
$table = new Table('table1', [], $this->adapter);
@@ -3166,7 +3198,7 @@ public function provideColumnNamesToCheck()
31663198
* @covers \Phinx\Db\Adapter\SQLiteAdapter::getTableInfo
31673199
* @covers \Phinx\Db\Adapter\SQLiteAdapter::getColumns
31683200
*/
3169-
public function testGetColumns()
3201+
public function testGetMultipleColumns()
31703202
{
31713203
$conn = $this->adapter->getConnection();
31723204
$conn->exec('create table t(a integer, b text, c char(5), d integer(12,6), e integer not null, f integer null)');

tests/Phinx/Db/Adapter/SqlServerAdapterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ public function columnsProvider()
709709
['column13', 'tinyinteger', ['default' => 5]],
710710
['column14', 'smallinteger', ['default' => 5]],
711711
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
712+
['decimal_precision_zero_scale', 'decimal', ['precision' => 10, 'scale' => 0]],
712713
['decimal_limit', 'decimal', ['limit' => 10]],
713714
['decimal_precision', 'decimal', ['precision' => 10]],
714715
];
@@ -728,6 +729,18 @@ public function testGetColumns($colName, $type, $options)
728729
$this->assertCount(2, $columns);
729730
$this->assertEquals($colName, $columns[$colName]->getName());
730731
$this->assertEquals($type, $columns[$colName]->getType());
732+
733+
if (isset($options['limit'])) {
734+
$this->assertEquals($options['limit'], $columns[$colName]->getLimit());
735+
}
736+
737+
if (isset($options['precision'])) {
738+
$this->assertEquals($options['precision'], $columns[$colName]->getPrecision());
739+
}
740+
741+
if (isset($options['scale'])) {
742+
$this->assertEquals($options['scale'], $columns[$colName]->getScale());
743+
}
731744
}
732745

733746
public function testAddIndex()

0 commit comments

Comments
 (0)