# Copyright 2011-2015 Therp BV # Copyright 2016 Opener B.V. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import logging from openupgradelib.openupgrade_tools import table_exists _logger = logging.getLogger(__name__) def log_xml_id(cr, module, xml_id): """ Log xml_ids at load time in the records table. Called from tools/convert.py:xml_import._test_xml_id() # Catcha's - The module needs to be loaded with 'init', or the calling method won't be called. This can be brought about by installing the module or updating the 'state' field of the module to 'to install' or call the server with '--init ' and the database argument. - Do you get the right results immediately when installing the module? No, sorry. This method retrieves the model from the ir_model_table, but when the xml id is encountered for the first time, this method is called before the item is present in this table. Therefore, you will not get any meaningful results until the *second* time that you 'init' the module. - The good news is that the upgrade_analysis module that comes with this distribution allows you to deal with all of this with one click on the menu item Settings -> Customizations -> Database Structure -> OpenUpgrade -> Generate Records - You cannot reinitialize the modules in your production database and expect to keep working on it happily ever after. Do not perform this routine on your production database. :param module: The module that contains the xml_id :param xml_id: the xml_id, with or without 'module.' prefix """ if not table_exists(cr, "upgrade_record"): return if "." not in xml_id: xml_id = "{}.{}".format(module, xml_id) cr.execute( "SELECT model FROM ir_model_data " "WHERE module = %s AND name = %s", xml_id.split("."), ) record = cr.fetchone() if not record: _logger.warning("Cannot find xml_id %s" % xml_id) return else: cr.execute( "SELECT id FROM upgrade_record " "WHERE module=%s AND model=%s AND name=%s AND type=%s", (module, record[0], xml_id, "xmlid"), ) if not cr.fetchone(): cr.execute( "INSERT INTO upgrade_record " "(module, model, name, type) values(%s, %s, %s, %s)", (module, record[0], xml_id, "xmlid"), )