From f825f415f885f57ab367ab1df4e74ffc250d1700 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Thu, 10 Nov 2016 13:50:04 +0100 Subject: [PATCH] [FIX] registry: ensure that all non-abstract models have a table (#14194) After modifying the database schema, if some models do not have a table/view, attempt to recreate it by calling their method `init()`, following inter-model dependencies. Fix the model `board.board` by making it an abstract model. Add the missing model dependencies on report `crm.partner.report.assign` to make it depend on report `account.invoice.report`. --- addons/board/models/board.py | 2 +- .../report/crm_partner_report.py | 17 +++++++---------- odoo/modules/registry.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/addons/board/models/board.py b/addons/board/models/board.py index 56742a9eb26..3acd277826a 100644 --- a/addons/board/models/board.py +++ b/addons/board/models/board.py @@ -4,7 +4,7 @@ from odoo import api, models -class Board(models.Model): +class Board(models.AbstractModel): _name = 'board.board' _description = "Board" _auto = False diff --git a/addons/website_crm_partner_assign/report/crm_partner_report.py b/addons/website_crm_partner_assign/report/crm_partner_report.py index d144280e998..e96b67525ab 100644 --- a/addons/website_crm_partner_assign/report/crm_partner_report.py +++ b/addons/website_crm_partner_assign/report/crm_partner_report.py @@ -23,6 +23,13 @@ class CrmPartnerReportAssign(models.Model): turnover = fields.Float('Turnover', readonly=True) date = fields.Date('Invoice Account Date', readonly=True) + _depends = { + 'account.invoice.report': ['date', 'partner_id', 'price_total', 'state', 'type'], + 'crm.lead': ['partner_assigned_id'], + 'res.partner': ['activation', 'country_id', 'date_partnership', 'date_review', + 'grade_id', 'parent_id', 'team_id', 'user_id'], + } + @api.model_cr def init(self): """ @@ -50,13 +57,3 @@ class CrmPartnerReportAssign(models.Model): left join account_invoice_report i on (i.partner_id=p.id and i.type in ('out_invoice','out_refund') and i.state in ('open','paid')) )""") - -class AccountInvoiceReport(models.Model): - _inherit = 'account.invoice.report' - - @api.model_cr - def init(self): - # ensure we re-create the crm_partner_report_assign view whenever - # the underlying account_invoice_report view is changed - super(AccountInvoiceReport, self).init() - self.env['crm.partner.report.assign'].init() diff --git a/odoo/modules/registry.py b/odoo/modules/registry.py index 946682c241e..6e5378d33c8 100644 --- a/odoo/modules/registry.py +++ b/odoo/modules/registry.py @@ -327,6 +327,24 @@ class Registry(Mapping): models[0].recompute() cr.commit() + # make sure all tables are present + missing = [name + for name, model in env.items() + if not model._abstract and not model._table_exist()] + if missing: + _logger.warning("Models have no table: %s.", ", ".join(missing)) + # recreate missing tables following model dependencies + deps = {name: model._depends for name, model in env.items()} + for name in topological_sort(deps): + if name in missing: + _logger.info("Recreate table of model %s.", name) + env[name].init() + cr.commit() + # check again, and log errors if tables are still missing + for name, model in env.items(): + if not model._abstract and not model._table_exist(): + _logger.error("Model %s has no table.", name) + def clear_caches(self): """ Clear the caches associated to methods decorated with ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models.