Skip to content

Commit 69ca39d

Browse files
committed
[FIX] util/fields: update usage before changing ir_model_fields
`update_field_usage` does multiple operations, e.g. adapt domains, that depend on correctly identifying the model of a field via `ir_model_fields`. If the rename in `ir_model_fields` is done before updating the usage of a field the latter cannot be done correctly. closes #97 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 5f83f3a commit 69ca39d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/base/tests/test_util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ast
12
import operator
23
import re
34
import threading
@@ -27,6 +28,20 @@ def setUp(self):
2728
super(TestAdaptOneDomain, self).setUp()
2829
self.mock_adapter = mock.Mock()
2930

31+
def test_adapt_renamed_field(self):
32+
domain = [("user_ids.partner_id.user_ids.partner_id", "=", False)]
33+
Filter = self.env["ir.filters"]
34+
filter1 = Filter.create(
35+
{"name": "Test filter for adapt domain", "model_id": "res.partner", "domain": str(domain)}
36+
)
37+
assert domain == ast.literal_eval(filter1.domain)
38+
util.invalidate(Filter)
39+
util.rename_field(self.cr, "res.partner", "user_ids", "renamed_user_ids")
40+
match_domain = [("renamed_user_ids.partner_id.renamed_user_ids.partner_id", "=", False)]
41+
new_domain = ast.literal_eval(filter1.domain)
42+
43+
self.assertEqual(match_domain, new_domain)
44+
3045
def test_change_no_leaf(self):
3146
# testing plan: updata path of a domain where the last element is not changed
3247

src/util/fields.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ def rename_field(cr, model, old, new, update_references=True, domain_adapter=Non
457457
rf = ENVIRON["__renamed_fields"][model]
458458
rf[new] = rf.pop(old, old)
459459

460+
if update_references:
461+
# skip all inherit, they will be handled by the resursive call
462+
update_field_usage(cr, model, old, new, domain_adapter=domain_adapter, skip_inherit="*")
463+
460464
try:
461465
with savepoint(cr):
462466
cr.execute("UPDATE ir_model_fields SET name=%s WHERE model=%s AND name=%s RETURNING id", (new, model, old))
@@ -568,10 +572,6 @@ def rename_field(cr, model, old, new, update_references=True, domain_adapter=Non
568572
old_index_name = make_index_name(table, old)
569573
cr.execute('ALTER INDEX IF EXISTS "{0}" RENAME TO "{1}"'.format(new_index_name, old_index_name))
570574

571-
if update_references:
572-
# skip all inherit, they will be handled by the resursive call
573-
update_field_usage(cr, model, old, new, domain_adapter=domain_adapter, skip_inherit="*")
574-
575575
# rename field on inherits
576576
for inh in for_each_inherit(cr, model, skip_inherit):
577577
rename_field(cr, inh.model, old, new, update_references=update_references, skip_inherit=skip_inherit)

0 commit comments

Comments
 (0)