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

Add precision to MSSQL datetime2 #6393

Closed
Closed
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
8 changes: 6 additions & 2 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,12 @@
public function getDateTimeTypeDeclarationSQL(array $column)
{
// 3 - microseconds precision length
// http://msdn.microsoft.com/en-us/library/ms187819.aspx
return 'DATETIME2(6)';
// https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime2-transact-sql?view=sql-server-ver16
$precision = $column['precision'] ?? null;
if (is_null($precision) || !is_int($precision) || $precision < 0 || $precision > 7) {

Check failure on line 1394 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 line after "if", found 0.

Check failure on line 1394 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Function is_null() should not be referenced via a fallback global name, but via a use statement.

Check failure on line 1394 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

The use of function is_null() is forbidden

Check failure on line 1394 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 space after NOT operator; 0 found

Check failure on line 1394 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Function is_int() should not be referenced via a fallback global name, but via a use statement.
$precision = 6;
}
return 'DATETIME2(' + $precision + ')';

Check failure on line 1397 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Binary operation "+" between 'DATETIME2(' and int<0, 7> results in an error.

Check failure on line 1397 in src/Platforms/SQLServerPlatform.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 line before "return", found 0.
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,30 @@ public function testGeneratesTypeDeclarationForDateTimeTz(): void
self::assertEquals('DATETIMEOFFSET(6)', $this->platform->getDateTimeTzTypeDeclarationSQL([]));
}

public function testGeneratesTypeDeclarationsForDateTime(): void
{
self::assertEquals(
'DATETIME2(6)',
$this->platform->getDateTimeTypeDeclarationSQL([]),
);
self::assertEquals(
'DATETIME2(0)',
$this->platform->getDateTimeTypeDeclarationSQL(['precision' => 0]),
);
self::assertEquals(
'DATETIME2(7)',
$this->platform->getDateTimeTypeDeclarationSQL(['precision' => 7]),
);
self::assertEquals(
'DATETIME2(6)',
$this->platform->getDateTimeTypeDeclarationSQL(['precision' => -1]),
);
self::assertEquals(
'DATETIME2(6)',
$this->platform->getDateTimeTypeDeclarationSQL(['precision' => 255]),
);
}

public function testDropIndexSQLRequiresTable(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
Loading