diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index b6fe2d324..e94c74dde 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 - diff --git a/src/Phinx/Db/Adapter/SQLiteAdapter.php b/src/Phinx/Db/Adapter/SQLiteAdapter.php index f6f7759c2..fe622177f 100644 --- a/src/Phinx/Db/Adapter/SQLiteAdapter.php +++ b/src/Phinx/Db/Adapter/SQLiteAdapter.php @@ -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() . ')'; } diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index 37b4bfd5e..28a90f1f0 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -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; } @@ -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: @@ -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'], diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index ab98cc51f..1b6ec3d47 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -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', []], @@ -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 [ diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 46714f992..d1db27e39 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -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]], ]; @@ -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()); + } } /** diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index 433e618c5..e9665abe0 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -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); @@ -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)'); diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index e442e1aaa..a69fc96a1 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -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]], ]; @@ -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()