Skip to content

Commit 7d124cc

Browse files
committed
[IMP] util,base: use cursor's psycopg2 objects new name
- The psycopg2 technical objects are now "dundered" inside the Odoo cursors. This is made to avoid using them improperly. So we must adapt the code to use new names. Related to: odoo/odoo#204273 odoo/enterprise#82655 odoo/upgrade#7478
1 parent 8b19705 commit 7d124cc

File tree

8 files changed

+32
-22
lines changed

8 files changed

+32
-22
lines changed

src/base/0.0.0/end-no-respawn-fields.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def migrate(cr, version):
2020
"""
2121
)
2222
execute_values(
23-
cr._obj,
23+
getattr(cr, '_obj__', None) or cr._obj,
2424
"INSERT INTO no_respawn(model, field) VALUES %s",
2525
# fmt:off
2626
[

src/base/tests/test_util.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
_model_of_path,
2929
)
3030
from odoo.addons.base.maintenance.migrations.util.exceptions import MigrationError
31+
from odoo.addons.base.maintenance.migrations.util.pg import cursor_get_connection
3132

3233
USE_ORM_DOMAIN = util.misc.version_gte("saas~18.2")
3334
NOTNOT = () if USE_ORM_DOMAIN else ("!", "!")
@@ -842,7 +843,7 @@ def test_create_column_with_fk(self):
842843
def test_ColumnList(self):
843844
cr = self.env.cr
844845

845-
s = lambda c: c.as_string(cr._cnx)
846+
s = lambda c: c.as_string(cursor_get_connection(cr))
846847

847848
columns = util.ColumnList(["a", "A"], ['"a"', '"A"'])
848849
self.assertEqual(len(columns), 2)

src/testing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from . import util
3030
from .util import json
31+
from .util.pg import cursor_get_connection
3132

3233
_logger = logging.getLogger(__name__)
3334

@@ -142,7 +143,7 @@ def _set_value(self, key, value):
142143
ON CONFLICT (key) DO UPDATE SET value=EXCLUDED.value
143144
""".format(DATA_TABLE)
144145
self._data_table_cr.execute(query, (key, value))
145-
self._data_table_cr._cnx.commit()
146+
cursor_get_connection(self._data_table_cr).commit()
146147

