3
3
namespace Illuminate \Tests \Integration \Database \Postgres ;
4
4
5
5
use Illuminate \Database \Schema \Blueprint ;
6
+ use Illuminate \Support \Carbon ;
6
7
use Illuminate \Support \Facades \DB ;
7
8
use Illuminate \Support \Facades \Schema ;
8
9
use PHPUnit \Framework \Attributes \DataProvider ;
@@ -15,32 +16,33 @@ class DatabasePostgresConnectionTest extends PostgresTestCase
15
16
{
16
17
protected function afterRefreshingDatabase ()
17
18
{
18
- if (! Schema::hasTable ('json_table ' )) {
19
- Schema::create ('json_table ' , function (Blueprint $ table ) {
19
+ if (! Schema::hasTable ('pgsql_table ' )) {
20
+ Schema::create ('pgsql_table ' , function (Blueprint $ table ) {
20
21
$ table ->json ('json_col ' )->nullable ();
22
+ $ table ->timestamptz ('timestamptz ' , precision: 6 )->nullable ();
21
23
});
22
24
}
23
25
}
24
26
25
27
protected function destroyDatabaseMigrations ()
26
28
{
27
- Schema::drop ('json_table ' );
29
+ Schema::drop ('pgsql_table ' );
28
30
}
29
31
30
32
#[DataProvider('jsonWhereNullDataProvider ' )]
31
33
public function testJsonWhereNull ($ expected , $ key , array $ value = ['value ' => 123 ])
32
34
{
33
- DB ::table ('json_table ' )->insert (['json_col ' => json_encode ($ value )]);
35
+ DB ::table ('pgsql_table ' )->insert (['json_col ' => json_encode ($ value )]);
34
36
35
- $ this ->assertSame ($ expected , DB ::table ('json_table ' )->whereNull ("json_col-> $ key " )->exists ());
37
+ $ this ->assertSame ($ expected , DB ::table ('pgsql_table ' )->whereNull ("json_col-> $ key " )->exists ());
36
38
}
37
39
38
40
#[DataProvider('jsonWhereNullDataProvider ' )]
39
41
public function testJsonWhereNotNull ($ expected , $ key , array $ value = ['value ' => 123 ])
40
42
{
41
- DB ::table ('json_table ' )->insert (['json_col ' => json_encode ($ value )]);
43
+ DB ::table ('pgsql_table ' )->insert (['json_col ' => json_encode ($ value )]);
42
44
43
- $ this ->assertSame (! $ expected , DB ::table ('json_table ' )->whereNotNull ("json_col-> $ key " )->exists ());
45
+ $ this ->assertSame (! $ expected , DB ::table ('pgsql_table ' )->whereNotNull ("json_col-> $ key " )->exists ());
44
46
}
45
47
46
48
public static function jsonWhereNullDataProvider ()
@@ -71,18 +73,18 @@ public static function jsonWhereNullDataProvider()
71
73
72
74
public function testJsonPathUpdate ()
73
75
{
74
- DB ::table ('json_table ' )->insert ([
76
+ DB ::table ('pgsql_table ' )->insert ([
75
77
['json_col ' => '{"foo":["bar"]} ' ],
76
78
['json_col ' => '{"foo":["baz"]} ' ],
77
79
['json_col ' => '{"foo":[["array"]]} ' ],
78
80
]);
79
81
80
- $ updatedCount = DB ::table ('json_table ' )->where ('json_col->foo[0] ' , 'baz ' )->update ([
82
+ $ updatedCount = DB ::table ('pgsql_table ' )->where ('json_col->foo[0] ' , 'baz ' )->update ([
81
83
'json_col->foo[0] ' => 'updated ' ,
82
84
]);
83
85
$ this ->assertSame (1 , $ updatedCount );
84
86
85
- $ updatedCount = DB ::table ('json_table ' )->where ('json_col->foo[0][0] ' , 'array ' )->update ([
87
+ $ updatedCount = DB ::table ('pgsql_table ' )->where ('json_col->foo[0][0] ' , 'array ' )->update ([
86
88
'json_col->foo[0][0] ' => 'updated ' ,
87
89
]);
88
90
$ this ->assertSame (1 , $ updatedCount );
@@ -91,15 +93,27 @@ public function testJsonPathUpdate()
91
93
#[DataProvider('jsonContainsKeyDataProvider ' )]
92
94
public function testWhereJsonContainsKey ($ count , $ column )
93
95
{
94
- DB ::table ('json_table ' )->insert ([
96
+ DB ::table ('pgsql_table ' )->insert ([
95
97
['json_col ' => '{"foo":{"bar":["baz"]}} ' ],
96
98
['json_col ' => '{"foo":{"bar":false}} ' ],
97
99
['json_col ' => '{"foo":{}} ' ],
98
100
['json_col ' => '{"foo":[{"bar":"bar"},{"baz":"baz"}]} ' ],
99
101
['json_col ' => '{"bar":null} ' ],
100
102
]);
101
103
102
- $ this ->assertSame ($ count , DB ::table ('json_table ' )->whereJsonContainsKey ($ column )->count ());
104
+ $ this ->assertSame ($ count , DB ::table ('pgsql_table ' )->whereJsonContainsKey ($ column )->count ());
105
+ }
106
+
107
+ public function testDateTimeInterfacesAreNotTruncated ()
108
+ {
109
+ $ datetime = Carbon::parse ('2021-01-01 12:34:56.123456 ' , 'America/New_York ' );
110
+
111
+ DB ::table ('pgsql_table ' )->insert ([['timestamptz ' => $ datetime ]]);
112
+
113
+ $ this ->assertSame (
114
+ '2021-01-01 07:34:56.123456+00 ' ,
115
+ DB ::table ('pgsql_table ' )->pluck ('timestamptz ' )->first (),
116
+ );
103
117
}
104
118
105
119
public static function jsonContainsKeyDataProvider ()
0 commit comments