Skip to content

Commit d978d47

Browse files
committed
[IMP] util.update_record_from_xml
Sometimes records get a field removed from their XML declaration. This creates a divergence between new dbs and upgraded ones. The former will be initialised with `NULL`/default values, whereas the latter will retain their current one. This is usually handled in dedicated upgrade scripts with simple queries un/setting the necessary columns. However, in some cases, these XML changes are backported/noticed too late and need to be addressed in multiple versions. That may create the pressure to target future versions, which creates a hidden coupling between the xml declaration and the query, prone to turn into a bug. It is one such example[^1] that inspired this PR. Here `update_record_from_xml` is adapted to unset fields missing from the xml declaration, if passed explicitely via the `fields` kwarg. This enables us to leverage the foreward compatibility of `update_record_from_xml` in the scenario described above, preventing the associated potential bugs. [^1]: odoo/upgrade#9512
1 parent b0fb61b commit d978d47

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/util/records.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,8 @@ def update_record_from_xml(
10391039
should also be updated.
10401040
:param set(str) or None fields: optional list of fields to include in the XML declaration.
10411041
If set, all other fields will be ignored. When set, record
1042-
won't be created if missing.
1042+
won't be created if missing and all fields not found in
1043+
the XML declaration will be set to NULL.
10431044
10441045
.. warning::
10451046
This functions uses the ORM, therefore it can only be used after **all** models
@@ -1147,9 +1148,13 @@ def add_ref(ref):
11471148
found = True
11481149
parent = node.getparent()
11491150
if node.tag == "record" and fields is not None:
1151+
xml_fields = set()
11501152
for fn in node.xpath("./field[@name]"):
11511153
if fn.attrib["name"] not in fields:
11521154
node.remove(fn)
1155+
xml_fields.add(fn.attrib["name"])
1156+
# override requested fields not found in xml
1157+
node.extend(lxml.builder.E.field(name=f, eval="False") for f in fields if f not in xml_fields)
11531158
new_root[0].append(node)
11541159

11551160
if node.tag == "menuitem" and parent.tag == "menuitem" and "parent_id" not in node.attrib:

0 commit comments

Comments
 (0)