Skip to content

[12.x] fix: update postgres grammar date format and time precision #51363

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

Closed
wants to merge 3 commits into from
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
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,16 @@ public function compileDelete(Builder $query)
return parent::compileDelete($query);
}

/**
* Get the format for database stored dates.
*
* @return string
*/
public function getDateFormat()
{
return 'Y-m-d H:i:s.uP';
}

/**
* Compile a delete statement with joins or limit into SQL.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class PostgresBuilder extends Builder
parseSearchPath as baseParseSearchPath;
}

/**
* The default time precision for migrations.
*/
public static ?int $defaultTimePrecision = 6;

/**
* Create a database in the schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Database\Postgres;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -15,32 +16,33 @@ class DatabasePostgresConnectionTest extends PostgresTestCase
{
protected function afterRefreshingDatabase()
{
if (! Schema::hasTable('json_table')) {
Schema::create('json_table', function (Blueprint $table) {
if (! Schema::hasTable('pgsql_table')) {
Schema::create('pgsql_table', function (Blueprint $table) {
$table->json('json_col')->nullable();
$table->timestamptz('timestamptz', precision: 6)->nullable();
});
}
}

protected function destroyDatabaseMigrations()
{
Schema::drop('json_table');
Schema::drop('pgsql_table');
}

#[DataProvider('jsonWhereNullDataProvider')]
public function testJsonWhereNull($expected, $key, array $value = ['value' => 123])
{
DB::table('json_table')->insert(['json_col' => json_encode($value)]);
DB::table('pgsql_table')->insert(['json_col' => json_encode($value)]);

$this->assertSame($expected, DB::table('json_table')->whereNull("json_col->$key")->exists());
$this->assertSame($expected, DB::table('pgsql_table')->whereNull("json_col->$key")->exists());
}

#[DataProvider('jsonWhereNullDataProvider')]
public function testJsonWhereNotNull($expected, $key, array $value = ['value' => 123])
{
DB::table('json_table')->insert(['json_col' => json_encode($value)]);
DB::table('pgsql_table')->insert(['json_col' => json_encode($value)]);

$this->assertSame(! $expected, DB::table('json_table')->whereNotNull("json_col->$key")->exists());
$this->assertSame(! $expected, DB::table('pgsql_table')->whereNotNull("json_col->$key")->exists());
}

public static function jsonWhereNullDataProvider()
Expand Down Expand Up @@ -71,18 +73,18 @@ public static function jsonWhereNullDataProvider()

public function testJsonPathUpdate()
{
DB::table('json_table')->insert([
DB::table('pgsql_table')->insert([
['json_col' => '{"foo":["bar"]}'],
['json_col' => '{"foo":["baz"]}'],
['json_col' => '{"foo":[["array"]]}'],
]);

$updatedCount = DB::table('json_table')->where('json_col->foo[0]', 'baz')->update([
$updatedCount = DB::table('pgsql_table')->where('json_col->foo[0]', 'baz')->update([
'json_col->foo[0]' => 'updated',
]);
$this->assertSame(1, $updatedCount);

$updatedCount = DB::table('json_table')->where('json_col->foo[0][0]', 'array')->update([
$updatedCount = DB::table('pgsql_table')->where('json_col->foo[0][0]', 'array')->update([
'json_col->foo[0][0]' => 'updated',
]);
$this->assertSame(1, $updatedCount);
Expand All @@ -91,15 +93,27 @@ public function testJsonPathUpdate()
#[DataProvider('jsonContainsKeyDataProvider')]
public function testWhereJsonContainsKey($count, $column)
{
DB::table('json_table')->insert([
DB::table('pgsql_table')->insert([
['json_col' => '{"foo":{"bar":["baz"]}}'],
['json_col' => '{"foo":{"bar":false}}'],
['json_col' => '{"foo":{}}'],
['json_col' => '{"foo":[{"bar":"bar"},{"baz":"baz"}]}'],
['json_col' => '{"bar":null}'],
]);

$this->assertSame($count, DB::table('json_table')->whereJsonContainsKey($column)->count());
$this->assertSame($count, DB::table('pgsql_table')->whereJsonContainsKey($column)->count());
}

public function testDateTimeInterfacesAreNotTruncated()
{
$datetime = Carbon::parse('2021-01-01 12:34:56.123456', 'America/New_York');

DB::table('pgsql_table')->insert([['timestamptz' => $datetime]]);

$this->assertSame(
'2021-01-01 17:34:56.123456+00',
DB::table('pgsql_table')->pluck('timestamptz')->first(),
);
}

public static function jsonContainsKeyDataProvider()
Expand Down