Coverage for adhoc-cicd-odoo-odoo / odoo / modules / loading.py: 64%

352 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-09 18:15 +0000

1# Part of Odoo. See LICENSE file for full copyright and licensing details. 

2 

3""" Modules (also called addons) management. 

4 

5""" 

6from __future__ import annotations 

7 

8import datetime 

9import itertools 

10import logging 

11import sys 

12import time 

13import typing 

14import traceback 

15 

16import odoo.sql_db 

17import odoo.tools.sql 

18import odoo.tools.translate 

19from odoo import api, tools 

20from odoo.tools import OrderedSet 

21from odoo.tools.convert import convert_file, IdRef, ConvertMode as LoadMode 

22 

23from . import db as modules_db 

24from .migration import MigrationManager 

25from .module import adapt_version, initialize_sys_path, load_openerp_module 

26from .module_graph import ModuleGraph 

27from .registry import Registry 

28 

29if typing.TYPE_CHECKING: 

30 from collections.abc import Collection, Iterable 

31 from odoo.api import Environment 

32 from odoo.sql_db import BaseCursor 

33 from odoo.tests.result import OdooTestResult 

34 from .module_graph import ModuleNode 

35 

36 LoadKind = typing.Literal['data', 'demo'] 

37 

38_logger = logging.getLogger(__name__) 

39 

40 

41def load_data(env: Environment, idref: IdRef, mode: LoadMode, kind: LoadKind, package: ModuleNode) -> bool: 

42 """ 

43 noupdate is False, unless it is demo data 

44 

45 :returns: Whether a file was loaded 

46 """ 

47 keys = ('init_xml', 'data') if kind == 'data' else ('demo',) 

48 

49 files: set[str] = set() 

50 for k in keys: 

51 if k == 'init_xml' and package.manifest[k]: 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true

52 _logger.warning("module %s: key 'init_xml' is deprecated in Odoo 19.", package.name) 

53 for filename in package.manifest[k]: 

54 if filename in files: 54 ↛ 55line 54 didn't jump to line 55 because the condition on line 54 was never true

55 _logger.warning("File %s is imported twice in module %s %s", filename, package.name, kind) 

56 files.add(filename) 

57 

58 _logger.info("loading %s/%s", package.name, filename) 

59 convert_file(env, package.name, filename, idref, mode, noupdate=kind == 'demo') 

60 

61 return bool(files) 

62 

63 

64def load_demo(env: Environment, package: ModuleNode, idref: IdRef, mode: LoadMode) -> bool: 

65 """ 

66 Loads demo data for the specified package. 

67 """ 

68 

69 try: 

70 if package.manifest.get('demo') or package.manifest.get('demo_xml'): 

71 _logger.info("Module %s: loading demo", package.name) 

72 with env.cr.savepoint(flush=False): 

73 load_data(env(su=True), idref, mode, kind='demo', package=package) 

74 return True 

75 except Exception: # noqa: BLE001 

76 # If we could not install demo data for this module 

77 _logger.warning( 

78 "Module %s demo data failed to install, installed without demo data", 

79 package.name, exc_info=True) 

80 

81 todo = env.ref('base.demo_failure_todo', raise_if_not_found=False) 

82 Failure = env.get('ir.demo_failure') 

83 if todo and Failure is not None: 

84 todo.state = 'open' 

85 Failure.create({'module_id': package.id, 'error': traceback.format_exc()}) 

86 return False 

87 

88 

89def force_demo(env: Environment) -> None: 

90 """ 

91 Forces the `demo` flag on all modules, and installs demo data for all installed modules. 

92 """ 

93 env.cr.execute('UPDATE ir_module_module SET demo=True') 

94 env.cr.execute( 

95 "SELECT name FROM ir_module_module WHERE state IN ('installed', 'to upgrade', 'to remove')" 

96 ) 

97 module_list = [name for (name,) in env.cr.fetchall()] 

98 graph = ModuleGraph(env.cr, mode='load') 

99 graph.extend(module_list) 

100 

101 for package in graph: 

