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

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
test: add postgres date format test
  • Loading branch information
calebdw committed May 13, 2024
commit e5373fb839373b160c403443f62ed13239155ced
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