Skip to content
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ parameters:
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
count: 1
path: src/Phinx/Db/Adapter/MysqlAdapter.php

-
message: "#^Ternary operator condition is always true\\.$#"
count: 2
path: src/Phinx/Db/Adapter/SqlServerAdapter.php

2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ protected function getColumnSqlDefinition(Column $column): string
$def .= '(' . ($column->getLimit() ?: $sqlType['limit']) . ')';
}
}
if ($column->getPrecision() && $column->getScale()) {
if ($column->getPrecision() && $column->getScale() !== null) {
$def .= '(' . $column->getPrecision() . ',' . $column->getScale() . ')';
}

Expand Down
10 changes: 8 additions & 2 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,17 @@ public function getColumns(string $tableName): array
->setNull($columnInfo['null'] !== 'NO')
->setDefault($this->parseDefault($columnInfo['default']))
->setIdentity($columnInfo['identity'] === '1')
->setScale($columnInfo['scale'] ? (int)$columnInfo['scale'] : null)
->setComment($this->getColumnComment($columnInfo['table_name'], $columnInfo['name']));

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

if ($type === self::PHINX_TYPE_DECIMAL) {
$column->setPrecision((int)$columnInfo['precision']);
}

$columns[$columnInfo['name']] = $column;
}

Expand Down Expand Up @@ -1063,8 +1068,9 @@ 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:
return ['name' => $type, 'precision' => 18, 'scale' => 0];
case static::PHINX_TYPE_FLOAT:
case static::PHINX_TYPE_DATETIME:
case static::PHINX_TYPE_TIME:
case static::PHINX_TYPE_DATE:
Expand Down Expand Up @@ -1237,7 +1243,7 @@ protected function getColumnSqlDefinition(Column $column, bool $create = true):
'tinyint',
'smallint',
];
if ($sqlType['name'] === static::PHINX_TYPE_DECIMAL && $column->getPrecision() && $column->getScale()) {
if ($sqlType['name'] === static::PHINX_TYPE_DECIMAL && ($column->getPrecision() || $column->getScale())) {
$buffer[] = sprintf(
'(%s, %s)',
$column->getPrecision() ?: $sqlType['precision'],
Expand Down
16 changes: 1 addition & 15 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ public function columnsProvider()
['column6', 'float', []],
['column7', 'decimal', []],
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
['decimal_precision_zero_scale', 'decimal', ['precision' => 15, 'scale' => 0]],
['decimal_limit', 'decimal', ['limit' => 10]],
['decimal_precision', 'decimal', ['precision' => 10]],
['column8', 'datetime', []],
Expand Down Expand Up @@ -2727,21 +2728,6 @@ public function testCreateTableWithPrecisionCurrentTimestamp()
$this->assertEqualsIgnoringCase('CURRENT_TIMESTAMP(3)', $colDef['COLUMN_DEFAULT']);
}

public function testCreateTableWithZeroScale(): void
{
$this->adapter->connect();
$table = new Table('test_table', ['id' => false], $this->adapter);
$table
->addColumn('col_1', 'decimal', ['null' => false, 'precision' => 15, 'scale' => 0])
->create();

$columns = $table->getColumns();
$this->assertCount(1, $columns);
$this->assertSame('col_1', $columns[0]->getName());
$this->assertSame(15, $columns[0]->getPrecision());
$this->assertSame(0, $columns[0]->getScale());
}

public function pdoAttributeProvider()
{
return [
Expand Down
13 changes: 13 additions & 0 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ public function columnsProvider()
['column13', 'string', ['limit' => 10]],
['column16', 'interval', []],
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
['decimal_precision_zero_scale', 'decimal', ['precision' => 12, 'scale' => 0]],
['decimal_limit', 'decimal', ['limit' => 10]],
['decimal_precision', 'decimal', ['precision' => 10]],
];
Expand All @@ -1284,6 +1285,18 @@ public function testGetColumns($colName, $type, $options, $actualType = null)
} else {
$this->assertEquals(['name' => $actualType] + $options, $columns[1]->getType());
}

if (isset($options['limit'])) {
$this->assertEquals($options['limit'], $columns[1]->getLimit());
}

if (isset($options['precision'])) {
$this->assertEquals($options['precision'], $columns[1]->getPrecision());
}

if (isset($options['scale'])) {
$this->assertEquals($options['scale'], $columns[1]->getScale());
}
}

/**
Expand Down
34 changes: 33 additions & 1 deletion tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1319,9 +1319,41 @@ public function columnsProvider()
['column15', 'smallinteger', []],
['column15', 'integer', []],
['column23', 'json', []],
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
['decimal_precision_zero_scale', 'decimal', ['precision' => 10, 'scale' => 0]],
];
}

/**
* @dataProvider columnsProvider
*/
public function testGetColumns($colName, $type, $options)
{
$table = new Table('t', [], $this->adapter);
$table->addColumn($colName, $type, $options)->save();

$columns = $this->adapter->getColumns('t');
$this->assertCount(2, $columns);
$this->assertEquals($colName, $columns[1]->getName());
$this->assertEquals($type, $columns[1]->getType());

if (isset($options['limit'])) {
$this->assertEquals($options['limit'], $columns[1]->getLimit());
}

if (isset($options['precision'])) {
$this->assertEquals($options['precision'], $columns[1]->getPrecision());
}

if (isset($options['scale'])) {
$this->assertEquals($options['scale'], $columns[1]->getScale());
}

if (isset($options['comment'])) {
$this->assertEquals($options['comment'], $columns[1]->getComment());
}
}

public function testAddIndex()
{
$table = new Table('table1', [], $this->adapter);
Expand Down Expand Up @@ -3166,7 +3198,7 @@ public function provideColumnNamesToCheck()
* @covers \Phinx\Db\Adapter\SQLiteAdapter::getTableInfo
* @covers \Phinx\Db\Adapter\SQLiteAdapter::getColumns
*/
public function testGetColumns()
public function testGetMultipleColumns()
{
$conn = $this->adapter->getConnection();
$conn->exec('create table t(a integer, b text, c char(5), d integer(12,6), e integer not null, f integer null)');
Expand Down
13 changes: 13 additions & 0 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ public function columnsProvider()
['column13', 'tinyinteger', ['default' => 5]],
['column14', 'smallinteger', ['default' => 5]],
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
['decimal_precision_zero_scale', 'decimal', ['precision' => 10, 'scale' => 0]],
['decimal_limit', 'decimal', ['limit' => 10]],
['decimal_precision', 'decimal', ['precision' => 10]],
];
Expand All @@ -728,6 +729,18 @@ public function testGetColumns($colName, $type, $options)
$this->assertCount(2, $columns);
$this->assertEquals($colName, $columns[$colName]->getName());
$this->assertEquals($type, $columns[$colName]->getType());

if (isset($options['limit'])) {
$this->assertEquals($options['limit'], $columns[$colName]->getLimit());
}

if (isset($options['precision'])) {
$this->assertEquals($options['precision'], $columns[$colName]->getPrecision());
}

if (isset($options['scale'])) {
$this->assertEquals($options['scale'], $columns[$colName]->getScale());
}
}

public function testAddIndex()
Expand Down