Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow precision 0 in Carbon DBAL types #2537

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved

$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