Skip to content
Draft
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: 7 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,12 @@ protected function typeEnum(Fluent $column)
*/
protected function typeJson(Fluent $column)
{
$version = $this->connection->getServerVersion();

if (version_compare($version, '2025', '>=')) {
return 'json';
}

return 'nvarchar(max)';
}

Expand All @@ -760,7 +766,7 @@ protected function typeJson(Fluent $column)
*/
protected function typeJsonb(Fluent $column)
{
return 'nvarchar(max)';
return $this->typeJson($column);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/SqlServerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,36 @@ public function testDropDatabaseIfExists()

$builder->dropDatabaseIfExists('my_temporary_database_b');
}

public function testItUsesNativeJsonTypeForSqlServer2025Plus()
{
// Mockery is already aliased as 'm' at the top of the file.
$connection = m::mock(Connection::class);
$grammar = new SqlServerGrammar($connection);

// CRITICAL: Mock the internal connection method to return the version supporting the feature.
$connection->shouldReceive('getServerVersion')->once()->andReturn('2025.0.0');
// Mock table prefix if needed by the Grammar methods you touch.
$connection->shouldReceive('getTablePrefix')->andReturn('');

$column = new \Illuminate\Support\Fluent(['name' => 'data', 'type' => 'json']);

// Assert that the Grammar returns the native JSON type.
$this->assertEquals('json', $grammar->typeJson($column));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests seem to be failing as the SqlServerGrammar@typeJson() method is protected and thus cannot be called here.

You can take a look on how the MySQL Schema Grammar is tested for the UUID fields (Illuminate\Tests\Database\DatabaseMySqlSchemaGrammarTest).

It tests against the Blueprint statements, which should suffice for this case too.

}

public function testItFallsBackToNvarcharMaxForSqlServerPre2025()
{
$connection = m::mock(Connection::class);
$grammar = new SqlServerGrammar($connection);

// CRITICAL: Mock the internal connection method to return an older version.
$connection->shouldReceive('getServerVersion')->once()->andReturn('2019.0.0');
$connection->shouldReceive('getTablePrefix')->andReturn('');

$column = new \Illuminate\Support\Fluent(['name' => 'data', 'type' => 'json']);

// Assert that the Grammar falls back to the old nvarchar(max) type.
$this->assertEquals('nvarchar(max)', $grammar->typeJson($column));
}
}
Loading