|
9 | 9 | from operator import itemgetter
|
10 | 10 |
|
11 | 11 | import lxml
|
| 12 | +from psycopg2 import sql |
12 | 13 | from psycopg2.extras import Json, execute_values
|
13 | 14 |
|
14 | 15 | try:
|
@@ -414,28 +415,33 @@ def remove_records(cr, model, ids):
|
414 | 415 | query = 'DELETE FROM "{}" WHERE {} AND "{}" IN %s'.format(ir.table, ir.model_filter(), ir.res_id)
|
415 | 416 | cr.execute(query, [model, ids])
|
416 | 417 | elif ir.company_dependent_comodel == model:
|
| 418 | + json_path = cr.mogrify( |
| 419 | + "$.* ? ({})".format(" || ".join(["@ == %s"] * len(ids))), |
| 420 | + ids, |
| 421 | + ).decode() |
| 422 | + |
417 | 423 | query = cr.mogrify(
|
418 | 424 | format_query(
|
419 | 425 | cr,
|
420 | 426 | """
|
421 | 427 | UPDATE {table}
|
422 |
| - SET {column} = ( |
423 |
| - SELECT jsonb_object_agg( |
424 |
| - key, |
425 |
| - CASE |
426 |
| - WHEN value::int4 IN %s THEN NULL |
427 |
| - ELSE value::int4 |
428 |
| - END) |
429 |
| - FROM jsonb_each_text({column}) |
430 |
| - ) |
431 |
| - WHERE {column} IS NOT NULL |
432 |
| - AND {column} @? %s |
433 |
| - AND {{parallel_filter}} |
| 428 | + SET {column} = ( |
| 429 | + SELECT jsonb_object_agg( |
| 430 | + key, |
| 431 | + CASE |
| 432 | + WHEN value::int4 IN %s THEN NULL |
| 433 | + ELSE value::int4 |
| 434 | + END) |
| 435 | + FROM jsonb_each_text({column}) |
| 436 | + ) |
| 437 | + WHERE {column} IS NOT NULL |
| 438 | + AND {column} @? {json_path} |
434 | 439 | """,
|
435 | 440 | table=ir.table,
|
436 | 441 | column=ir.res_id,
|
| 442 | + json_path=sql.Literal(json_path), |
437 | 443 | ),
|
438 |
| - [ids, "$.* ? ({})".format(" || ".join(map("@ == {}".format, ids)))], |
| 444 | + [ids], |
439 | 445 | ).decode()
|
440 | 446 | explode_execute(cr, query, table=ir.table)
|
441 | 447 | _rm_refs(cr, model, ids)
|
@@ -1469,23 +1475,28 @@ def replace_record_references_batch(cr, id_mapping, model_src, model_dst=None, r
|
1469 | 1475 | if ir.company_dependent_comodel:
|
1470 | 1476 | if ir.company_dependent_comodel == model_src:
|
1471 | 1477 | assert model_src == model_dst
|
| 1478 | + json_path = cr.mogrify( |
| 1479 | + "$.* ? ({})".format(" || ".join(["@ == %s"] * len(id_mapping))), |
| 1480 | + list(id_mapping), |
| 1481 | + ).decode() |
| 1482 | + |
1472 | 1483 | query = cr.mogrify(
|
1473 | 1484 | format_query(
|
1474 | 1485 | cr,
|
1475 | 1486 | """
|
1476 | 1487 | UPDATE {table}
|
1477 | 1488 | SET {column} = (
|
1478 |
| - SELECT jsonb_object_agg(key, COALESCE((%s::jsonb->>value)::int, value::int)) |
| 1489 | + SELECT jsonb_object_agg(key, COALESCE(((jsonb_object(%s::text[]))->>value)::int, value::int)) |
1479 | 1490 | FROM jsonb_each_text({column})
|
1480 | 1491 | )
|
1481 | 1492 | WHERE {column} IS NOT NULL
|
1482 |
| - AND {column} @? %s |
1483 |
| - AND {{parallel_filter}} |
| 1493 | + AND {column} @? {json_path} |
1484 | 1494 | """,
|
1485 | 1495 | table=ir.table,
|
1486 | 1496 | column=ir.res_id,
|
| 1497 | + json_path=sql.Literal(json_path), |
1487 | 1498 | ),
|
1488 |
| - [Json(id_mapping), "$.* ? ({})".format(" || ".join(map("@ == {}".format, id_mapping)))], |
| 1499 | + [list(map(list, id_mapping.items()))], |
1489 | 1500 | ).decode()
|
1490 | 1501 | explode_execute(cr, query, table=ir.table)
|
1491 | 1502 | continue
|
|
0 commit comments