Debian Codegraph @

addons/point_of_sale/upgrades/1.0.2/post-deduplicate-uuids.py

raw symbols SCIP
1 import uuid
2 from psycopg2.extras import Json
3 from odoo.tools import split_every
4
5 def migrate(cr, version):
6 """
7 When UUIDs were introduced for POS [order, order.line, payment] records,
8 they were initially generated on the backend, defined as the default value
9 on the column. The issue with this is that during the upgrade to 18.0+
10 from a version < 18.0, this default value is determined once for the column
11 and then applied to all records, resulting in one UUID, duplicated across
12 all existing POS records.
13
14 This migration fixes the issue by generating a new UUID for each record
15 that has the same UUID as another record. Specifically, it does this for
16 the following tables:
17 - pos_order
18 - pos_order_line
19 - pos_payment
20 """
21 def deduplicate_uuids(table):
22 query = f"""
23 SELECT UNNEST(ARRAY_AGG(id))
24 FROM {table}
25 WHERE uuid IS NOT NULL
26 GROUP BY uuid
27 HAVING COUNT(*) > 1
28 """
29 while True:
30 cr.execute(query)
31 if not cr.rowcount:
32 break
33 ids = tuple(r[0] for r in cr.fetchmany(10000))
34 cr.execute(
35 f"UPDATE {table} SET uuid = (%s::json)->>(id::text) WHERE id IN %s",
36 [Json({id_: str(uuid.uuid4()) for id_ in ids}), ids]
37 )
38
39 deduplicate_uuids("pos_order")
40 deduplicate_uuids("pos_order_line")
41 deduplicate_uuids("pos_payment")