Skip to content

Commit b18e530

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

File tree

25 files changed

+133
-25
lines changed

25 files changed

+133
-25
lines changed

odoo_module_migrate/base_migration_script.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,15 @@ 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-
212-
new_text = tools._replace_in_file(
211+
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, field_renames, "Updated field names of %s" % filename
219+
)
215220

216221
# Display errors if the new content contains some obsolete
217222
# pattern
@@ -260,17 +265,32 @@ def handle_renamed_fields(self, removed_fields):
260265
For now this handler is simple but the idea would be to improve it
261266
with deeper analysis and direct replaces if it is possible and secure.
262267
For that analysis model_name could be used
268+
It also will add to the replaces key of the returned dictionary a key value pair
269+
to be used in _replace_in_file
263270
"""
264-
res = {}
271+
res = {"warnings": {}, "replaces": {}}
272+
res['replaces'] = {}
265273
for model_name, old_field_name, new_field_name, more_info in removed_fields:
274+
# if model_name in res['replaces']:
275+
# res['replaces'][model_name].update({old_field_name: new_field_name,})
276+
# else:
277+
res["replaces"].update(
278+
{
279+
model_name: {old_field_name: new_field_name,}
280+
}
281+
)
266282
msg = "On the model %s, the field %s was renamed to %s.%s" % (
267283
model_name,
268284
old_field_name,
269285
new_field_name,
270286
" %s" % more_info if more_info else "",
271287
)
272-
res[r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name)] = msg
273-
return {"warnings": res}
288+
res["warnings"].update(
289+
{
290+
r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name): msg,
291+
}
292+
)
293+
return res
274294

275295
def handle_deprecated_modules(self, manifest_path, deprecated_modules):
276296
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
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+.*)+)', re.MULTILINE
17+
)
18+
19+
1320
def _get_available_init_version_names():
1421
return [x["init_version_name"] for x in _AVAILABLE_MIGRATION_STEPS]
1522

@@ -65,6 +72,42 @@ def _replace_in_file(file_path, replaces, log_message=False):
6572
return new_text
6673

6774

75+
def _replace_field_names(file_path, replaces, log_message=False):
76+
current_text = _read_content(file_path)
77+
new_text = current_text
78+
# if the field is a python file with _inherit = model_name or <field name="name">model.name</field>
79+
# we try to replace the fields
80+
model = get_model(file_path)
81+
if model in replaces:
82+
model_field_name_replaces = replaces[model]
83+
# This replace is more careful on when and where we do replaces because the idea is to only change field
84+
# names instead of everywhere (i.e. changing move_type to type affects the arch type on xml files)
85+
if '.xml' in file_path:
86+
# replace only between inside the arch tags
87+
xml_data_bytes = new_text.encode('utf-8')
88+
root = etree.fromstring(xml_data_bytes)
89+
archs = root.xpath('.//field[@name="arch"]')
90+
# 3 looped for, not a good look
91+
for arch in archs:
92+
for tag in arch:
93+
for old_term, new_term in model_field_name_replaces.items():
94+
new_tag = etree.fromstring(etree.tostring(tag).decode().replace(old_term, new_term))
95+
arch.replace(tag, new_tag)
96+
new_text = etree.tostring(root, pretty_print=True, xml_declaration=True, encoding="UTF-8").decode()
97+
elif '.py' in file_path:
98+
# replace only inside of classes
99+
for old_term, new_term in model_field_name_replaces.items():
100+
new_text = replace_in_classes(new_text, old_term, new_term)
101+
102+
# Write file if changed
103+
if new_text != current_text:
104+
if not log_message:
105+
log_message = "Changing content of file: %s" % file_path.name
106+
logger.info(log_message)
107+
_write_content(file_path, new_text)
108+
return new_text
109+
110+
68111
def get_files(module_path, extensions):
69112
"""
70113
Returns a list of files with the specified extensions within the module_path.
@@ -79,3 +122,27 @@ def get_files(module_path, extensions):
79122
file_paths.extend(module_dir.rglob(f"*{ext}"))
80123

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

0 commit comments

Comments
 (0)