From 4c9968397b0714bc90a9c94c4673bd3148db4010 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Wed, 12 Jul 2023 12:20:52 +0000 Subject: [PATCH] [IMP] base: avoid invalidation on xmlid updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Splitting the cached revealed a missing cache invaldation. The cache invalidation added a a query in test_related_fields The query can be avoid by not updating the xmlid if it is not useful. closes odoo/odoo#119813 Related: odoo/enterprise#42527 Signed-off-by: Xavier Dollé (xdo) --- odoo/addons/base/models/ir_model.py | 17 +++++++++-------- odoo/addons/base/tests/test_ir_actions.py | 2 +- odoo/modules/registry.py | 1 - 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/odoo/addons/base/models/ir_model.py b/odoo/addons/base/models/ir_model.py index 1e6e37c0d7f..1f531a3b4c4 100644 --- a/odoo/addons/base/models/ir_model.py +++ b/odoo/addons/base/models/ir_model.py @@ -1655,7 +1655,8 @@ class IrModelConstraint(models.Model): return super(IrModelConstraint, self).copy(default) def _reflect_constraint(self, model, conname, type, definition, module, message=None): - """ Reflect the given constraint, and return its corresponding record. + """ Reflect the given constraint, and return its corresponding record + if a record is created or modified; returns ``None`` otherwise. The reflection makes it possible to remove a constraint when its corresponding module is uninstalled. ``type`` is either 'f' or 'u' depending on the constraint being a foreign key or not. @@ -1692,7 +1693,7 @@ class IrModelConstraint(models.Model): write_uid=%s, type=%s, definition=%s, message=%s WHERE id=%s""" cr.execute(query, (self.env.uid, type, definition, Json({'en_US': message}), cons_id)) - return self.browse(cons_id) + return self.browse(cons_id) def _reflect_constraints(self, model_names): """ Reflect the SQL constraints of the given models. """ @@ -1720,8 +1721,8 @@ class IrModelConstraint(models.Model): if record: xml_id = '%s.constraint_%s' % (module, conname) data_list.append(dict(xml_id=xml_id, record=record)) - - self.env['ir.model.data']._update_xmlids(data_list) + if data_list: + self.env['ir.model.data']._update_xmlids(data_list) class IrModelRelation(models.Model): @@ -2007,7 +2008,7 @@ class IrModelData(models.Model): # NEW V8 API @api.model - @tools.ormcache('xmlid', cache='xmlid') + @tools.ormcache('xmlid') def _xmlid_lookup(self, xmlid: str) -> tuple: """Low level xmlid lookup Return (id, res_model, res_id) or raise ValueError if not found @@ -2055,12 +2056,12 @@ class IrModelData(models.Model): return super().copy(default) def write(self, values): - self.env.registry.clear_cache('xmlid') # _xmlid_lookup + self.env.registry.clear_cache() # _xmlid_lookup return super().write(values) def unlink(self): """ Regular unlink method, but make sure to clear the caches. """ - self.env.registry.clear_cache('xmlid') # _xmlid_lookup + self.env.registry.clear_cache() # _xmlid_lookup return super(IrModelData, self).unlink() def _lookup_xmlids(self, xml_ids, model): @@ -2112,7 +2113,7 @@ class IrModelData(models.Model): query = self._build_update_xmlids_query(sub_rows, update) try: self.env.cr.execute(query, [arg for row in sub_rows for arg in row]) - self.env.registry.clear_cache('xmlid') + self.env.registry.clear_cache() except Exception: _logger.error("Failed to insert ir_model_data\n%s", "\n".join(str(row) for row in sub_rows)) raise diff --git a/odoo/addons/base/tests/test_ir_actions.py b/odoo/addons/base/tests/test_ir_actions.py index 689a864cc97..f940ce63cfe 100644 --- a/odoo/addons/base/tests/test_ir_actions.py +++ b/odoo/addons/base/tests/test_ir_actions.py @@ -548,7 +548,7 @@ class TestCustomFields(common.TransactionCase): # create a non-computed field, and assert how many queries it takes model_id = self.env['ir.model']._get_id('res.partner') - query_count = 40 + query_count = 38 with self.assertQueryCount(query_count): self.env.registry.clear_cache() self.env['ir.model.fields'].create({ diff --git a/odoo/modules/registry.py b/odoo/modules/registry.py index 55cf2f7f58a..ce5434bbd0e 100644 --- a/odoo/modules/registry.py +++ b/odoo/modules/registry.py @@ -43,7 +43,6 @@ _REGISTRY_CACHES = { 'templates.cached_values': 2048, # arbitrary 'routing': 1024, # 2 entries per website 'routing.rewrites': 8192, # url_rewrite entries - 'xmlid': 1024, }