102 load_demo(env, package, {}, 'init') 

103 

104 env['ir.module.module'].invalidate_model(['demo']) 

105 

106 

107def load_module_graph( 

108 env: Environment, 

109 graph: ModuleGraph, 

110 update_module: bool = False, 

111 report: OdooTestResult | None = None, 

112 models_to_check: OrderedSet[str] | None = None, 

113 install_demo: bool = True, 

114) -> None: 

115 """ Load, upgrade and install not loaded module nodes in the ``graph`` for ``env.registry`` 

116 

117 :param env: 

118 :param graph: graph of module nodes to load 

119 :param update_module: whether to update modules or not 

120 :param report: 

121 :param set models_to_check: 

122 :param install_demo: whether to attempt installing demo data for newly installed modules 

123 """ 

124 if models_to_check is None: 124 ↛ 125line 124 didn't jump to line 125 because the condition on line 124 was never true

125 models_to_check = OrderedSet() 

126 

127 registry = env.registry 

128 assert isinstance(env.cr, odoo.sql_db.Cursor), "Need for a real Cursor to load modules" 

129 migrations = MigrationManager(env.cr, graph) 

130 module_count = len(graph) 

131 _logger.info('loading %d modules...', module_count) 

132 

133 # register, instantiate and initialize models for each modules 

134 t0 = time.time() 

135 loading_extra_query_count = odoo.sql_db.sql_counter 

136 loading_cursor_query_count = env.cr.sql_log_count 

137 

138 models_updated = set() 

139 

140 for index, package in enumerate(graph, 1): 

141 module_name = package.name 

142 module_id = package.id 

143 

144 if module_name in registry._init_modules: 

145 continue 

146 

147 module_t0 = time.time() 

148 module_cursor_query_count = env.cr.sql_log_count 

149 module_extra_query_count = odoo.sql_db.sql_counter 

150 

151 update_operation = ( 

152 'install' if package.state == 'to install' else 

153 'upgrade' if package.state == 'to upgrade' else 

154 'reinit' if module_name in registry._reinit_modules else 

155 None 

156 ) if update_module else None 

157 module_log_level = logging.DEBUG 

158 if update_operation: 158 ↛ 160line 158 didn't jump to line 160 because the condition on line 158 was always true

159 module_log_level = logging.INFO 

160 _logger.log(module_log_level, 'Loading module %s (%d/%d)', module_name, index, module_count) 

161 

162 if update_operation: 162 ↛ 170line 162 didn't jump to line 170 because the condition on line 162 was always true

163 if update_operation == 'upgrade' or module_name in registry._force_upgrade_scripts: 163 ↛ 164line 163 didn't jump to line 164 because the condition on line 163 was never true

164 if package.name != 'base': 

165 registry._setup_models__(env.cr, []) # incremental setup 

166 migrations.migrate_module(package, 'pre') 

167 if package.name != 'base': 

168 env.flush_all() 

169 

170 load_openerp_module(package.name) 

171 

172 if update_operation == 'install': 172 ↛ 179line 172 didn't jump to line 179 because the condition on line 172 was always true

173 py_module = sys.modules['odoo.addons.%s' % (module_name,)] 

174 pre_init = package.manifest.get('pre_init_hook') 

175 if pre_init: 

176 registry._setup_models__(env.cr, []) # incremental setup 

177 getattr(py_module, pre_init)(env) 

178 

179 model_names = registry.load(package) 

180 

181 if update_operation: 181 ↛ 187line 181 didn't jump to line 187 because the condition on line 181 was always true

182 model_names = registry.descendants(model_names, '_inherit', '_inherits') 

183 models_updated |= model_names 

184 models_to_check -= model_names 

185 registry._setup_models__(env.cr, []) # incremental setup 

186 registry.init_models(env.cr, model_names, {'module': package.name}, update_operation == 'install') 

187 elif update_module and package.state != 'to remove': 

188 # The current module has simply been loaded. The models extended by this module 

189 # and for which we updated the schema, must have their schema checked again. 

