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
49 changes: 35 additions & 14 deletions src/util/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
from psycopg2.extras import Json

try:
from odoo.modules import get_modules
from odoo.modules import module as odoo_module
from odoo.sql_db import db_connect
except ImportError:
from openerp.modules import get_modules
from openerp.sql_db import db_connect

odoo_module = None
Expand Down Expand Up @@ -1462,6 +1464,7 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
if old_table == new_table or not version_gte("10.0"):
return
ignored_m2ms = set(ignored_m2ms)
standard_modules = set(get_modules()) - {"studio_customization", "__cloc_exclude__"}
for orig_m2m_table in get_m2m_tables(cr, new_table):
if orig_m2m_table in ignored_m2ms:
continue
Expand All @@ -1474,12 +1477,18 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
rename_table(cr, orig_m2m_table, m2m_table, remove_constraints=False)
cr.execute(
"""
UPDATE ir_model_fields
UPDATE ir_model_fields f
SET relation_table = %s
WHERE relation_table = %s
AND state = 'manual'
FROM ir_model_data d
WHERE f.relation_table = %s
AND (
f.state = 'manual'
OR d.model = 'ir.model.fields'
AND d.res_id = f.id
AND d.module NOT IN %s
)
Comment on lines +1484 to +1489
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
AND (
f.state = 'manual'
OR d.model = 'ir.model.fields'
AND d.res_id = f.id
AND d.module NOT IN %s
)
AND ( f.state = 'manual'
OR (d.model = 'ir.model.fields'
AND d.res_id = f.id
AND d.module NOT IN %s)
)

Even with formatted this query looks hard to understand. Wouldn't it be better if we just do a cte?

WITH to_upd (
   SELECT f.id
     FROM ir_model_fields f
     JOIN ir_model_data d
       ON d.model = 'ir.model.fields'
      AND d.res_id = f.id
    WHERE f.relation_table = %s
       AND (  f.state = 'manual'
           OR d.module NOT IN %s)
) UPDATE ...

""",
[m2m_table, orig_m2m_table],
[m2m_table, orig_m2m_table, tuple(standard_modules)],
)
_logger.info("Renamed m2m table %s to %s", orig_m2m_table, m2m_table)
else:
Expand Down Expand Up @@ -1531,23 +1540,35 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):

cr.execute(
"""
UPDATE ir_model_fields
UPDATE ir_model_fields f
SET column1 = %s
WHERE relation_table = %s
AND column1 = %s
AND state = 'manual'
FROM ir_model_data d
WHERE f.relation_table = %s
AND f.column1 = %s
AND (
f.state = 'manual'
OR d.model = 'ir.model.fields'
AND d.res_id = f.id
AND d.module NOT IN %s
)
""",
[new_col, m2m_table, old_col],
[new_col, m2m_table, old_col, tuple(standard_modules)],
)
cr.execute(
"""
UPDATE ir_model_fields
UPDATE ir_model_fields f
SET column2 = %s
WHERE relation_table = %s
AND column2 = %s
AND state = 'manual'
FROM ir_model_data d
WHERE f.relation_table = %s
AND f.column2 = %s
AND (
f.state = 'manual'
OR d.model = 'ir.model.fields'
AND d.res_id = f.id
AND d.module NOT IN %s
)
""",
[new_col, m2m_table, old_col],
[new_col, m2m_table, old_col, tuple(standard_modules)],
)

_logger.info("Renamed m2m column of table %s from %s to %s", m2m_table, old_col, new_col)
Expand Down