Skip to content

Commit 6b23df0

Browse files
author
BT-dherreros
committed
[UPG] Base migration upgrade for fields rename.
1 parent 9014d66 commit 6b23df0

File tree

25 files changed

+145
-24
lines changed

25 files changed

+145
-24
lines changed

odoo_module_migrate/base_migration_script.py

Lines changed: 28 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,34 @@ 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+
res["replaces"].update(
280+
{
281+
model_name: {
282+
old_field_name: new_field_name,
283+
}
284+
}
285+
)
266286
msg = "On the model %s, the field %s was renamed to %s.%s" % (
267287
model_name,
268288
old_field_name,
269289
new_field_name,
270290
" %s" % more_info if more_info else "",
271291
)
272-
res[r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name)] = msg
273-
return {"warnings": res}
292+
res["warnings"].update(
293+
{
294+
r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name): msg,
295+
}
296+
)
297+
return res
274298

275299
def handle_deprecated_modules(self, manifest_path, deprecated_modules):
276300
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- ['hr.expense', 'total_amount', 'total_amount_currency', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
2-
- ['hr.expense', 'unit_amount', 'price_unit', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
3-
- ['hr.expense', 'amount_tax', 'tax_amount_currency', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
4-
- ['hr.expense', 'untaxed_amount', 'untaxed_amount_currency', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
5-
- ['hr.expense', 'total_amount_company', 'total_amount', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
6-
- ['hr.expense', 'sheet_is_editable', 'is_editable', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
7-
- ['hr.expense', 'attachment_number', 'nb_attachment', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
1+
- ["hr.expense", "total_amount", "total_amount_currency", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
2+
- ["hr.expense", "unit_amount", "price_unit", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
3+
- ["hr.expense", "amount_tax", "tax_amount_currency", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
4+
- ["hr.expense", "untaxed_amount", "untaxed_amount_currency", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
5+
- ["hr.expense", "total_amount_company", "total_amount", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
6+
- ["hr.expense", "sheet_is_editable", "is_editable", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
7+
- ["hr.expense", "attachment_number", "nb_attachment", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
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: 76 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,46 @@ 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 tag in arch:
94+
for old_term, new_term in model_field_name_replaces.items():
95+
new_tag = etree.fromstring(
96+
etree.tostring(tag).decode().replace(old_term, new_term)
97+
)
98+
arch.replace(tag, new_tag)
99+
new_text = etree.tostring(
100+
root, pretty_print=True, xml_declaration=True, encoding="UTF-8"
101+
).decode()
102+
elif ".py" in file_path:
103+
# replace only inside of classes
104+
for old_term, new_term in model_field_name_replaces.items():
105+
new_text = replace_in_classes(new_text, old_term, new_term)
106+
107+
# Write file if changed
108+
if new_text != current_text:
109+
if not log_message:
110+
log_message = "Changing content of file: %s" % file_path.name
111+
logger.info(log_message)
112+
_write_content(file_path, new_text)
113+
return new_text
114+
115+
68116
def get_files(module_path, extensions):
69117
"""
70118
Returns a list of files with the specified extensions within the module_path.
@@ -79,3 +127,31 @@ def get_files(module_path, extensions):
79127
file_paths.extend(module_dir.rglob(f"*{ext}"))
80128

81129
return file_paths
130+
131+
132+
def get_model(absolute_filepath):
133+
model = ""
134+
match = ""
135+
with open(absolute_filepath, "r") as file:
136+
file_content = file.read()
137+
if "xml" in absolute_filepath:
138+
match = re.search(
139+
r"<field name=\"model\">([a-zA-Z0-9_.]+)</field>", file_content
140+
)
141+
elif "py" in absolute_filepath:
142+
match = re.search(r"_inherit\s*=\s*['\"]([^'\"]+)['\"]", file_content)
143+
if match:
144+
model = match.group(1)
145+
return model
146+
147+
148+
def replace_in_classes(code, old_text, new_text):
149+
# Find all classes in the code
150+
classes = CLASS_PATTERN.findall(code)
151+
152+
# Replace old_text with new_text in each class body
153+
for cls in classes:
154+
updated_class = cls.replace(old_text, new_text)
155+
code = code.replace(cls, updated_class)
156+
157+
return code

0 commit comments

Comments
 (0)