190 # This is because the extension may have changed the model, 

191 # e.g. adding required=True to an existing field, but the schema has not been 

192 # updated by this module because it's not marked as 'to upgrade/to install'. 

193 model_names = registry.descendants(model_names, '_inherit', '_inherits') 

194 models_to_check |= model_names & models_updated 

195 elif update_module and package.state == 'to remove': 

196 # For all model extented (with _inherit) in the package to uninstall, we need to 

197 # update ir.model / ir.model.fields along side not-null SQL constrains. 

198 models_to_check |= model_names 

199 

200 if update_operation: 200 ↛ 228line 200 didn't jump to line 228 because the condition on line 200 was always true

201 # Can't put this line out of the loop: ir.module.module will be 

202 # registered by init_models() above. 

203 module = env['ir.module.module'].browse(module_id) 

204 module._check() 

205 

206 idref: dict = {} 

207 

208 if update_operation == 'install': 208 ↛ 214line 208 didn't jump to line 214 because the condition on line 208 was always true

209 load_data(env, idref, 'init', kind='data', package=package) 

210 if install_demo and package.demo_installable: 210 ↛ 219line 210 didn't jump to line 219 because the condition on line 210 was always true

211 package.demo = load_demo(env, package, idref, 'init') 

212 else: # 'upgrade' or 'reinit' 

213 # upgrading the module information 

214 module.write(module.get_values_from_terp(package.manifest)) 

215 mode = 'update' if update_operation == 'upgrade' else 'init' 

216 load_data(env, idref, mode, kind='data', package=package) 

217 if package.demo: 

218 package.demo = load_demo(env, package, idref, mode) 

219 env.cr.execute('UPDATE ir_module_module SET demo = %s WHERE id = %s', (package.demo, module_id)) 

220 module.invalidate_model(['demo']) 

221 

222 migrations.migrate_module(package, 'post') 

223 

224 # Update translations for all installed languages 

225 overwrite = tools.config["overwrite_existing_translations"] 

226 module._update_translations(overwrite=overwrite) 

227 

228 if package.name is not None: 228 ↛ 231line 228 didn't jump to line 231 because the condition on line 228 was always true

229 registry._init_modules.add(package.name) 

230 

231 if update_operation: 231 ↛ 267line 231 didn't jump to line 267 because the condition on line 231 was always true

232 if update_operation == 'install': 232 ↛ 236line 232 didn't jump to line 236 because the condition on line 232 was always true

233 post_init = package.manifest.get('post_init_hook') 

234 if post_init: 

235 getattr(py_module, post_init)(env) 

236 elif update_operation == 'upgrade': 

237 # validate the views that have not been checked yet 

238 env['ir.ui.view']._validate_module_views(module_name) 

239 

240 concrete_models = [model for model in model_names if not registry[model]._abstract] 

241 if concrete_models: 

242 env.cr.execute(""" 

243 SELECT model FROM ir_model  

244 WHERE id NOT IN (SELECT DISTINCT model_id FROM ir_model_access) AND model IN %s 

245 """, [tuple(concrete_models)]) 

246 models = [model for [model] in env.cr.fetchall()] 

247 if models: 247 ↛ 248line 247 didn't jump to line 248 because the condition on line 247 was never true

248 lines = [ 

249 f"The models {models} have no access rules in module {module_name}, consider adding some, like:", 

250 "id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink" 

251 ] 

252 for model in models: 

253 xmlid = model.replace('.', '_') 

254 lines.append(f"{module_name}.access_{xmlid},access_{xmlid},{module_name}.model_{xmlid},base.group_user,1,0,0,0") 

255 _logger.warning('\n'.join(lines)) 

256 

257 registry.updated_modules.append(package.name) 

258 

259 ver = adapt_version(package.manifest['version']) 

260 # Set new modules and dependencies 

261 module.write({'state': 'installed', 'latest_version': ver}) 

262 

263 package.state = 'installed' 

264 module.env.flush_all() 

265 module.env.cr.commit() 

266 

267 test_time = 0.0 

268 test_queries = 0 

