Skip to content

Commit

Permalink
[UPG] Base migration upgrade for fields rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
BT-dherreros committed Dec 13, 2024
1 parent 9014d66 commit 72db965
Show file tree
Hide file tree
Showing 25 changed files with 146 additions and 25 deletions.
34 changes: 29 additions & 5 deletions odoo_module_migrate/base_migration_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,17 @@ def process_file(
replaces.update(self._TEXT_REPLACES.get(extension, {}))
replaces.update(renamed_models.get("replaces"))
replaces.update(removed_models.get("replaces"))

new_text = tools._replace_in_file(
tools._replace_in_file(
absolute_file_path, replaces, "Change file content of %s" % filename
)
field_renames = renamed_fields.get("replaces")
# To be safe we only rename fields on files associated with the current replaces
if field_renames:
new_text = tools._replace_field_names(
absolute_file_path,
field_renames,
"Updated field names of %s" % filename,
)

# Display errors if the new content contains some obsolete
# pattern
Expand Down Expand Up @@ -260,17 +267,34 @@ def handle_renamed_fields(self, removed_fields):
For now this handler is simple but the idea would be to improve it
with deeper analysis and direct replaces if it is possible and secure.
For that analysis model_name could be used
It also will add to the replaces key of the returned dictionary a key value pair
to be used in _replace_in_file
"""
res = {}
res = {"warnings": {}, "replaces": {}}
res["replaces"] = {}
for model_name, old_field_name, new_field_name, more_info in removed_fields:
# if model_name in res['replaces']:
# res['replaces'][model_name].update({old_field_name: new_field_name,})
# else:
res["replaces"].update(
{
model_name: {
old_field_name: new_field_name,
}
}
)
msg = "On the model %s, the field %s was renamed to %s.%s" % (
model_name,
old_field_name,
new_field_name,
" %s" % more_info if more_info else "",
)
res[r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name)] = msg
return {"warnings": res}
res["warnings"].update(
{
r"""(['"]{0}['"]|\.{0}[\s,=])""".format(old_field_name): msg,
}
)
return res

def handle_deprecated_modules(self, manifest_path, deprecated_modules):
current_manifest_text = tools._read_content(manifest_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ['mail.template', 'user_signature', 'Commit https://github.com/odoo/odoo/commit/de1743ab']
- ["mail.template", "user_signature", "Commit https://github.com/odoo/odoo/commit/de1743ab"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ['account.move.line', 'exclude_from_invoice_tab', 'Commit https://github.com/odoo/odoo/commit/d8d47f9ff8554f4b39487fd2f13c153c7d6f958d']
- ["account.move.line", "exclude_from_invoice_tab", "Commit https://github.com/odoo/odoo/commit/d8d47f9ff8554f4b39487fd2f13c153c7d6f958d"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ['product.product', 'price', 'Commit https://github.com/odoo/odoo/commit/9e99a9df464d97a74ca320d']
- ["product.product", "price", "Commit https://github.com/odoo/odoo/commit/9e99a9df464d97a74ca320d"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ['hr.expense', 'reference', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ["hr.expense", "reference", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- ["account.invoice", "Commit https://github.com/odoo/odoo/commit/bc131c0cfb51c953de8ec41fb820c8c7831eefb5"]
- ["account.invoice.line", "Commit https://github.com/odoo/odoo/commit/bc131c0cfb51c953de8ec41fb820c8c7831eefb5"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- ["stock.inventory", "Commit https://github.com/brain-tec/odoo/commit/bdcb3d192be1e01e1141aa09c60027337009a67b"]
- ["stock.inventory.line", "Commit https://github.com/brain-tec/odoo/commit/bdcb3d192be1e01e1141aa09c60027337009a67b"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["account.invoice.report", "product_qty", "quantity", "Commit https://github.com/odoo/odoo/commit/bc131c0cfb51c953de8ec41fb820c8c7831eefb5"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- ["image.mixin", "image_original", "image_1920", "Commit https://github.com/odoo/odoo/commit/58a2ffa26f1a3b0f9630ce16d11b758d18e20a21"]
- ["mail.channel", "image", "image_128", "Commit https://github.com/odoo/odoo/commit/f0ffbea17381cb117f2eed4ea18a76e2b0f37f00"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["hr.plan.activity.type", "name", "summary", "Commit https://github.com/odoo/odoo/commit/58a2ffa26f1a3b0f9630ce16d11b758d18e20a21"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["product.attribute", "type", "display_type", "Commit https://github.com/odoo/odoo/commit/58a2ffa26f1a3b0f9630ce16d11b758d18e20a21"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["payment.acquirer", "image", "image_128", "Commit https://github.com/odoo/odoo/commit/f0ffbea17381cb117f2eed4ea18a76e2b0f37f00"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- ['account.move', 'type', 'move_type', 'Commit https://github.com/odoo/odoo/commit/d675dbaa4c7174591e0e7c1a3caf3e76877312ce']
- ['account.move', 'invoice_payment_state', 'payment_state', 'Commit https://github.com/odoo/odoo/commit/8e4158af810bcf475214946fa64d3b0ce4d3b26d']
- ['account.move', 'invoice_sent', 'is_move_sent', 'Commit https://github.com/odoo/odoo/commit/caeb782841fc5a7ad71a196e2c9ee67644ef9074']
- ["account.move", "type", "move_type", "Commit https://github.com/odoo/odoo/commit/d675dbaa4c7174591e0e7c1a3caf3e76877312ce"]
- ["account.move", "invoice_payment_state", "payment_state", "Commit https://github.com/odoo/odoo/commit/8e4158af810bcf475214946fa64d3b0ce4d3b26d"]
- ["account.move", "invoice_sent", "is_move_sent", "Commit https://github.com/odoo/odoo/commit/caeb782841fc5a7ad71a196e2c9ee67644ef9074"]
- ["account.bank.statement.line", "name", "payment_ref", "Commit https://github.com/odoo/odoo/commit/f3fe2d50d99698eb0261c2309bd63f9f64b3d703"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["res.company", "accrual_default_journal_id", "automatic_entry_default_journal_id", "Commit https://github.com/odoo/odoo/commit/0ea9254c81ea42a7fd9af455f05d70e3ca227460"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- ["calendar.event", "mo", "mon", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "we", "wed", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "mo", "mon", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "we", "wed", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["payment.transaction", "date", "last_state_change", "Commit https://github.com/odoo/odoo/commit/f3fe2d50d99698eb0261c2309bd63f9f64b3d703"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- ['account.account', 'user_type_id', 'account_type', 'Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f']
- ['account.account', 'internal_type', 'account_type', 'Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f08be2c04313fd21']
- ['account.move.line', 'analytic_account_id', 'analytic_distribution', 'Commit https://github.com/odoo/odoo/commit/7064c95aa04e5138bb12ae97acfee04ebb67cc0e']
- ["account.account", "user_type_id", "account_type", "Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f"]
- ["account.account", "internal_type", "account_type", "Commit https://github.com/odoo/odoo/commit/26b2472f4977ccedbb0b5ed5f08be2c04313fd21"]
- ["account.move.line", "analytic_account_id", "analytic_distribution", "Commit https://github.com/odoo/odoo/commit/7064c95aa04e5138bb12ae97acfee04ebb67cc0e"]
- ["account.analytic.line", "move_id", "move_line_id", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ['hr.expense', 'analytic_account_id', 'analytic_distribution', 'Commit https://github.com/odoo/odoo/commit/7e3403068fc3fbc40182b3cfeb80e97a9300e8ff']
- ["hr.expense", "analytic_account_id", "analytic_distribution", "Commit https://github.com/odoo/odoo/commit/7e3403068fc3fbc40182b3cfeb80e97a9300e8ff"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["product.supplierinfo", "name", "partner_id", "Commit https://github.com/odoo/odoo/commit/f3fe2d50d99698eb0261c2309bd63f9f64b3d703"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["payment.token", "name", "payment_details", "Commit https://github.com/odoo/odoo/commit/c1b41bcd08ea26032904b3fdd34d743f4342ac62"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ['stock.move.line', 'product_qty', 'reserved_qty', 'Commit https://github.com/odoo/odoo/commit/56e7bcf0cb88acf3d60420569ab3eea9bdb19bdb']
- ["stock.move.line", "product_qty", "reserved_qty", "Commit https://github.com/odoo/odoo/commit/56e7bcf0cb88acf3d60420569ab3eea9bdb19bdb"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ["utm.campaign", "name", "title", "Commit https://github.com/brain-tec/odoo/commit/4dbcefb5e5b1878e81fe9be9fe48a785f813334f"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- ['hr.expense', 'total_amount', 'total_amount_currency', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ['hr.expense', 'unit_amount', 'price_unit', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ['hr.expense', 'amount_tax', 'tax_amount_currency', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ['hr.expense', 'untaxed_amount', 'untaxed_amount_currency', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ['hr.expense', 'total_amount_company', 'total_amount', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ['hr.expense', 'sheet_is_editable', 'is_editable', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ['hr.expense', 'attachment_number', 'nb_attachment', 'Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a']
- ["hr.expense", "total_amount", "total_amount_currency", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
- ["hr.expense", "unit_amount", "price_unit", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
- ["hr.expense", "amount_tax", "tax_amount_currency", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
- ["hr.expense", "untaxed_amount", "untaxed_amount_currency", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
- ["hr.expense", "total_amount_company", "total_amount", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
- ["hr.expense", "sheet_is_editable", "is_editable", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
- ["hr.expense", "attachment_number", "nb_attachment", "Commit https://github.com/odoo/odoo/commit/68fbdc964038ef6a1cf0d3df773db101ca81794a"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ["stock.production.lot", "stock.lot", None]
- ["stock.production.lot", "stock.lot", "c1b41bcd08ea26032904b3fdd34d743f4342ac62"]
76 changes: 76 additions & 0 deletions odoo_module_migrate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import subprocess
import re
import pathlib
from lxml import etree
from dataclasses import fields

from .config import _AVAILABLE_MIGRATION_STEPS
from .log import logger


CLASS_PATTERN = re.compile(
r"(class\s+\w+\s*\(\s*(?:\w+\.)?\w+(?:,\s*(?:\w+\.)?\w+)*\)\s*:\s*(?:\n\s+.*)+)",
re.MULTILINE,
)


def _get_available_init_version_names():
return [x["init_version_name"] for x in _AVAILABLE_MIGRATION_STEPS]

Expand Down Expand Up @@ -65,6 +73,46 @@ def _replace_in_file(file_path, replaces, log_message=False):
return new_text


def _replace_field_names(file_path, replaces, log_message=False):
current_text = _read_content(file_path)
new_text = current_text
# if the field is a python file with _inherit = model_name or <field name="name">model.name</field>
# we try to replace the fields
model = get_model(file_path)
if model in replaces:
model_field_name_replaces = replaces[model]
# This replace is more careful on when and where we do replaces because the idea is to only change field
# names instead of everywhere (i.e. changing move_type to type affects the arch type on xml files)
if ".xml" in file_path:
# replace only between inside the arch tags
xml_data_bytes = new_text.encode("utf-8")
root = etree.fromstring(xml_data_bytes)
archs = root.xpath('.//field[@name="arch"]')
# 3 looped for, not a good look
for arch in archs:
for tag in arch:
for old_term, new_term in model_field_name_replaces.items():
new_tag = etree.fromstring(
etree.tostring(tag).decode().replace(old_term, new_term)
)
arch.replace(tag, new_tag)
new_text = etree.tostring(
root, pretty_print=True, xml_declaration=True, encoding="UTF-8"
).decode()
elif ".py" in file_path:
# replace only inside of classes
for old_term, new_term in model_field_name_replaces.items():
new_text = replace_in_classes(new_text, old_term, new_term)

# Write file if changed
if new_text != current_text:
if not log_message:
log_message = "Changing content of file: %s" % file_path.name
logger.info(log_message)
_write_content(file_path, new_text)
return new_text


def get_files(module_path, extensions):
"""
Returns a list of files with the specified extensions within the module_path.
Expand All @@ -79,3 +127,31 @@ def get_files(module_path, extensions):
file_paths.extend(module_dir.rglob(f"*{ext}"))

return file_paths


def get_model(absolute_filepath):
model = ""
match = ""
with open(absolute_filepath, "r") as file:
file_content = file.read()
if "xml" in absolute_filepath:
match = re.search(
r"<field name=\"model\">([a-zA-Z0-9_.]+)</field>", file_content
)
elif "py" in absolute_filepath:
match = re.search(r"_inherit\s*=\s*['\"]([^'\"]+)['\"]", file_content)
if match:
model = match.group(1)
return model


def replace_in_classes(code, old_text, new_text):
# Find all classes in the code
classes = CLASS_PATTERN.findall(code)

# Replace old_text with new_text in each class body
for cls in classes:
updated_class = cls.replace(old_text, new_text)
code = code.replace(cls, updated_class)

return code

0 comments on commit 72db965

Please sign in to comment.