From 8f88570ca18061716fd6e246b9d16aa2cbc24f3a Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 23 Apr 2019 12:53:20 +0000 Subject: [PATCH] [FIX] base: proper removal of xmlid after update Context: After a module update, the records present in ir_model_data table but not in self.pool.loaded_xmlids are considered as no longer needed and should be removed. An exception is done with noupdate=true entries. Bug 1: Entries with noupdate=NULL are not considered in the evaluation and are ignored while it may be worth deleting. Bug 2: Some records with their external id being automatically created are not present in self.pool.loaded_xmlids and may get removed after updating a module. By a lucky coincidence, bug 1 make it so that records targeted by bug 2 are ignored and not deleted (the "automagically" created ir.model.data often lack a noupdate value). Fix Bug 1: Use a COALESCE to find both records with noupdate=NULL and noupdate=false Fix Bug 2: Depends on the source of the generated external id: - ir.model: The entries created through _reflect_model were not loaded in self.loaded_xmlids Use the proper ORM method _update_xmlids that correctly populates self.pool.loaded_xmlids - ir.model.category: The categories were generated when the db was initalised, doing SQL was not avoidable. Create the categories in noupdate to avoid it being considered for removal. - ir.property: Are always created in noupdate in data files but in stock_account it was manually created without being in noupdate closes odoo/odoo#32881 Signed-off-by: Martin Trigaux (mat) Co-authored-by: Raphael Collet --- addons/stock_account/__init__.py | 2 +- odoo/addons/base/models/ir_model.py | 14 ++++---------- odoo/modules/db.py | 4 ++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/addons/stock_account/__init__.py b/addons/stock_account/__init__.py index 7a48ab4b36a..853953aa510 100644 --- a/addons/stock_account/__init__.py +++ b/addons/stock_account/__init__.py @@ -91,4 +91,4 @@ def _configure_journals(cr, registry): 'value': 'account.account,'+str(account_id), 'company_id': env.ref('base.main_company').id, } - env['ir.property']._load_records([dict(xml_id=xml_id, values=vals)]) + env['ir.property']._load_records([dict(xml_id=xml_id, values=vals, noupdate=True)]) diff --git a/odoo/addons/base/models/ir_model.py b/odoo/addons/base/models/ir_model.py index 117d3be6d63..02f085f4131 100644 --- a/odoo/addons/base/models/ir_model.py +++ b/odoo/addons/base/models/ir_model.py @@ -280,14 +280,8 @@ class IrModel(models.Model): if model._module == self._context.get('module'): # self._module is the name of the module that last extended self - xmlid = 'model_' + model._name.replace('.', '_') - cr.execute("SELECT * FROM ir_model_data WHERE name=%s AND module=%s", - (xmlid, self._context['module'])) - if not cr.rowcount: - cr.execute(""" INSERT INTO ir_model_data (module, name, model, res_id, date_init, date_update) - VALUES (%s, %s, %s, %s, (now() at time zone 'UTC'), (now() at time zone 'UTC')) """, - (self._context['module'], xmlid, record._name, record.id)) - + xmlid = '%s.model_%s' % (model._module, model._name.replace('.', '_')) + self.env['ir.model.data']._update_xmlids([{'xml_id': xmlid, 'record': record}]) return record @api.model @@ -1656,9 +1650,9 @@ class IrModelData(models.Model): loaded_xmlids = self.pool.loaded_xmlids query = """ SELECT id, module || '.' || name, model, res_id FROM ir_model_data - WHERE module IN %s AND res_id IS NOT NULL AND noupdate=%s ORDER BY id DESC + WHERE module IN %s AND res_id IS NOT NULL AND COALESCE(noupdate, false) != %s ORDER BY id DESC """ - self._cr.execute(query, (tuple(modules), False)) + self._cr.execute(query, (tuple(modules), True)) for (id, xmlid, model, res_id) in self._cr.fetchall(): if xmlid not in loaded_xmlids: if model in self.env: diff --git a/odoo/modules/db.py b/odoo/modules/db.py index 3a432f0fecb..b917718fdb6 100644 --- a/odoo/modules/db.py +++ b/odoo/modules/db.py @@ -108,8 +108,8 @@ def create_categories(cr, categories): (name, parent_id) \ VALUES (%s, %s) RETURNING id', (categories[0], p_id)) c_id = cr.fetchone()[0] - cr.execute('INSERT INTO ir_model_data (module, name, res_id, model) \ - VALUES (%s, %s, %s, %s)', ('base', xml_id, c_id, 'ir.module.category')) + cr.execute('INSERT INTO ir_model_data (module, name, res_id, model, noupdate) \ + VALUES (%s, %s, %s, %s, %s)', ('base', xml_id, c_id, 'ir.module.category', True)) else: c_id = c_id[0] p_id = c_id