Skip to content

[12.x] Add support for native JSON/JSONB column types in SQLite Schema builder #54991

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

Merged
merged 4 commits into from
Mar 13, 2025
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
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ protected function typeEnum(Fluent $column)
*/
protected function typeJson(Fluent $column)
{
return 'text';
return $this->connection->getConfig('use_native_json') ? 'json' : 'text';
}

/**
Expand All @@ -902,7 +902,7 @@ protected function typeJson(Fluent $column)
*/
protected function typeJsonb(Fluent $column)
{
return 'text';
return $this->connection->getConfig('use_native_jsonb') ? 'jsonb' : 'text';
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,25 @@ public function testAddingJson()
$this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
}

public function testAddingNativeJson()
{
$connection = m::mock(Connection::class);
$connection
->shouldReceive('getTablePrefix')->andReturn('')
->shouldReceive('getConfig')->once()->with('use_native_json')->andReturn(true)
->shouldReceive('getSchemaGrammar')->andReturn($this->getGrammar($connection))
->shouldReceive('getSchemaBuilder')->andReturn($this->getBuilder())
->shouldReceive('getServerVersion')->andReturn('3.35')
->getMock();

$blueprint = new Blueprint($connection, 'users');
$blueprint->json('foo');
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" json not null', $statements[0]);
}

public function testAddingJsonb()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand All @@ -592,6 +611,25 @@ public function testAddingJsonb()
$this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
}

public function testAddingNativeJsonb()
{
$connection = m::mock(Connection::class);
$connection
->shouldReceive('getTablePrefix')->andReturn('')
->shouldReceive('getConfig')->once()->with('use_native_jsonb')->andReturn(true)
->shouldReceive('getSchemaGrammar')->andReturn($this->getGrammar($connection))
->shouldReceive('getSchemaBuilder')->andReturn($this->getBuilder())
->shouldReceive('getServerVersion')->andReturn('3.35')
->getMock();

$blueprint = new Blueprint($connection, 'users');
$blueprint->jsonb('foo');
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" jsonb not null', $statements[0]);
}

public function testAddingDate()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand Down
Loading