269 test_results = None 

270 

271 update_from_config = tools.config['update'] or tools.config['init'] or tools.config['reinit'] 

272 if tools.config['test_enable'] and (update_operation or not update_from_config): 272 ↛ 291line 272 didn't jump to line 291 because the condition on line 272 was always true

273 from odoo.tests import loader # noqa: PLC0415 

274 suite = loader.make_suite([module_name], 'at_install') 

275 if suite.countTestCases(): 

276 if not update_operation: 276 ↛ 277line 276 didn't jump to line 277 because the condition on line 276 was never true

277 registry._setup_models__(env.cr, []) # incremental setup 

278 registry.check_null_constraints(env.cr) 

279 # Python tests 

280 tests_t0, tests_q0 = time.time(), odoo.sql_db.sql_counter 

281 test_results = loader.run_suite(suite, global_report=report) 

282 assert report is not None, "Missing report during tests" 

283 report.update(test_results) 

284 test_time = time.time() - tests_t0 

285 test_queries = odoo.sql_db.sql_counter - tests_q0 

286 

287 # tests may have reset the environment 

288 module = env['ir.module.module'].browse(module_id) 

289 

290 

291 extra_queries = odoo.sql_db.sql_counter - module_extra_query_count - test_queries 

292 extras = [] 

293 if test_queries: 

294 extras.append(f'+{test_queries} test') 

295 if extra_queries: 295 ↛ 297line 295 didn't jump to line 297 because the condition on line 295 was always true

296 extras.append(f'+{extra_queries} other') 

297 _logger.log( 

298 module_log_level, "Module %s loaded in %.2fs%s, %s queries%s", 

299 module_name, time.time() - module_t0, 

300 f' (incl. {test_time:.2f}s test)' if test_time else '', 

301 env.cr.sql_log_count - module_cursor_query_count, 

302 f' ({", ".join(extras)})' if extras else '' 

303 ) 

304 if test_results and not test_results.wasSuccessful(): 304 ↛ 305line 304 didn't jump to line 305 because the condition on line 304 was never true

305 _logger.error( 

306 "Module %s: %d failures, %d errors of %d tests", 

307 module_name, test_results.failures_count, test_results.errors_count, 

308 test_results.testsRun 

309 ) 

310 

311 _logger.runbot("%s modules loaded in %.2fs, %s queries (+%s extra)", 

312 len(graph), 

313 time.time() - t0, 

314 env.cr.sql_log_count - loading_cursor_query_count, 

315 odoo.sql_db.sql_counter - loading_extra_query_count) # extra queries: testes, notify, any other closed cursor 

316 

317 

318def _check_module_names(cr: BaseCursor, module_names: Iterable[str]) -> None: 

319 mod_names = set(module_names) 

320 mod_names.discard('all') 

321 if mod_names: 321 ↛ exitline 321 didn't return from function '_check_module_names' because the condition on line 321 was always true

322 cr.execute("SELECT count(id) AS count FROM ir_module_module WHERE name in %s", (tuple(mod_names),)) 

323 row = cr.fetchone() 

324 assert row is not None # for typing 

325 if row[0] != len(mod_names): 325 ↛ 327line 325 didn't jump to line 327 because the condition on line 325 was never true

326 # find out what module name(s) are incorrect: 

327 cr.execute("SELECT name FROM ir_module_module") 

328 incorrect_names = mod_names.difference([x['name'] for x in cr.dictfetchall()]) 

329 _logger.warning('invalid module names, ignored: %s', ", ".join(incorrect_names)) 

330 

331 

332def load_modules( 

333 registry: Registry, 

334 *, 

335 update_module: bool = False, 

336 upgrade_modules: Collection[str] = (), 

337 install_modules: Collection[str] = (), 

338 reinit_modules: Collection[str] = (), 

339 new_db_demo: bool = False, 

340 models_to_check: OrderedSet[str] | None = None, 

341) -> None: 

