Skip to content

Commit 8372b21

Browse files
filisbitsaj-fuentes
authored andcommitted
[IMP] util/fields: Convert to company dependent without company field
The current implementation of the function `_convert_field_to_company_dependent` requires a field that stores the company value, but this field is not always available. This change aims to allow that case by passing a falsy value as company_field, then it creates a json with the current value for all companies as the new value in the column
1 parent e9c8bde commit 8372b21

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/base/tests/test_util.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,65 @@ def test_change_field_selection_with_default(self):
11561156

11571157
self.assertEqual(new_default, "en_US")
11581158

1159+
@unittest.skipIf(not util.version_gte("saas~17.5"), "Company dependent fields are stored as jsonb since saas~17.5")
1160+
def test_convert_field_to_company_dependent(self):
1161+
cr = self.env.cr
1162+
1163+
partner_model = self.env["ir.model"].search([("model", "=", "res.partner")])
1164+
self.env["ir.model.fields"].create(
1165+
[
1166+
{
1167+
"name": "x_test_cd_1",
1168+
"ttype": "char",
1169+
"model_id": partner_model.id,
1170+
},
1171+
{
1172+
"name": "x_test_cd_2",
1173+
"ttype": "char",
1174+
"model_id": partner_model.id,
1175+
},
1176+
]
1177+
)
1178+
1179+
c1 = self.env["res.company"].create({"name": "Flancrest"})
1180+
c2 = self.env["res.company"].create({"name": "Flancrest2"})
1181+
1182+
test_partners = self.env["res.partner"].create(
1183+
[
1184+
{"name": "Homer", "x_test_cd_1": "A", "x_test_cd_2": "A", "company_id": c1.id},
1185+
{"name": "Marjorie", "x_test_cd_1": "B", "x_test_cd_2": "B"},
1186+
{"name": "Bartholomew"},
1187+
]
1188+
)
1189+
test_partners.invalidate_recordset(["x_test_cd_1", "x_test_cd_2"])
1190+
1191+
# Using company_id as default, only records with company set are updated
1192+
util.make_field_company_dependent(cr, "res.partner", "x_test_cd_1", "char")
1193+
util.make_field_company_dependent(cr, "res.partner", "x_test_cd_2", "char", company_field=False)
1194+
1195+
# make the ORM re-read the info about these manual fields from the DB
1196+
setup_models = (
1197+
self.registry.setup_models if hasattr(self.registry, "setup_models") else self.registry._setup_models__
1198+
)
1199+
args = (["res.partner"],) if util.version_gte("saas~18.4") else ()
1200+
setup_models(cr, *args)
1201+
1202+
test_partners_c1 = test_partners.with_company(c1.id)
1203+
self.assertEqual(test_partners_c1[0].x_test_cd_1, "A")
1204+
self.assertFalse(test_partners_c1[1].x_test_cd_1)
1205+
self.assertFalse(test_partners_c1[2].x_test_cd_1)
1206+
self.assertEqual(test_partners_c1[0].x_test_cd_2, "A")
1207+
self.assertEqual(test_partners_c1[1].x_test_cd_2, "B")
1208+
self.assertFalse(test_partners_c1[2].x_test_cd_2)
1209+
1210+
test_partners_c2 = test_partners.with_company(c2.id)
1211+
self.assertFalse(test_partners_c2[0].x_test_cd_1)
1212+
self.assertFalse(test_partners_c2[1].x_test_cd_1)
1213+
self.assertFalse(test_partners_c2[2].x_test_cd_1)
1214+
self.assertEqual(test_partners_c2[0].x_test_cd_2, "A")
1215+
self.assertEqual(test_partners_c2[1].x_test_cd_2, "B")
1216+
self.assertFalse(test_partners_c2[2].x_test_cd_2)
1217+
11591218

11601219
class TestHelpers(UnitTestCase):
11611220
def test_model_table_conversion(self):

src/util/fields.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,16 @@ def _convert_field_to_company_dependent(
881881
where_condition = "{0} IS NOT NULL"
882882
else:
883883
where_condition = cr.mogrify("{0} != %s", [default_value]).decode()
884-
where_condition += format_query(cr, " AND {} IS NOT NULL", company_field)
885884

886-
using = format_query(cr, "jsonb_build_object({}, {{0}})", company_field)
885+
if company_field:
886+
where_condition += format_query(cr, " AND {} IS NOT NULL", company_field)
887+
using = format_query(cr, "jsonb_build_object({}, {{0}})", company_field)
888+
else:
889+
using = format_query(
890+
cr,
891+
"(SELECT jsonb_object_agg(c.id::text, {}.{{0}}) FROM res_company c)",
892+
table,
893+
)
887894
alter_column_type(cr, table, field, "jsonb", using=using, where=where_condition)
888895

889896
# delete all old default

0 commit comments

Comments
 (0)