Skip to content

Commit 3cc87f9

Browse files
committed
Prevent exceptions because of prefetching non existing fields
1 parent 982ac6a commit 3cc87f9

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author_email = info@initos.com
66
description = Management tool for Odoo installations
77
long_description = file: README.md
88
long_description_content_type = text/markdown
9-
license = Apache License 2.0
9+
license_file = file: LICENSE.md
1010
keywords = odoo environment management
1111
url = https://github.com/initos/dob-lib
1212
classifiers =

src/doblib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "0.20.4"
1+
VERSION = "0.20.5"

src/doblib/env.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def odoo_version(self):
262262
return odoo.release.version_info
263263

264264
@contextmanager
265-
def env(self, db_name, rollback=False):
265+
def env(self, db_name, rollback=False, minimal=False):
266266
"""Create an environment from a registry"""
267267
# pylint: disable=C0415,E0401,W0611
268268
# ruff: noqa: F401
@@ -278,7 +278,19 @@ def env(self, db_name, rollback=False):
278278
# Get all installed modules
279279
reg = Registry(db_name)
280280
with closing(reg.cursor()) as cr:
281-
yield Environment(cr, SUPERUSER_ID, {})
281+
uid = SUPERUSER_ID
282+
env = Environment(cr, uid, {})
283+
284+
if minimal:
285+
# Prevent prefetching fields when creating a context
286+
ctx = (
287+
Environment(cr, uid, {})["res.users"]
288+
.with_context(prefetch_fields=False)
289+
.context_get()
290+
)
291+
env = Environment(cr, uid, ctx)
292+
293+
yield env
282294

283295
if rollback:
284296
cr.rollback()

src/doblib/module.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _run_migration(self, db_name, script_name):
6161
sys.path = path
6262

6363
utils.info(f"Executing {script.__name__.replace('_', ' ')} script")
64-
with self.env(db_name) as env:
64+
with self.env(db_name, minimal=True) as env:
6565
version = utils.Version(
6666
env["ir.config_parameter"].get_param("db_version", False)
6767
)
@@ -85,7 +85,7 @@ def _run_migration_sql(self, db_name, script_name):
8585

8686
def _get_installed_modules(self, db_name):
8787
"""Return the list of modules which are installed"""
88-
with self.env(db_name, False) as env:
88+
with self.env(db_name, False, minimal=True) as env:
8989
domain = [("state", "=", "installed")]
9090
installed = env["ir.module.module"].search(domain).mapped("name")
9191
return set(installed).union(["base"])
@@ -120,7 +120,7 @@ def check_auto_install(self, db_name):
120120
"""Install auto installable modules if the dependencies are installed"""
121121
states = frozenset(("installed", "to install", "to upgrade"))
122122

123-
with self.env(db_name, False) as env:
123+
with self.env(db_name, False, minimal=True) as env:
124124
countries = env["res.company"].search([]).mapped("country_id")
125125

126126
domain = [("state", "=", "uninstalled"), ("auto_install", "=", True)]
@@ -158,7 +158,7 @@ def check_auto_install(self, db_name):
158158

159159
def update_checksums(self, db_name):
160160
"""Only update the module checksums in the database"""
161-
with self.env(db_name, False) as env:
161+
with self.env(db_name, False, minimal=True) as env:
162162
model = env["ir.module.module"]
163163
if hasattr(model, "_save_installed_checksums"):
164164
utils.info("Updating module checksums")
@@ -202,7 +202,7 @@ def update_specific(
202202
def update_changed(self, db_name, blacklist=None):
203203
"""Update only changed modules"""
204204
utils.info("Updating changed modules")
205-
with self.env(db_name, False) as env:
205+
with self.env(db_name, False, minimal=True) as env:
206206
model = env["ir.module.module"]
207207
if hasattr(model, "upgrade_changed_checksum"):
208208
# Initialize `res.company`, `res.partner` and `res.users` to prevent

0 commit comments

Comments
 (0)