Skip to content

Commit ee04109

Browse files
committed
[FIX] pg: match manual m2m table names
The regex for matching m2m table names was not strict in the start and end of the name, it could still match tables with suffix _1 for example. The other issue with the regex is not considering manual names that start with 'x_', the 'x_' prefix was taken as part of the name of the other table instead of keeping it in the beginning of the name. This fix adds a matching group for the 'x_' prefix and ensures the start and end of the regex are strict. It also renames the manual m2m relation in ir_model_fields table, matching the rename done for column names. closes #335 Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
1 parent 46cac4a commit ee04109

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/base/tests/test_util.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,46 @@ def test_create_m2m(self):
10571057
self.assertEqual("res_groups_res_users_rel", auto_generated_m2m_table_name)
10581058
self.assertTrue(util.table_exists(cr, auto_generated_m2m_table_name))
10591059

1060+
def test_rename_m2m(self):
1061+
cr = self.env.cr
1062+
1063+
self.env["ir.model"].create({"model": "x_new.model", "name": "Custom test model"})
1064+
manual_model_id = self.env["ir.model"].create({"model": "x_manual.model", "name": "Manual model"}).id
1065+
1066+
field_regular = self.env["ir.model.fields"].create(
1067+
{
1068+
"name": "x_m2m_field_regular",
1069+
"ttype": "many2many",
1070+
"model_id": manual_model_id,
1071+
"relation": "x_new.model",
1072+
"relation_table": "x_x_manual_model_x_new_model_rel",
1073+
}
1074+
)
1075+
field_custom = self.env["ir.model.fields"].create(
1076+
{
1077+
"name": "x_m2m_field_custom",
1078+
"ttype": "many2many",
1079+
"model_id": manual_model_id,
1080+
"relation": "x_new.model",
1081+
"relation_table": "x_x_manual_model_x_new_model_rel_2",
1082+
}
1083+
)
1084+
old_regular_table = field_regular.relation_table
1085+
old_custom_table = field_custom.relation_table
1086+
1087+
util.pg_rename_table(cr, "x_new_model", "new_special_model")
1088+
util.update_m2m_tables(cr, "x_new_model", "new_special_model")
1089+
util.invalidate(field_regular)
1090+
1091+
new_regular_table = field_regular.relation_table
1092+
self.assertEqual(new_regular_table, "x_new_special_model_x_manual_model_rel")
1093+
self.assertEqual(field_custom.relation_table, old_custom_table)
1094+
self.assertEqual(field_regular.column2, "new_special_model_id")
1095+
self.assertEqual(field_custom.column2, "new_special_model_id")
1096+
self.assertTrue(util.table_exists(cr, new_regular_table))
1097+
self.assertTrue(util.table_exists(cr, old_custom_table))
1098+
self.assertFalse(util.table_exists(cr, old_regular_table))
1099+
10601100

10611101
class TestORM(UnitTestCase):
10621102
def test_create_cron(self):

src/util/pg.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,13 +1411,22 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
14111411
for orig_m2m_table in get_m2m_tables(cr, new_table):
14121412
if orig_m2m_table in ignored_m2ms:
14131413
continue
1414-
m = re.match(r"^(\w+)_{0}_rel|{0}_(\w+)_rel$".format(re.escape(old_table)), orig_m2m_table)
1414+
m = re.match(r"^(x_|)(?:(\w+)_{0}|{0}_(\w+))_rel$".format(re.escape(old_table)), orig_m2m_table)
14151415
if m:
1416-
m2m_table = "{}_{}_rel".format(*sorted([m.group(1) or m.group(2), new_table]))
1416+
m2m_table = "{}{}_{}_rel".format(m.group(1), *sorted([m.group(2) or m.group(3), new_table]))
14171417
# Due to the 63 chars limit in generated constraint names, for long table names the FK
14181418
# constraint is dropped when renaming the table. We need the constraint to correctly
14191419
# identify the FK targets. The FK constraints will be dropped and recreated below.
14201420
rename_table(cr, orig_m2m_table, m2m_table, remove_constraints=False)
1421+
cr.execute(
1422+
"""
1423+
UPDATE ir_model_fields
1424+
SET relation_table = %s
1425+
WHERE relation_table = %s
1426+
AND state = 'manual'
1427+
""",
1428+
[m2m_table, orig_m2m_table],
1429+
)
14211430
_logger.info("Renamed m2m table %s to %s", orig_m2m_table, m2m_table)
14221431
else:
14231432
m2m_table = orig_m2m_table

0 commit comments

Comments
 (0)