[IMP] upgrade_analysis: log sql_constraints in analysis

pull/2417/head
Miquel Raïch 2021-12-13 18:00:58 +01:00 committed by Stefan Rijnhart
parent 2978a6132e
commit edc31da81c
10 changed files with 84 additions and 4 deletions

View File

@ -387,14 +387,22 @@ def compare_xml_sets(old_records, new_records):
and found["domain"] is False and found["domain"] is False
) )
column["domain"] = False column["domain"] = False
found["definition"] = (
column["definition"]
and column["definition"] != found["definition"]
and "is now '{}' ('{}')".format(
found["definition"], column["definition"]
)
)
column["definition"] = False
column["noupdate_switched"] = False column["noupdate_switched"] = False
found["noupdate_switched"] = column["noupdate"] != found["noupdate"] found["noupdate_switched"] = column["noupdate"] != found["noupdate"]
if match_type != "direct": if match_type != "direct":
matched_records.append(column) matched_records.append(column)
matched_records.append(found) matched_records.append(found)
elif (match_type == "direct" and found["domain"]) or found[ elif (
"noupdate_switched" match_type == "direct" and (found["domain"] or found["definition"])
]: ) or found["noupdate_switched"]:
matched_records.append(found) matched_records.append(found)
return matched_records return matched_records
@ -413,10 +421,12 @@ def compare_xml_sets(old_records, new_records):
for record in old_records: for record in old_records:
record["old"] = True record["old"] = True
record["domain"] = False record["domain"] = False
record["definition"] = False
record["noupdate_switched"] = False record["noupdate_switched"] = False
for record in new_records: for record in new_records:
record["new"] = True record["new"] = True
record["domain"] = False record["domain"] = False
record["definition"] = False
record["noupdate_switched"] = False record["noupdate_switched"] = False
sorted_records = sorted( sorted_records = sorted(
@ -441,6 +451,8 @@ def compare_xml_sets(old_records, new_records):
content = "%(model)s: %(name)s" % entry content = "%(model)s: %(name)s" % entry
if entry["domain"]: if entry["domain"]:
content += " (deleted domain)" content += " (deleted domain)"
if entry["definition"]:
content += " (changed definition: %(definition)s)" % entry
if entry["noupdate"]: if entry["noupdate"]:
content += " (noupdate)" content += " (noupdate)"
if entry["noupdate_switched"]: if entry["noupdate_switched"]:

View File

@ -144,6 +144,7 @@ class UpgradeAnalysis(models.Model):
"prefix", "prefix",
"suffix", "suffix",
"domain", "domain",
"definition",
] ]
local_xml_records = [ local_xml_records = [
{field: record[field] for field in flds} {field: record[field] for field in flds}

View File

@ -49,6 +49,8 @@ class UpgradeRecord(models.Model):
domain = fields.Char(readonly=True) domain = fields.Char(readonly=True)
definition = fields.Char(readonly=True)
prefix = fields.Char(compute="_compute_prefix_and_suffix") prefix = fields.Char(compute="_compute_prefix_and_suffix")
suffix = fields.Char(compute="_compute_prefix_and_suffix") suffix = fields.Char(compute="_compute_prefix_and_suffix")

View File

@ -1,3 +1,4 @@
from . import addons
from . import models from . import models
from . import modules from . import modules
from . import tools from . import tools

View File

@ -0,0 +1 @@
from . import base

View File

@ -0,0 +1 @@
from . import models

View File

@ -0,0 +1 @@
from . import ir_model

View File

@ -0,0 +1,40 @@
from odoo.addons.base.models import ir_model
from ...... import upgrade_log
from .....odoo_patch import OdooPatch
class IrModelConstraintPatch(OdooPatch):
target = ir_model.IrModelConstraint
method_names = ["_reflect_model"]
def _reflect_model(self, model):
"""Reflect the _sql_constraints of the given model."""
def cons_text(txt):
return txt.lower().replace(", ", ",").replace(" (", "(")
# map each constraint on the name of the module where it is defined
constraint_module = {
constraint[0]: cls._module
for cls in reversed(type(model).mro())
if not getattr(cls, "pool", None)
for constraint in getattr(cls, "_local_sql_constraints", ())
}
data_list = []
for (key, definition, message) in model._sql_constraints:
conname = "%s_%s" % (model._table, key)
module = constraint_module.get(key)
record = self._reflect_constraint(
model, conname, "u", cons_text(definition), module, message
)
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)
for data in data_list:
xml_id = data.get("xml_id")
module = xml_id.split(".")[0]
upgrade_log.log_xml_id(self.env.cr, module, xml_id)

View File

@ -165,7 +165,10 @@ def log_model(model, local_registry):
def log_xml_id(cr, module, xml_id): def log_xml_id(cr, module, xml_id):
""" """
Log xml_ids at load time in the records table. Log xml_ids at load time in the records table.
Called from tools/convert.py:xml_import._test_xml_id() Called from:
- tools/convert.py:xml_import._test_xml_id()
- odoo/models.py:BaseModel._convert_records()
- odoo/addons/base/models/ir_model.py:IrModelConstraint._reflect_model()
# Catcha's # Catcha's
- The module needs to be loaded with 'init', or the calling method - The module needs to be loaded with 'init', or the calling method

View File

@ -72,6 +72,24 @@ class GenerateWizard(models.TransientModel):
] ]
) )
# Set constraint definition
self.env.cr.execute(
""" UPDATE upgrade_record our
SET definition = btrim(replace(replace(replace(replace(
imc.definition, chr(9), ' '), chr(10), ' '), ' ', ' '), ' ', ' '))
FROM ir_model_data imd
JOIN ir_model_constraint imc ON imd.res_id = imc.id
WHERE our.type = 'xmlid'
AND imd.model = 'ir.model.constraint'
AND our.model = imd.model
AND our.name = imd.module || '.' || imd.name"""
)
self.env.cache.invalidate(
[
(self.env["upgrade.record"]._fields["definition"], None),
]
)
# Set noupdate property from ir_model_data # Set noupdate property from ir_model_data
self.env.cr.execute( self.env.cr.execute(
""" UPDATE upgrade_record our """ UPDATE upgrade_record our