Skip to content

Commit

Permalink
Merge pull request #2537 from lingoda/allow-precision-0-in-carbon-dba…
Browse files Browse the repository at this point in the history
…l-types

Allow precision 0 in Carbon DBAL types
  • Loading branch information
kylekatarnls authored Jan 21, 2022
2 parents b989a73 + 5a6c852 commit 626ec8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Carbon/Doctrine/CarbonTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ protected function getCarbonClassName(): string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$precision = ($fieldDeclaration['precision'] ?: 10) === 10
? DateTimeDefaultPrecision::get()
: $fieldDeclaration['precision'];
$precision = $fieldDeclaration['precision'] ?: 10;

if ($fieldDeclaration['secondPrecision'] ?? false) {
$precision = 0;
}

if ($precision === 10) {
$precision = DateTimeDefaultPrecision::get();
}

$type = parent::getSQLDeclaration($fieldDeclaration, $platform);

if (!$precision) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/CarbonTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,32 @@ public function testGetSQLDeclaration(string $name)
$precision = DateTimeDefaultPrecision::get();
$this->assertSame(6, $precision);

$this->assertSame('DATETIME', $type->getSQLDeclaration([
'precision' => null,
'secondPrecision' => true,
], new MySQL57Platform()));

$this->assertSame('DATETIME(3)', $type->getSQLDeclaration([
'precision' => 3,
], new MySQL57Platform()));

$this->assertSame('TIMESTAMP(0)', $type->getSQLDeclaration([
'precision' => null,
'secondPrecision' => true,
], new DB2Platform()));

$this->assertSame('TIMESTAMP(6)', $type->getSQLDeclaration([
'precision' => null,
], new DB2Platform()));

$this->assertSame('TIMESTAMP(6)', $type->getSQLDeclaration([
'precision' => 0,
], new DB2Platform()));

$this->assertSame('DATETIME(6)', $type->getSQLDeclaration([
'precision' => 0,
], new MySQL57Platform()));

$this->assertSame('DATETIME(6)', $type->getSQLDeclaration([
'precision' => null,
], new MySQL57Platform()));
Expand Down

0 comments on commit 626ec8c

Please sign in to comment.