342 """ Load the modules for a registry object that has just been created. This 

343 function is part of Registry.new() and should not be used anywhere else. 

344 

345 :param registry: The new inited registry object used to load modules. 

346 :param update_module: Whether to update (install, upgrade, or uninstall) modules. Defaults to ``False`` 

347 :param upgrade_modules: A collection of module names to upgrade. 

348 :param install_modules: A collection of module names to install. 

349 :param reinit_modules: A collection of module names to reinitialize. 

350 :param new_db_demo: Whether to install demo data for new database. Defaults to ``False`` 

351 """ 

352 if models_to_check is None: 352 ↛ 355line 352 didn't jump to line 355 because the condition on line 352 was always true

353 models_to_check = OrderedSet() 

354 

355 initialize_sys_path() 

356 

357 with registry.cursor() as cr: 

358 # prevent endless wait for locks on schema changes (during online 

359 # installs) if a concurrent transaction has accessed the table; 

360 # connection settings are automatically reset when the connection is 

361 # borrowed from the pool 

362 cr.execute("SET SESSION lock_timeout = '15s'") 

363 if not modules_db.is_initialized(cr): 363 ↛ 369line 363 didn't jump to line 369 because the condition on line 363 was always true

364 if not update_module: 364 ↛ 365line 364 didn't jump to line 365 because the condition on line 364 was never true

365 _logger.error("Database %s not initialized, you can force it with `-i base`", cr.dbname) 

366 return 

367 _logger.info("Initializing database %s", cr.dbname) 

368 modules_db.initialize(cr) 

369 elif 'base' in reinit_modules: 

370 registry._reinit_modules.add('base') 

371 

372 if 'base' in upgrade_modules: 372 ↛ 373line 372 didn't jump to line 373 because the condition on line 372 was never true

373 cr.execute("update ir_module_module set state=%s where name=%s and state=%s", ('to upgrade', 'base', 'installed')) 

374 

375 # STEP 1: LOAD BASE (must be done before module dependencies can be computed for later steps) 

376 graph = ModuleGraph(cr, mode='update' if update_module else 'load') 

377 graph.extend(['base']) 

378 if not graph: 378 ↛ 379line 378 didn't jump to line 379 because the condition on line 378 was never true

379 _logger.critical('module base cannot be loaded! (hint: verify addons-path)') 

380 raise ImportError('Module `base` cannot be loaded! (hint: verify addons-path)') 

381 if update_module and upgrade_modules: 381 ↛ 382line 381 didn't jump to line 382 because the condition on line 381 was never true

382 for pyfile in tools.config['pre_upgrade_scripts']: 

383 odoo.modules.migration.exec_script(cr, graph['base'].installed_version, pyfile, 'base', 'pre') 

384 

385 if update_module and tools.sql.table_exists(cr, 'ir_model_fields'): 385 ↛ 387line 385 didn't jump to line 387 because the condition on line 385 was never true

386 # determine the fields which are currently translated in the database 

387 cr.execute("SELECT model || '.' || name, translate FROM ir_model_fields WHERE translate IS NOT NULL") 

388 registry._database_translated_fields = dict(cr.fetchall()) 

389 

390 # determine the fields which are currently company dependent in the database 

391 if odoo.tools.sql.column_exists(cr, 'ir_model_fields', 'company_dependent'): 

392 cr.execute("SELECT model || '.' || name FROM ir_model_fields WHERE company_dependent IS TRUE") 

393 registry._database_company_dependent_fields = {row[0] for row in cr.fetchall()} 

394 

395 report = registry._assertion_report 

396 env = api.Environment(cr, api.SUPERUSER_ID, {}) 

397 load_module_graph( 

398 env, 

399 graph, 

400 update_module=update_module, 

401 report=report, 

402 models_to_check=models_to_check, 

403 install_demo=new_db_demo, 

404 ) 

405 

406 load_lang = tools.config._cli_options.pop('load_language', None) 

407 if load_lang or update_module: 407 ↛ 411line 407 didn't jump to line 411 because the condition on line 407 was always true

408 # some base models are used below, so make sure they are set up 

409 registry._setup_models__(cr, []) # incremental setup 

