Skip to content

Commit f131338

Browse files
committed
[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 cc87c6d commit f131338

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/base/tests/test_util.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,49 @@ def test_invert_boolean_field(self):
11311131
initial_repartition[False] += initial_repartition.pop(None, 0)
11321132
self.assertEqual(back_repartition, initial_repartition)
11331133

1134+
@unittest.skipIf(not util.version_gte("saas~17.5"), "Company dependent fields are stored as jsonb since saas~17.5")
1135+
def test_convert_field_to_company_dependent(self):
1136+
cr = self.env.cr
1137+
1138+
partner_model = self.env["ir.model"].search([("model", "=", "res.partner")])
1139+
self.env["ir.model.fields"].create(
1140+
[
1141+
{
1142+
"name": "x_test_cd_1",
1143+
"ttype": "char",
1144+
"model_id": partner_model.id,
1145+
},
1146+
{
1147+
"name": "x_test_cd_2",
1148+
"ttype": "char",
1149+
"model_id": partner_model.id,
1150+
},
1151+
]
1152+
)
1153+
1154+
c1 = self.env["res.company"].create({"name": "Flancrest"})
1155+
1156+
test_partners = self.env["res.partner"].create(
1157+
[
1158+
{"name": "Homer", "x_test_cd_1": "A", "x_test_cd_2": "A", "company_id": c1.id},
1159+
{"name": "Marjorie", "x_test_cd_1": "B", "x_test_cd_2": "B"},
1160+
{"name": "Bartholomew"},
1161+
]
1162+
)
1163+
test_partners.invalidate_recordset(["x_test_cd_1", "x_test_cd_2"])
1164+
1165+
# Using company_id as default, only records with company set are updated
1166+
util.make_field_company_dependent(cr, "res.partner", "x_test_cd_1", "char")
1167+
self.assertEqual(test_partners[0].x_test_cd_1, {str(c1.id): "A"})
1168+
self.assertFalse(test_partners[1].x_test_cd_1)
1169+
self.assertFalse(test_partners[2].x_test_cd_1)
1170+
1171+
# Ignoring company field and composing value for all companies
1172+
util.make_field_company_dependent(cr, "res.partner", "x_test_cd_2", "char", company_field=False)
1173+
self.assertEqual(test_partners[0].x_test_cd_2, {str(id): "A" for id in self.env["res.company"].search([]).ids})
1174+
self.assertEqual(test_partners[1].x_test_cd_2, {str(id): "B" for id in self.env["res.company"].search([]).ids})
1175+
self.assertFalse(test_partners[2].x_test_cd_2)
1176+
11341177

11351178
class TestHelpers(UnitTestCase):
11361179
def test_model_table_conversion(self):

src/util/fields.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,16 @@ def _convert_field_to_company_dependent(
845845
where_condition = "{0} IS NOT NULL"
846846
else:
847847
where_condition = cr.mogrify("{0} != %s", [default_value]).decode()
848-
where_condition += format_query(cr, " AND {} IS NOT NULL", company_field)
849848

850-
using = format_query(cr, "jsonb_build_object({}, {{0}})", company_field)
849+
if company_field:
850+
where_condition += format_query(cr, " AND {} IS NOT NULL", company_field)
851+
using = format_query(cr, "jsonb_build_object({}, {{0}})", company_field)
852+
else:
853+
using = format_query(
854+
cr,
855+
"(SELECT jsonb_object_agg(c.id::text, {}.{{0}}) FROM res_company c)",
856+
table,
857+
)
851858
alter_column_type(cr, table, field, "jsonb", using=using, where=where_condition)
852859

853860
# delete all old default

0 commit comments

Comments
 (0)