Skip to content

Commit 44574c7

Browse files
author
BT-dherreros
committed
[UPG] Base migration upgrade for fields rename.
1 parent 8f0f1a3 commit 44574c7

File tree

28 files changed

+197
-17
lines changed

28 files changed

+197
-17
lines changed

odoo_module_migrate/base_migration_script.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,17 @@ def process_file(
208208
replaces.update(self._TEXT_REPLACES.get(extension, {}))
209209
replaces.update(renamed_models.get("replaces"))
210210
replaces.update(removed_models.get("replaces"))
211-
212211
new_text = tools._replace_in_file(
213212
absolute_file_path, replaces, "Change file content of %s" % filename
214213
)
214+
field_renames = renamed_fields.get("replaces")
215+
# To be safe we only rename fields on files associated with the current replaces
216+
if field_renames:
217+
new_text = tools._replace_field_names(
218+
absolute_file_path,
219+
field_renames,
220+
"Updated field names of %s" % filename,
221+
)
215222

216223
# Display errors if the new content contains some obsolete
217224
# pattern
@@ -260,17 +267,36 @@ def handle_renamed_fields(self, removed_fields):
260267
For now this handler is simple but the idea would be to improve it
261268
with deeper analysis and direct replaces if it is possible and secure.
262269
For that analysis model_name could be used
270+
It also will add to the replaces key of the returned dictionary a key value pair
271+
to be used in _replace_in_file
263272
"""
264-
res = {}
273+
res = {"warnings": {}, "replaces": {}}
274+
res["replaces"] = {}
265275
for model_name, old_field_name, new_field_name, more_info in removed_fields:
276+
# if model_name in res['replaces']:
277+
# res['replaces'][model_name].update({old_field_name: new_field_name,})
278+
# else:
279+
model_info = res["replaces"].get(model_name)
280+
if model_info:
281+
model_info.update({
282+
old_field_name: new_field_name
283+
})
284+
else:
285+
res["replaces"].update({
286+
model_name: {old_field_name: new_field_name}
287+
})
266288
msg = "On the model %s, the field %s was renamed to %s.%s" % (
267289
model_name,
268290
old_field_name,
269291
new_field_name,
270292
" %s" % more_info if more_info else "",
271293
)
272-
res[r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name)] = msg
273-
return {"warnings": res}
294+
res["warnings"].update(
295+
{
296+
r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name): msg,
297+
}
298+
)
299+
return res
274300

275301
def handle_deprecated_modules(self, manifest_path, deprecated_modules):
276302
current_manifest_text = tools._read_content(manifest_path)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ['mail.template', 'user_signature', 'Commit https://github.com/odoo/odoo/commit/de1743ab']
1+
- ["mail.template", "user_signature", "Commit https://github.com/odoo/odoo/commit/de1743ab"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ['account.move.line', 'exclude_from_invoice_tab', 'Commit https://github.com/odoo/odoo/commit/d8d47f9ff8554f4b39487fd2f13c153c7d6f958d']
1+
- ["account.move.line", "exclude_from_invoice_tab", "Commit https://github.com/odoo/odoo/commit/d8d47f9ff8554f4b39487fd2f13c153c7d6f958d"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ['product.product', 'price', 'Commit https://github.com/odoo/odoo/commit/9e99a9df464d97a74ca320d']
1+
- ["product.product", "price", "Commit https://github.com/odoo/odoo/commit/9e99a9df464d97a74ca320d"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ['hr.expense', 'reference', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
1+
- ["hr.expense", "reference", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ["account.invoice", "Commit https://github.com/odoo/odoo/commit/bc131c0cfb51c953de8ec41fb820c8c7831eefb5"]
2+
- ["account.invoice.line", "Commit https://github.com/odoo/odoo/commit/bc131c0cfb51c953de8ec41fb820c8c7831eefb5"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ["stock.inventory", "Commit https://github.com/brain-tec/odoo/commit/bdcb3d192be1e01e1141aa09c60027337009a67b"]
2+
- ["stock.inventory.line", "Commit https://github.com/brain-tec/odoo/commit/bdcb3d192be1e01e1141aa09c60027337009a67b"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["account.invoice.report", "product_qty", "quantity", "Commit https://github.com/odoo/odoo/commit/bc131c0cfb51c953de8ec41fb820c8c7831eefb5"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ["image.mixin", "image_original", "image_1920", "Commit https://github.com/odoo/odoo/commit/58a2ffa26f1a3b0f9630ce16d11b758d18e20a21"]
2+
- ["mail.channel", "image", "image_128", "Commit https://github.com/odoo/odoo/commit/f0ffbea17381cb117f2eed4ea18a76e2b0f37f00"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["hr.plan.activity.type", "name", "summary", "Commit https://github.com/odoo/odoo/commit/58a2ffa26f1a3b0f9630ce16d11b758d18e20a21"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["product.attribute", "type", "display_type", "Commit https://github.com/odoo/odoo/commit/58a2ffa26f1a3b0f9630ce16d11b758d18e20a21"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["payment.acquirer", "image", "image_128", "Commit https://github.com/odoo/odoo/commit/f0ffbea17381cb117f2eed4ea18a76e2b0f37f00"]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
- ['account.move', 'type', 'move_type', 'Commit https://github.com/odoo/odoo/commit/d675dbaa4c7174591e0e7c1a3caf3e76877312ce']
2-
- ['account.move', 'invoice_payment_state', 'payment_state', 'Commit https://github.com/odoo/odoo/commit/8e4158af810bcf475214946fa64d3b0ce4d3b26d']
3-
- ['account.move', 'invoice_sent', 'is_move_sent', 'Commit https://github.com/odoo/odoo/commit/caeb782841fc5a7ad71a196e2c9ee67644ef9074']
1+
- ["account.move", "type", "move_type", "Commit https://github.com/odoo/odoo/commit/d675dbaa4c7174591e0e7c1a3caf3e76877312ce"]
2+
- ["account.move", "invoice_payment_state", "payment_state", "Commit https://github.com/odoo/odoo/commit/8e4158af810bcf475214946fa64d3b0ce4d3b26d"]
3+
- ["account.move", "invoice_sent", "is_move_sent", "Commit https://github.com/odoo/odoo/commit/caeb782841fc5a7ad71a196e2c9ee67644ef9074"]
4+
- ["account.bank.statement.line", "name", "payment_ref", "Commit https://github.com/odoo/odoo/commit/f3fe2d50d99698eb0261c2309bd63f9f64b3d703"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["res.company", "accrual_default_journal_id", "automatic_entry_default_journal_id", "Commit https://github.com/odoo/odoo/commit/0ea9254c81ea42a7fd9af455f05d70e3ca227460"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- ["calendar.event", "mo", "mon", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
2+
- ["calendar.event", "we", "wed", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
3+
- ["calendar.recurrence", "mo", "mon", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
4+
- ["calendar.recurrence", "we", "wed", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["payment.transaction", "date", "last_state_change", "Commit https://github.com/odoo/odoo/commit/f3fe2d50d99698eb0261c2309bd63f9f64b3d703"]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
- ['account.account', 'user_type_id', 'account_type', 'Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f']
2-
- ['account.account', 'internal_type', 'account_type', 'Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f08be2c04313fd21']
3-
- ['account.move.line', 'analytic_account_id', 'analytic_distribution', 'Commit https://github.com/odoo/odoo/commit/7064c95aa04e5138bb12ae97acfee04ebb67cc0e']
1+
- ["account.account", "user_type_id", "account_type", "Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f"]
2+
- ["account.account", "internal_type", "account_type", "Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f08be2c04313fd21"]
3+
- ["account.move.line", "analytic_account_id", "analytic_distribution", "Commit https://github.com/odoo/odoo/commit/7064c95aa04e5138bb12ae97acfee04ebb67cc0e"]
4+
- ["account.analytic.line", "move_id", "move_line_id", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ['hr.expense', 'analytic_account_id', 'analytic_distribution', 'Commit https://github.com/odoo/odoo/commit/7e3403068fc3fbc40182b3cfeb80e97a9300e8ff']
1+
- ["hr.expense", "analytic_account_id", "analytic_distribution", "Commit https://github.com/odoo/odoo/commit/7e3403068fc3fbc40182b3cfeb80e97a9300e8ff"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["product.supplierinfo", "name", "partner_id", "Commit https://github.com/odoo/odoo/commit/f3fe2d50d99698eb0261c2309bd63f9f64b3d703"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["payment.token", "name", "payment_details", "Commit https://github.com/odoo/odoo/commit/c1b41bcd08ea26032904b3fdd34d743f4342ac62"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ['stock.move.line', 'product_qty', 'reserved_qty', 'Commit https://github.com/odoo/odoo/commit/56e7bcf0cb88acf3d60420569ab3eea9bdb19bdb']
1+
- ["stock.move.line", "product_qty", "reserved_qty", "Commit https://github.com/odoo/odoo/commit/56e7bcf0cb88acf3d60420569ab3eea9bdb19bdb"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- ["utm.campaign", "name", "title", "Commit https://github.com/brain-tec/odoo/commit/4dbcefb5e5b1878e81fe9be9fe48a785f813334f"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- ["stock.production.lot", "stock.lot", None]
1+
- ["stock.production.lot", "stock.lot", "c1b41bcd08ea26032904b3fdd34d743f4342ac62"]

odoo_module_migrate/tools.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
import subprocess
66
import re
77
import pathlib
8+
from lxml import etree
9+
from dataclasses import fields
810

911
from .config import _AVAILABLE_MIGRATION_STEPS
1012
from .log import logger
1113

1214

15+
CLASS_PATTERN = re.compile(
16+
r"(class\s+\w+\s*\(\s*(?:\w+\.)?\w+(?:,\s*(?:\w+\.)?\w+)*\)\s*:\s*(?:\n\s+.*)+)",
17+
re.MULTILINE,
18+
)
19+
20+
1321
def _get_available_init_version_names():
1422
return [x["init_version_name"] for x in _AVAILABLE_MIGRATION_STEPS]
1523

@@ -65,6 +73,53 @@ def _replace_in_file(file_path, replaces, log_message=False):
6573
return new_text
6674

6775

76+
def _replace_field_names(file_path, replaces, log_message=False):
77+
current_text = _read_content(file_path)
78+
new_text = current_text
79+
# if the field is a python file with _inherit = model_name or <field name="name">model.name</field>
80+
# we try to replace the fields
81+
model = get_model(file_path)
82+
if model in replaces:
83+
model_field_name_replaces = replaces[model]
84+
# This replace is more careful on when and where we do replaces because the idea is to only change field
85+
# names instead of everywhere (i.e. changing move_type to type affects the arch type on xml files)
86+
if ".xml" in file_path:
87+
# replace only between inside the arch tags
88+
xml_data_bytes = new_text.encode("utf-8")
89+
root = etree.fromstring(xml_data_bytes)
90+
archs = root.xpath('.//field[@name="arch"]')
91+
# 3 looped for, not a good look
92+
for arch in archs:
93+
for elem in arch.iterdescendants():
94+
if elem.tag == 'field':
95+
for old_term, new_term in model_field_name_replaces.items():
96+
new_elem = etree.fromstring(
97+
etree.tostring(elem).decode().replace("\"" + old_term + "\"", "\"" + new_term + "\"")
98+
)
99+
# Keep the tailing chars to avoid breaking the clean structure of an xml
100+
new_elem.tail = elem.tail
101+
parent = elem.getparent()
102+
if parent is not None:
103+
parent.replace(elem, new_elem)
104+
105+
new_text = etree.tostring(
106+
root, pretty_print=True, xml_declaration=True, encoding="UTF-8"
107+
).decode()
108+
new_text = re.sub( r"<\?xml([^>]+)\?>", lambda m: "<?xml" + m.group(1).replace("'", '"') + "?>", new_text, count=1)
109+
elif ".py" in file_path:
110+
# replace only inside of classes
111+
for old_term, new_term in model_field_name_replaces.items():
112+
new_text = replace_in_classes(new_text, old_term, new_term)
113+
114+
# Write file if changed
115+
if new_text != current_text:
116+
if not log_message:
117+
log_message = "Changing content of file: %s" % file_path.name
118+
logger.info(log_message)
119+
_write_content(file_path, new_text)
120+
return new_text
121+
122+
68123
def get_files(module_path, extensions):
69124
"""
70125
Returns a list of files with the specified extensions within the module_path.
@@ -79,3 +134,31 @@ def get_files(module_path, extensions):
79134
file_paths.extend(module_dir.rglob(f"*{ext}"))
80135

81136
return file_paths
137+
138+
139+
def get_model(absolute_filepath):
140+
model = ""
141+
match = ""
142+
with open(absolute_filepath, "r") as file:
143+
file_content = file.read()
144+
if "xml" in absolute_filepath:
145+
match = re.search(
146+
r"<field name=\"model\">([a-zA-Z0-9_.]+)</field>", file_content
147+
)
148+
elif "py" in absolute_filepath:
149+
match = re.search(r"_inherit\s*=\s*['\"]([^'\"]+)['\"]", file_content)
150+
if match:
151+
model = match.group(1)
152+
return model
153+
154+
155+
def replace_in_classes(code, old_text, new_text):
156+
# Find all classes in the code
157+
classes = CLASS_PATTERN.findall(code)
158+
159+
# Replace old_text with new_text in each class body
160+
for cls in classes:
161+
new_cls_code = re.sub(r"\b%s\b" % old_text, new_text, cls)
162+
code = code.replace(cls, new_cls_code)
163+
164+
return code
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from odoo import api, fields, models
2+
3+
4+
class AccountMove(models.Model):
5+
_inherit = 'account.move'
6+
7+
dummy_type = fields.Selection()
8+
9+
@api.depends('move_type')
10+
def _compute_dummy(self):
11+
for move in self:
12+
move.dummy_type = move.move_type
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<odoo>
3+
<record id="account_move_form" model="ir.ui.view">
4+
<field name="name">account.move.form</field>
5+
<field name="model">account.move</field>
6+
<field name="inherit_id" ref="account.account_move_form"/>
7+
<field name="arch" type="xml">
8+
<field name="move_type" position="after">
9+
<field name="dummy_type"/>
10+
</field>
11+
</field>
12+
</record>
13+
</odoo>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from odoo import api, fields, models
2+
3+
4+
class AccountMove(models.Model):
5+
_inherit = 'account.move'
6+
7+
dummy_type = fields.Selection()
8+
9+
@api.depends('type')
10+
def _compute_dummy(self):
11+
for move in self:
12+
move.dummy_type = move.type
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<odoo>
3+
<record id="account_move_form" model="ir.ui.view">
4+
<field name="name">account.move.form</field>
5+
<field name="model">account.move</field>
6+
<field name="inherit_id" ref="account.account_move_form" />
7+
<field name="arch" type="xml">
8+
<field name="type" position="after">
9+
<field name="dummy_type"/>
10+
</field>
11+
</field>
12+
</record>
13+
</odoo>

0 commit comments

Comments
 (0)