410 

411 if load_lang: 411 ↛ 416line 411 didn't jump to line 416 because the condition on line 411 was always true

412 for lang in load_lang.split(','): 

413 tools.translate.load_language(cr, lang) 

414 

415 # STEP 2: Mark other modules to be loaded/updated 

416 if update_module: 416 ↛ 444line 416 didn't jump to line 444 because the condition on line 416 was always true

417 Module = env['ir.module.module'] 

418 _logger.info('updating modules list') 

419 Module.update_list() 

420 

421 _check_module_names(cr, itertools.chain(install_modules, upgrade_modules)) 

422 

423 if install_modules: 423 ↛ 428line 423 didn't jump to line 428 because the condition on line 423 was always true

424 modules = Module.search([('state', '=', 'uninstalled'), ('name', 'in', tuple(install_modules))]) 

425 if modules: 425 ↛ 428line 425 didn't jump to line 428 because the condition on line 425 was always true

426 modules.button_install() 

427 

428 if upgrade_modules: 428 ↛ 429line 428 didn't jump to line 429 because the condition on line 428 was never true

429 modules = Module.search([('state', 'in', ('installed', 'to upgrade')), ('name', 'in', tuple(upgrade_modules))]) 

430 if modules: 

431 modules.button_upgrade() 

432 

433 if reinit_modules: 433 ↛ 434line 433 didn't jump to line 434 because the condition on line 433 was never true

434 modules = Module.search([('state', 'in', ('installed', 'to upgrade')), ('name', 'in', tuple(reinit_modules))]) 

435 reinit_modules = modules.downstream_dependencies(exclude_states=('uninstalled', 'uninstallable', 'to remove', 'to install')) + modules 

436 registry._reinit_modules.update(m for m in reinit_modules.mapped('name') if m not in graph._imported_modules) 

437 

438 env.flush_all() 

439 cr.execute("update ir_module_module set state=%s where name=%s", ('installed', 'base')) 

440 Module.invalidate_model(['state']) 

441 

442 # STEP 3: Load marked modules (skipping base which was done in STEP 1) 

443 # loop this step in case extra modules' states are changed to 'to install'/'to update' during loading 

444 while True: 

445 if update_module: 445 ↛ 448line 445 didn't jump to line 448 because the condition on line 445 was always true

446 states = ('installed', 'to upgrade', 'to remove', 'to install') 

447 else: 

448 states = ('installed', 'to upgrade', 'to remove') 

449 env.cr.execute("SELECT name from ir_module_module WHERE state IN %s", [states]) 

450 module_list = [name for (name,) in env.cr.fetchall() if name not in graph] 

451 if not module_list: 

452 break 

453 graph.extend(module_list) 

454 _logger.debug('Updating graph with %d more modules', len(module_list)) 

455 updated_modules_count = len(registry.updated_modules) 

456 load_module_graph( 

457 env, graph, update_module=update_module, 

458 report=report, models_to_check=models_to_check) 

459 if len(registry.updated_modules) == updated_modules_count: 459 ↛ 460line 459 didn't jump to line 460 because the condition on line 459 was never true

460 break 

461 

462 if update_module: 462 ↛ 479line 462 didn't jump to line 479 because the condition on line 462 was always true

463 # set up the registry without the patch for translated fields 

464 database_translated_fields = registry._database_translated_fields 

465 registry._database_translated_fields = {} 

466 registry._setup_models__(cr, []) # incremental setup 

467 # determine which translated fields should no longer be translated, 

468 # and make their model fix the database schema 

469 models_to_untranslate = set() 

470 for full_name in database_translated_fields: 470 ↛ 471line 470 didn't jump to line 471 because the loop on line 470 never started

471 model_name, field_name = full_name.rsplit('.', 1) 

472 if model_name in registry: 

473 field = registry[model_name]._fields.get(field_name) 

474 if field and not field.translate: 

475 _logger.debug("Making field %s non-translated", field) 

476 models_to_untranslate.add(model_name) 

