OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5 KiB

  1. # Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import logging
  4. from odoo import SUPERUSER_ID
  5. from odoo import api, modules
  6. from odoo.tools import existing_tables, topological_sort
  7. _logger = logging.getLogger(__name__)
  8. def _bi_view(_name):
  9. return _name.startswith('x_bve.')
  10. def post_load():
  11. def check_tables_exist(self, cr):
  12. """
  13. Verify that all tables are present and try to initialize
  14. those that are missing.
  15. """
  16. # This monkey patch is meant to avoid that the _logger writes
  17. # warning and error messages, while running an update all,
  18. # in case the model is a bi-view-generated model.
  19. env = api.Environment(cr, SUPERUSER_ID, {})
  20. table2model = {
  21. model._table: name for name, model in env.items()
  22. if not model._abstract and not _bi_view(name) # here is the patch
  23. }
  24. missing_tables = set(table2model).difference(
  25. existing_tables(cr, table2model))
  26. if missing_tables:
  27. missing = {table2model[table] for table in missing_tables}
  28. _logger.warning("Models have no table: %s.", ", ".join(missing))
  29. # recreate missing tables following model dependencies
  30. deps = {name: model._depends for name, model in env.items()}
  31. for name in topological_sort(deps):
  32. if name in missing:
  33. _logger.info("Recreate table of model %s.", name)
  34. env[name].init()
  35. # check again, and log errors if tables are still missing
  36. missing_tables = set(table2model).difference(
  37. existing_tables(cr, table2model))
  38. for table in missing_tables:
  39. _logger.error("Model %s has no table.", table2model[table])
  40. modules.registry.Registry.check_tables_exist = check_tables_exist
  41. def uninstall_hook(cr, registry):
  42. # delete dirty data that could cause problems
  43. # while re-installing the module
  44. cr.execute("""
  45. delete from ir_model where model like 'x_bve.%'
  46. """)
  47. cr.execute("""
  48. delete from bve_view where model_name like 'x_bve.%'
  49. """)
  50. cr.execute("""
  51. SELECT 'DROP VIEW ' || table_name
  52. FROM information_schema.views
  53. WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
  54. AND table_name like 'x_bve_%'
  55. """)
  56. results = list(cr.fetchall())
  57. for result in results:
  58. cr.execute(result[0])