Skip to content

Commit c3b03d8

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 unsetting 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, which can easily turn into a bug. It is one such example[^1] that inspired this PR. The proposed solution is to adapt `update_record_from_xml` to unset fields from the `fields` kwarg and missing from the XML declaration ensuring foreward compatibility. [^1]: odoo/upgrade#9512
1 parent b0fb61b commit c3b03d8

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+
fields_to_find = set(fields)
11501152
for fn in node.xpath("./field[@name]"):
11511153
if fn.attrib["name"] not in fields:
11521154
node.remove(fn)
1155+
else:
1156+
fields_to_find.remove(fn.attrib["name"])
1157+
node.extend(lxml.builder.E.field(name=field_name, eval="False") for field_name in fields_to_find)
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)