477 registry.init_models(cr, list(models_to_untranslate), {'models_to_check': True}) 

478 

479 registry.loaded = True 

480 registry._setup_models__(cr) 

481 

482 # check that all installed modules have been loaded by the registry 

483 Module = env['ir.module.module'] 

484 modules = Module.search_fetch(Module._get_modules_to_load_domain(), ['name'], order='name') 

485 missing = [name for name in modules.mapped('name') if name not in graph] 

486 if missing: 486 ↛ 487line 486 didn't jump to line 487 because the condition on line 486 was never true

487 _logger.error("Some modules are not loaded, some dependencies or manifest may be missing: %s", missing) 

488 

489 # STEP 3.5: execute migration end-scripts 

490 if update_module: 490 ↛ 496line 490 didn't jump to line 496 because the condition on line 490 was always true

491 migrations = MigrationManager(cr, graph) 

492 for package in graph: 

493 migrations.migrate_module(package, 'end') 

494 

495 # check that new module dependencies have been properly installed after a migration/upgrade 

496 cr.execute("SELECT name from ir_module_module WHERE state IN ('to install', 'to upgrade')") 

497 module_list = [name for (name,) in cr.fetchall()] 

498 if module_list: 498 ↛ 499line 498 didn't jump to line 499 because the condition on line 498 was never true

499 _logger.error("Some modules have inconsistent states, some dependencies may be missing: %s", sorted(module_list)) 

500 

501 # STEP 3.6: apply remaining constraints in case of an upgrade 

502 registry.finalize_constraints(cr) 

503 

504 # STEP 4: Finish and cleanup installations 

505 if registry.updated_modules: 505 ↛ 525line 505 didn't jump to line 525 because the condition on line 505 was always true

506 

507 cr.execute("SELECT model from ir_model") 

508 for (model,) in cr.fetchall(): 

509 if model in registry: 509 ↛ 511line 509 didn't jump to line 511 because the condition on line 509 was always true

510 env[model]._check_removed_columns(log=True) 

511 elif _logger.isEnabledFor(logging.INFO): # more an info that a warning... 

512 _logger.runbot("Model %s is declared but cannot be loaded! (Perhaps a module was partially removed or renamed)", model) 

513 

514 # Cleanup orphan records 

515 env['ir.model.data']._process_end(registry.updated_modules) 

516 # Cleanup cron 

517 vacuum_cron = env.ref('base.autovacuum_job', raise_if_not_found=False) 

518 if vacuum_cron: 518 ↛ 522line 518 didn't jump to line 522 because the condition on line 518 was always true

519 # trigger after a small delay to give time for assets to regenerate 

520 vacuum_cron._trigger(at=datetime.datetime.now() + datetime.timedelta(minutes=1)) 

521 

522 env.flush_all() 

523 

524 # STEP 5: Uninstall modules to remove 

525 if update_module: 525 ↛ 557line 525 didn't jump to line 557 because the condition on line 525 was always true

526 # Remove records referenced from ir_model_data for modules to be 

527 # removed (and removed the references from ir_model_data). 

528 cr.execute("SELECT name, id FROM ir_module_module WHERE state=%s", ('to remove',)) 

529 modules_to_remove = dict(cr.fetchall()) 

530 if modules_to_remove: 530 ↛ 531line 530 didn't jump to line 531 because the condition on line 530 was never true

531 pkgs = reversed([p for p in graph if p.name in modules_to_remove]) 

532 for pkg in pkgs: 

533 uninstall_hook = pkg.manifest.get('uninstall_hook') 

534 if uninstall_hook: 

535 py_module = sys.modules['odoo.addons.%s' % (pkg.name,)] 

536 getattr(py_module, uninstall_hook)(env) 

537 env.flush_all() 

538 

539 Module = env['ir.module.module'] 

540 Module.browse(modules_to_remove.values()).module_uninstall() 

541 # Recursive reload, should only happen once, because there should be no 

542 # modules to remove next time 

543 cr.commit() 

544 _logger.info('Reloading registry once more after uninstalling modules') 

