Skip to content

[UPG] Base migration upgrade for fields rename. #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 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(
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,32 @@ 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:
model_info = res["replaces"].get(model_name)
if model_info:
model_info.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/odoo/odoo/commit/bdcb3d192be1e01e1141aa09c60027337009a67b"]
- ["stock.inventory.line", "Commit https://github.com/odoo/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"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the commit, it's image_medium (not image) that became image_128 (and also image_small -> image_64)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebalix I don't see image_medium in that commit, could you link the line you are referencing?

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/bea9662632274ea7af669a7e73d66cfed54a2c9f"]
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"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add also image_64? I won't block for that as we are talking about 12.0/13.0 here, up to you.

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/caeb782841fc5a7ad71a196e2c9ee67644ef9074"]
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,14 @@
- ["calendar.event", "mo", "mon", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "tu", "tue", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "we", "wed", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "th", "thu", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "fr", "fri", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "sa", "sat", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.event", "su", "sun", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "mo", "mon", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "tu", "tue", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "we", "wed", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "th", "thu", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "fr", "fri", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "sa", "sat", "Commit https://github.com/odoo/odoo/commit/7fb9c8129606538b004dea4e6ad071eabf6c6fb1"]
- ["calendar.recurrence", "su", "sun", "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"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit seems not the right one ⚠️

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"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name of the file should be product.yaml I guess

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/odoo/odoo/commit/4dbcefb5e5b1878e81fe9be9fe48a785f813334f"]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- ["stock.production.lot", "stock.lot", None]
- ["stock.production.lot", "stock.lot", "Commit https://github.com/odoo/odoo/commit/573ed74c121c1572b3fab6f9553ed7f93f7b3f99"]
90 changes: 90 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,60 @@
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 elem in arch.iterdescendants():
if elem.tag == "field":
for old_term, new_term in model_field_name_replaces.items():
new_elem = etree.fromstring(
etree.tostring(elem)
.decode()
.replace('"' + old_term + '"', '"' + new_term + '"')
)
# Keep the tailing chars to avoid breaking the clean structure of an xml
new_elem.tail = elem.tail
parent = elem.getparent()
if parent is not None:
parent.replace(elem, new_elem)

new_text = etree.tostring(
root, pretty_print=True, xml_declaration=True, encoding="UTF-8"
).decode()
new_text = re.sub(
r"<\?xml([^>]+)\?>",
lambda m: "<?xml" + m.group(1).replace("'", '"') + "?>",
new_text,
count=1,
)
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

Check warning on line 124 in odoo_module_migrate/tools.py

View check run for this annotation

Codecov / codecov/patch

odoo_module_migrate/tools.py#L124

Added line #L124 was not covered by tests
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 +141,31 @@
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:
new_cls_code = re.sub(r"\b%s\b" % old_text, new_text, cls)
code = code.replace(cls, new_cls_code)

return code
12 changes: 12 additions & 0 deletions tests/data_result/module_130_140/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import api, fields, models


class AccountMove(models.Model):
_inherit = 'account.move'

dummy_type = fields.Selection()

@api.depends('move_type')
def _compute_dummy(self):
for move in self:
move.dummy_type = move.move_type
13 changes: 13 additions & 0 deletions tests/data_result/module_130_140/views/account_move.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="account_move_form" model="ir.ui.view">
<field name="name">account.move.form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.account_move_form"/>
<field name="arch" type="xml">
<field name="move_type" position="after">
<field name="dummy_type"/>
</field>
</field>
</record>
</odoo>
12 changes: 12 additions & 0 deletions tests/data_template/module_130/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import api, fields, models


class AccountMove(models.Model):
_inherit = 'account.move'

dummy_type = fields.Selection()

@api.depends('type')
def _compute_dummy(self):
for move in self:
move.dummy_type = move.type
13 changes: 13 additions & 0 deletions tests/data_template/module_130/views/account_move.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="account_move_form" model="ir.ui.view">
<field name="name">account.move.form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.account_move_form" />
<field name="arch" type="xml">
<field name="type" position="after">
<field name="dummy_type"/>
</field>
</field>
</record>
</odoo>