[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) <mat@odoo.com>


Co-authored-by: Raphael Collet <rco@odoo.com>
This commit is contained in:
Martin Trigaux
2019-06-11 12:04:08 +00:00
co-authored by Raphael Collet
parent 2843dcfaa0
commit 8f88570ca1
3 changed files with 7 additions and 13 deletions
+1 -1
View File
@@ -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)])
+4 -10
View File
@@ -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:
+2 -2
View File
@@ -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