Skip to content

Commit 38c262c

Browse files
committed
add sql param converter for psycopg2._json.Json
1 parent fe380a3 commit 38c262c

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ def command_process(db_using):
434434
env = os.environ.copy()
435435
env['UTILMETA_OPERATIONS_DATABASE_ENGINE'] = db_using
436436
server = subprocess.Popen(cmd, env=env, cwd=str(cwd or os.getcwd()))
437-
438437
try:
439438
if port:
440439
import socket

tests/test_7_ops/test_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
)
5959

6060
retry = RetryPlugin(
61-
max_retries=3, max_retries_timeout=15, retry_interval=1
61+
max_retries=5, max_retries_timeout=15, retry_interval=1.5
6262
)
6363

64-
OPS_WAIT = 1.0
64+
OPS_WAIT = 2.0
6565
# add wait to make sure that the operations data are all setup
6666

6767

utilmeta/core/orm/databases/encode.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,24 @@ async def disconnect(self):
229229
@property
230230
def _param_converter(self):
231231
if self.async_engine == 'asyncpg':
232+
json_types = []
232233
try:
233234
from psycopg.types.json import Json, Jsonb
235+
json_types.extend([Json, Jsonb])
234236
except (ModuleNotFoundError, ImportError):
235-
return lambda x: x
236-
237-
json_types = (Json, Jsonb)
238-
239-
def converter(x):
240-
if isinstance(x, json_types):
241-
return json_dumps(x.obj)
242-
return x
243-
return converter
237+
pass
238+
try:
239+
from psycopg2._json import Json
240+
json_types.append(Json)
241+
except (ModuleNotFoundError, ImportError):
242+
pass
243+
json_types = tuple(json_types)
244+
if json_types:
245+
def converter(x):
246+
if isinstance(x, json_types):
247+
return json_dumps(getattr(x, 'obj', getattr(x, 'adapted', None)))
248+
return x
249+
return converter
244250
return lambda x: x
245251

246252
def _parse_sql_params(self, sql: str, params=None):

utilmeta/core/orm/encoder.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44
from psycopg2._json import Json # noqa
55

66
@register_encoder(Json)
7-
def from_iterable(encoder, data):
7+
def from_psycopg2_json(encoder, data):
88
return data.adapted
99

1010
except (ImportError, ModuleNotFoundError):
1111
pass
12+
13+
14+
try:
15+
from psycopg.types.json import Json, Jsonb
16+
17+
@register_encoder(Json, Jsonb)
18+
def from_psycopg_json(encoder, data):
19+
return data.obj
20+
21+
except (ImportError, ModuleNotFoundError):
22+
pass

0 commit comments

Comments
 (0)