545 registry = Registry.new( 

546 cr.dbname, update_module=update_module, models_to_check=models_to_check, 

547 ) 

548 return 

549 

550 # STEP 5.5: Verify extended fields on every model 

551 # This will fix the schema of all models in a situation such as: 

552 # - module A is loaded and defines model M; 

553 # - module B is installed/upgraded and extends model M; 

554 # - module C is loaded and extends model M; 

555 # - module B and C depend on A but not on each other; 

556 # The changes introduced by module C are not taken into account by the upgrade of B. 

557 if update_module: 557 ↛ 561line 557 didn't jump to line 561 because the condition on line 557 was always true

558 # We need to fix custom fields for which we have dropped the not-null constraint. 

559 cr.execute("""SELECT DISTINCT model FROM ir_model_fields WHERE state = 'manual'""") 

560 models_to_check.update(model_name for model_name, in cr.fetchall() if model_name in registry) 

561 if models_to_check: 561 ↛ 567line 561 didn't jump to line 567 because the condition on line 561 was always true

562 # Doesn't check models that didn't exist anymore, it might happen during uninstallation 

563 models_to_check = [model for model in models_to_check if model in registry] 

564 registry.init_models(cr, models_to_check, {'models_to_check': True, 'update_custom_fields': True}) 

565 

566 # STEP 6: verify custom views on every model 

567 if update_module: 567 ↛ 575line 567 didn't jump to line 575 because the condition on line 567 was always true

568 View = env['ir.ui.view'] 

569 for model in registry: 

570 try: 

571 View._validate_custom_views(model) 

572 except Exception as e: 

573 _logger.warning('invalid custom view(s) for model %s: %s', model, e) 

574 

575 if not registry._assertion_report or registry._assertion_report.wasSuccessful(): 575 ↛ 578line 575 didn't jump to line 578 because the condition on line 575 was always true

576 _logger.info('Modules loaded.') 

577 else: 

578 _logger.error('At least one test failed when loading the modules.') 

579 

580 # STEP 9: call _register_hook on every model 

581 # This is done *exactly once* when the registry is being loaded. See the 

582 # management of those hooks in `Registry._setup_models__`: all the calls to 

583 # _setup_models__() done here do not mess up with hooks, as registry.ready 

584 # is False. 

585 for model in env.values(): 

586 model._register_hook() 

587 env.flush_all() 

588 

589 # STEP 10: check that we can trust nullable columns 

590 registry.check_null_constraints(cr) 

591 

592 if update_module: 592 ↛ exitline 592 didn't jump to the function exit

593 cr.execute( 

594 """ 

595 INSERT INTO ir_config_parameter(key, value) 

596 SELECT 'base.partially_updated_database', '1' 

597 WHERE EXISTS(SELECT FROM ir_module_module WHERE state IN ('to upgrade', 'to install', 'to remove')) 

598 ON CONFLICT DO NOTHING 

599 """ 

600 ) 

601 

602 

603def reset_modules_state(db_name: str) -> None: 

604 """ 

605 Resets modules flagged as "to x" to their original state 

606 """ 

607 # Warning, this function was introduced in response to commit 763d714 

608 # which locks cron jobs for dbs which have modules marked as 'to %'. 

609 # The goal of this function is to be called ONLY when module 

610 # installation/upgrade/uninstallation fails, which is the only known case 

611 # for which modules can stay marked as 'to %' for an indefinite amount 

612 # of time 

613 db = odoo.sql_db.db_connect(db_name) 

614 with db.cursor() as cr: 

615 if not odoo.tools.sql.table_exists(cr, 'ir_module_module'): 

616 _logger.info('skipping reset_modules_state, ir_module_module table does not exists') 

617 return 

618 cr.execute( 

619 "UPDATE ir_module_module SET state='installed' WHERE state IN ('to remove', 'to upgrade')" 

620 ) 

621 cr.execute( 

622 "UPDATE ir_module_module SET state='uninstalled' WHERE state='to install'" 

623 ) 

624 _logger.warning("Transient module states were reset")