Skip to content
Open
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
24 changes: 24 additions & 0 deletions src/util/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ def remove_model(cr, model, drop_table=True, ignore_m2m=()):
if ignore_m2m != "*":
tables = get_m2m_tables(cr, table_of_model(cr, model))
ignore = set(ignore_m2m)
if tables:
cr.execute(
"""
SELECT name, model
FROM ir_model_fields
WHERE relation_table IN %s
AND state = 'manual'
""",
[tuple(tables)],
Copy link
Contributor

Choose a reason for hiding this comment

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

we could also exclude fields for tables that were explicitly ignored, but since this is only for manual fields they will not be excluded in generic scripts, specific scripts can handle moving them before the related model is removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hello @jjmaksoud,sorry but I didn't understand your point :(

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm saying it looks good to me. I was pointing out that the explicitly ignored m2m fields will still be removed if they are manual, but we will have to handle them in specific fixes if they need to be preserved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, If this is good to go. can we move ahead :(

)

if cr.rowcount:
affected_fields = cr.fetchall()
for field, field_model in affected_fields:
remove_field(cr, field_model, field, drop_column=False)
msg = "The following fields have been removed because their related model {} ({}) was removed:\n{}".format(
mod_label, model, "\n".join(" - field {} of model {}".format(*r) for r in affected_fields)
)
add_to_migration_reports(
message=msg,
category="Removed Fields",
format="md",
)

for table_name in tables:
if table_name in ignore:
_logger.info("remove_model(%r): m2m table %r explicitly ignored", model, table_name)
Expand Down