Skip to content

Commit 1e86042

Browse files
committed
[IMP] util/inherit.py: leverage the new ir.model.inherit model
Since odoo/odoo@35e22b8, the inheritance tree is reflected into the database. So for upgrade from version 17, we can use this instead of the harcoded tree. Part-of: #2
1 parent 541c4a8 commit 1e86042

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/base/tests/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ def test_model_table_convertion(self):
469469
self.assertEqual(util.model_of_table(cr, table), model)
470470

471471

472+
@unittest.skipIf(
473+
util.version_gte("saas~17.1"),
474+
"Starting Odoo 17, the info being stored in the database, the test can't lie about its base version",
475+
)
472476
class TestInherit(UnitTestCase):
473477
@classmethod
474478
def setUpClass(cls):

src/util/inherit.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,50 @@
33
import operator
44
import os
55

6-
from ._inherit import inheritance_data
76
from .const import ENVIRON
8-
from .misc import parse_version
7+
from .misc import _cached, parse_version, version_gte
98

109
_logger = logging.getLogger(__name__)
1110

1211

12+
if version_gte("saas~17.1"):
13+
from ._inherit import Inherit, frozendict
14+
15+
@_cached
16+
def _get_inheritance_data(cr):
17+
base_version = _get_base_version(cr)[:2] + ("*final",)
18+
cr.execute(
19+
"""
20+
SELECT p.model,
21+
array_agg(m.model ORDER BY i.id),
22+
array_agg(f.name ORDER BY i.id)
23+
FROM ir_model_inherit i
24+
JOIN ir_model m
25+
ON m.id = i.model_id
26+
JOIN ir_model p
27+
ON p.id = i.parent_id
28+
LEFT JOIN ir_model_fields f
29+
ON f.id = i.parent_field_id
30+
GROUP BY p.model
31+
"""
32+
)
33+
return frozendict(
34+
{
35+
parent: [
36+
Inherit(model=model, born=base_version, dead=None, via=via)
37+
for model, via in zip(children, vias, strict=True)
38+
]
39+
for parent, children, vias in cr.fetchall()
40+
}
41+
)
42+
43+
else:
44+
from ._inherit import inheritance_data
45+
46+
def _get_inheritance_data(cr):
47+
return inheritance_data
48+
49+
1350
def _get_base_version(cr):
1451
# base_version is normaly computed in `base/0.0.0/pre-base_version.py` (and symlinks)
1552
# However, if theses scripts are used to upgrade custom modules afterward (like the P.S. do),
@@ -49,7 +86,7 @@ def for_each_inherit(cr, model, skip=(), interval="[)"):
4986
if skip == "*":
5087
return
5188
cmp_ = _version_comparator(cr, interval)
52-
for inh in inheritance_data.get(model, []):
89+
for inh in _get_inheritance_data(cr).get(model, []):
5390
if inh.model in skip:
5491
continue
5592
if cmp_(inh):
@@ -61,7 +98,7 @@ def inherit_parents(cr, model, skip=(), interval="[)"):
6198
return
6299
skip = set(skip)
63100
cmp_ = _version_comparator(cr, interval)
64-
for parent, inhs in inheritance_data.items():
101+
for parent, inhs in _get_inheritance_data(cr).items():
65102
if parent in skip:
66103
continue
67104
for inh in inhs:

0 commit comments

Comments
 (0)