147148
def _get_value(self, key):
148149
self._init_db()
@@ -169,7 +170,7 @@ def _init_db(self):
169170
value JSONB NOT NULL
170171
)""".format(DATA_TABLE)
171172
self._data_table_cr.execute(query)
172-
self._data_table_cr._cnx.commit()
173+
cursor_get_connection(self._data_table_cr).commit()
173174
UpgradeCommon.__initialized = True
174175

175176
def _setup_registry(self):
@@ -355,7 +356,7 @@ def setUp(self):
355356

356357
def commit(self):
357358
if self.dbname == config["log_db"].split("/")[-1]:
358-
self._cnx.commit()
359+
cursor_get_connection(self).commit()
359360
else:
360361
raise RuntimeError("Commit is forbidden in integrity cases")
361362

src/util/fields.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def make_index_name(table_name, column_name):
5353
alter_column_type,
5454
column_exists,
5555
column_type,
56+
cursor_get_connection,
5657
explode_execute,
5758
explode_query_range,
5859
format_query,
@@ -898,7 +899,7 @@ def convert_binary_field_to_attachment(cr, model, field, encoded=True, name_fiel
898899
[count] = cr.fetchone()
899900

900901
A = env(cr)["ir.attachment"]
901-
iter_cur = cr._cnx.cursor("fetch_binary")
902+
iter_cur = cursor_get_connection(cr).cursor("fetch_binary")
902903
iter_cur.itersize = 1
903904
iter_cur.execute(
904905
format_query(
@@ -1484,7 +1485,7 @@ def update_server_actions_fields(cr, src_model, dst_model=None, fields_mapping=N
14841485
)
14851486
else:
14861487
psycopg2.extras.execute_values(
1487-
cr._obj,
1488+
getattr(cr, '_obj__') or cr._obj,
14881489
"""
14891490
WITH field_ids AS (
14901491
SELECT mf1.id as old_field_id, mf2.id as new_field_id

src/util/inconsistencies.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from .helpers import _validate_model, table_of_model
1111
from .misc import Sentinel, chunks, str2bool
12-
from .pg import format_query, get_value_or_en_translation, target_of
12+
from .pg import cursor_get_connection, format_query, get_value_or_en_translation, target_of
1313
from .report import add_to_migration_reports, get_anchor_link_to_record, html_escape
1414

1515
_logger = logging.getLogger(__name__)
@@ -228,7 +228,7 @@ def verify_uoms(
228228
_validate_model(model)
229229
table = table_of_model(cr, model)
230230

231-
q = lambda s: quote_ident(s, cr._cnx)
231+
q = lambda s: quote_ident(s, cursor_get_connection(cr))
232232

233233
if include_archived_products is FROM_ENV:
234234
include_archived_products = INCLUDE_ARCHIVED_PRODUCTS
@@ -404,7 +404,7 @@ def verify_products(
404404
table = table_of_model(cr, model)
405405
foreign_table = table_of_model(cr, foreign_model)
406406

407-
q = lambda s: quote_ident(s, cr._cnx)
407+
q = lambda s: quote_ident(s, cursor_get_connection(cr))
408408

409409
if include_archived_products is FROM_ENV:
410410
include_archived_products = INCLUDE_ARCHIVED_PRODUCTS

src/util/pg.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def wrap(arg):
217217

218218
args = tuple(wrap(a) for a in args)
219219
kwargs = {k: wrap(v) for k, v in kwargs.items()}
220-
return SQLStr(sql.SQL(query).format(*args, **kwargs).as_string(cr._cnx))
220+
return SQLStr(sql.SQL(query).format(*args, **kwargs).as_string(cursor_get_connection(cr)))
221221

222222

223223
def explode_query(cr, query, alias=None, num_buckets=8, prefix=None):
@@ -557,7 +557,7 @@ def create_column(cr, table, column, definition, **kwargs):
557557
fk = (
558558
sql.SQL("REFERENCES {}(id) ON DELETE {}")
559559
.format(sql.Identifier(fk_table), sql.SQL(on_delete_action))
560-
.as_string(cr._cnx)
560+
.as_string(cursor_get_connection(cr))
561561
)
562562
elif on_delete_action is not no_def:
563563
raise ValueError("`on_delete_action` argument can only be used if `fk_table` argument is set.")
@@ -845,7 +845,7 @@ def get_index_on(cr, table, *columns):
845845
"""
846846
_validate_table(table)
847847

848-
if cr._cnx.server_version >= 90500:
848+
if cursor_get_connection(cr).server_version >= 90500:
849849
position = "array_position(x.indkey, x.unnest_indkey)"
850850
else:
851851
# array_position does not exists prior postgresql 9.5
@@ -980,10 +980,10 @@ class ColumnList(UserList, sql.Composable):
980980
>>> list(columns)
981981
['id', '"field_Yx"']
982982
983-
>>> columns.using(alias="t").as_string(cr._cnx)
983+
>>> columns.using(alias="t").as_string(cursor_get_connection(cr))
984984
'"t"."id", "t"."field_Yx"'
985985
986-
>>> columns.using(leading_comma=True).as_string(cr._cnx)
986+
>>> columns.using(leading_comma=True).as_string(cursor_get_connection(cr))
987987
', "id", "field_Yx"'
988988
989989
>>> util.format_query(cr, "SELECT {} t.name FROM table t", columns.using(alias="t", trailing_comma=True))
@@ -1026,7 +1026,7 @@ def from_unquoted(cls, cr, list_):
10261026
10271027
:param list(str) list_: list of unquoted column names
10281028
"""
1029-
quoted = [quote_ident(c, cr._cnx) for c in list_]
1029+
quoted = [quote_ident(c, cursor_get_connection(cr)) for c in list_]
10301030
return cls(list_, quoted)
10311031

10321032
def using(self, leading_comma=KEEP_CURRENT, trailing_comma=KEEP_CURRENT, alias=KEEP_CURRENT):
@@ -1482,7 +1482,8 @@ def get_m2m_tables(cr, table):
14821482

14831483
class named_cursor(object):
14841484
def __init__(self, cr, itersize=None):
1485-
self._ncr = cr._cnx.cursor("upg_nc_" + uuid.uuid4().hex, withhold=True)
1485+
pgconn = cursor_get_connection(cr)
1486+
self._ncr = pgconn.cursor("upg_nc_" + uuid.uuid4().hex, withhold=True)
14861487
if itersize:
14871488
self._ncr.itersize = itersize
14881489

@@ -1571,3 +1572,9 @@ def create_id_sequence(cr, table, set_as_default=True):
15711572
table=table_sql,
15721573
)
15731574
)
1575+
1576+
1577+
def cursor_get_connection(cursor):
1578+
if hasattr(cursor, '_cnx__'):
1579+
return cursor._cnx__
1580+
return cursor._cnx

src/util/records.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ def replace_record_references_batch(cr, id_mapping, model_src, model_dst=None, r
14761476
ignores.append("ir_model_data")
14771477

14781478
cr.execute("CREATE UNLOGGED TABLE _upgrade_rrr(old int PRIMARY KEY, new int)")
1479-
execute_values(cr, "INSERT INTO _upgrade_rrr (old, new) VALUES %s", id_mapping.items())
1479+
execute_values(getattr(cr, '_obj__', cr), "INSERT INTO _upgrade_rrr (old, new) VALUES %s", id_mapping.items())
14801480

14811481
if model_src == model_dst:
14821482
fk_def = []

src/util/snippets.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .exceptions import MigrationError
1616
from .helpers import table_of_model
1717
from .misc import import_script, log_progress
18-
from .pg import column_exists, column_type, get_max_workers, table_exists
18+
from .pg import column_exists, column_type, cursor_get_connection, get_max_workers, table_exists
1919

2020
_logger = logging.getLogger(__name__)
2121
utf8_parser = html.HTMLParser(encoding="utf-8")
@@ -44,7 +44,7 @@ def add_snippet_names(cr, table, column, snippets, select_query):
4444
it = log_progress(cr.fetchall(), _logger, qualifier="rows", size=cr.rowcount, log_hundred_percent=True)
4545

4646
def quote(ident):
47-
return quote_ident(ident, cr._cnx)
47+
return quote_ident(ident, cursor_get_connection(cr))
4848

4949
for res_id, regex_matches, arch in it:
5050
regex_matches = [match[0] for match in regex_matches] # noqa: PLW2901
@@ -88,7 +88,7 @@ def get_html_fields(cr):
8888
# yield (table, column) of stored html fields (that needs snippets updates)
8989
for table, columns in html_fields(cr):
9090
for column in columns:
91-
yield table, quote_ident(column, cr._cnx)
91+
yield table, quote_ident(column, cursor_get_connection(cr))
9292

9393

9494
def html_fields(cr):
@@ -316,7 +316,7 @@ def convert_html_columns(cr, table, columns, converter_callback, where_column="I
316316

317317
def determine_chunk_limit_ids(cr, table, column_arr, where):
318318
bytes_per_chunk = 100 * 1024 * 1024
319-
columns = ", ".join(quote_ident(column, cr._cnx) for column in column_arr if column != "id")
319+
columns = ", ".join(quote_ident(column, cursor_get_connection(cr)) for column in column_arr if column != "id")
320320
cr.execute(
321321
f"""
322322
WITH info AS (

0 commit comments

Comments
 (0)