Skip to content

Commit 95a8561

Browse files
[12.x] Add support for native JSON/JSONB column types in SQLite Schema builder (#54991)
* feat: 🎸 add use_native_json option * test: 💍 add schema tests * fix: 🐛 delete var_dump * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8f63574 commit 95a8561

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ protected function typeEnum(Fluent $column)
891891
*/
892892
protected function typeJson(Fluent $column)
893893
{
894-
return 'text';
894+
return $this->connection->getConfig('use_native_json') ? 'json' : 'text';
895895
}
896896

897897
/**
@@ -902,7 +902,7 @@ protected function typeJson(Fluent $column)
902902
*/
903903
protected function typeJsonb(Fluent $column)
904904
{
905-
return 'text';
905+
return $this->connection->getConfig('use_native_jsonb') ? 'jsonb' : 'text';
906906
}
907907

908908
/**

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,25 @@ public function testAddingJson()
582582
$this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
583583
}
584584

585+
public function testAddingNativeJson()
586+
{
587+
$connection = m::mock(Connection::class);
588+
$connection
589+
->shouldReceive('getTablePrefix')->andReturn('')
590+
->shouldReceive('getConfig')->once()->with('use_native_json')->andReturn(true)
591+
->shouldReceive('getSchemaGrammar')->andReturn($this->getGrammar($connection))
592+
->shouldReceive('getSchemaBuilder')->andReturn($this->getBuilder())
593+
->shouldReceive('getServerVersion')->andReturn('3.35')
594+
->getMock();
595+
596+
$blueprint = new Blueprint($connection, 'users');
597+
$blueprint->json('foo');
598+
$statements = $blueprint->toSql();
599+
600+
$this->assertCount(1, $statements);
601+
$this->assertSame('alter table "users" add column "foo" json not null', $statements[0]);
602+
}
603+
585604
public function testAddingJsonb()
586605
{
587606
$blueprint = new Blueprint($this->getConnection(), 'users');
@@ -592,6 +611,25 @@ public function testAddingJsonb()
592611
$this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
593612
}
594613

614+
public function testAddingNativeJsonb()
615+
{
616+
$connection = m::mock(Connection::class);
617+
$connection
618+
->shouldReceive('getTablePrefix')->andReturn('')
619+
->shouldReceive('getConfig')->once()->with('use_native_jsonb')->andReturn(true)
620+
->shouldReceive('getSchemaGrammar')->andReturn($this->getGrammar($connection))
621+
->shouldReceive('getSchemaBuilder')->andReturn($this->getBuilder())
622+
->shouldReceive('getServerVersion')->andReturn('3.35')
623+
->getMock();
624+
625+
$blueprint = new Blueprint($connection, 'users');
626+
$blueprint->jsonb('foo');
627+
$statements = $blueprint->toSql();
628+
629+
$this->assertCount(1, $statements);
630+
$this->assertSame('alter table "users" add column "foo" jsonb not null', $statements[0]);
631+
}
632+
595633
public function testAddingDate()
596634
{
597635
$blueprint = new Blueprint($this->getConnection(), 'users');

0 commit comments

Comments
 (0)