db.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from psycopg2.extras import Json
  4. import logging
  5. from enum import IntEnum
  6. import odoo.modules
  7. _logger = logging.getLogger(__name__)
  8. def is_initialized(cr):
  9. """ Check if a database has been initialized for the ORM.
  10. The database can be initialized with the 'initialize' function below.
  11. """
  12. return odoo.tools.table_exists(cr, 'ir_module_module')
  13. def initialize(cr):
  14. """ Initialize a database with for the ORM.
  15. This executes base/data/base_data.sql, creates the ir_module_categories
  16. (taken from each module descriptor file), and creates the ir_module_module
  17. and ir_model_data entries.
  18. """
  19. f = odoo.modules.get_module_resource('base', 'data', 'base_data.sql')
  20. if not f:
  21. m = "File not found: 'base.sql' (provided by module 'base')."
  22. _logger.critical(m)
  23. raise IOError(m)
  24. with odoo.tools.misc.file_open(f) as base_sql_file:
  25. cr.execute(base_sql_file.read()) # pylint: disable=sql-injection
  26. for i in odoo.modules.get_modules():
  27. mod_path = odoo.modules.get_module_path(i)
  28. if not mod_path:
  29. continue
  30. # This will raise an exception if no/unreadable descriptor file.
  31. info = odoo.modules.get_manifest(i)
  32. if not info:
  33. continue
  34. categories = info['category'].split('/')
  35. category_id = create_categories(cr, categories)
  36. if info['installable']:
  37. state = 'uninstalled'
  38. else:
  39. state = 'uninstallable'
  40. cr.execute('INSERT INTO ir_module_module \
  41. (author, website, name, shortdesc, description, \
  42. category_id, auto_install, state, web, license, application, icon, sequence, summary) \
  43. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id', (
  44. info['author'],
  45. info['website'], i, Json({'en_US': info['name']}),
  46. Json({'en_US': info['description']}), category_id,
  47. info['auto_install'] is not False, state,
  48. info['web'],
  49. info['license'],
  50. info['application'], info['icon'],
  51. info['sequence'], Json({'en_US': info['summary']})))
  52. id = cr.fetchone()[0]
  53. cr.execute('INSERT INTO ir_model_data \
  54. (name,model,module, res_id, noupdate) VALUES (%s,%s,%s,%s,%s)', (
  55. 'module_'+i, 'ir.module.module', 'base', id, True))
  56. dependencies = info['depends']
  57. for d in dependencies:
  58. cr.execute(
  59. 'INSERT INTO ir_module_module_dependency (module_id, name, auto_install_required)'
  60. ' VALUES (%s, %s, %s)',
  61. (id, d, d in (info['auto_install'] or ()))
  62. )
  63. # Install recursively all auto-installing modules
  64. while True:
  65. # this selects all the auto_install modules whose auto_install_required
  66. # deps are marked as to install
  67. cr.execute("""
  68. SELECT m.name FROM ir_module_module m
  69. WHERE m.auto_install
  70. AND state != 'to install'
  71. AND NOT EXISTS (
  72. SELECT 1 FROM ir_module_module_dependency d
  73. JOIN ir_module_module mdep ON (d.name = mdep.name)
  74. WHERE d.module_id = m.id
  75. AND d.auto_install_required
  76. AND mdep.state != 'to install'
  77. )""")
  78. to_auto_install = [x[0] for x in cr.fetchall()]
  79. # however if the module has non-required deps we need to install
  80. # those, so merge-in the modules which have a dependen*t* which is
  81. # *either* to_install or in to_auto_install and merge it in?
  82. cr.execute("""
  83. SELECT d.name FROM ir_module_module_dependency d
  84. JOIN ir_module_module m ON (d.module_id = m.id)
  85. JOIN ir_module_module mdep ON (d.name = mdep.name)
  86. WHERE (m.state = 'to install' OR m.name = any(%s))
  87. -- don't re-mark marked modules
  88. AND NOT (mdep.state = 'to install' OR mdep.name = any(%s))
  89. """, [to_auto_install, to_auto_install])
  90. to_auto_install.extend(x[0] for x in cr.fetchall())
  91. if not to_auto_install: break
  92. cr.execute("""UPDATE ir_module_module SET state='to install' WHERE name in %s""", (tuple(to_auto_install),))
  93. def create_categories(cr, categories):
  94. """ Create the ir_module_category entries for some categories.
  95. categories is a list of strings forming a single category with its
  96. parent categories, like ['Grand Parent', 'Parent', 'Child'].
  97. Return the database id of the (last) category.
  98. """
  99. p_id = None
  100. category = []
  101. while categories:
  102. category.append(categories[0])
  103. xml_id = 'module_category_' + ('_'.join(x.lower() for x in category)).replace('&', 'and').replace(' ', '_')
  104. # search via xml_id (because some categories are renamed)
  105. cr.execute("SELECT res_id FROM ir_model_data WHERE name=%s AND module=%s AND model=%s",
  106. (xml_id, "base", "ir.module.category"))
  107. c_id = cr.fetchone()
  108. if not c_id:
  109. cr.execute('INSERT INTO ir_module_category \
  110. (name, parent_id) \
  111. VALUES (%s, %s) RETURNING id', (Json({'en_US': categories[0]}), p_id))
  112. c_id = cr.fetchone()[0]
  113. cr.execute('INSERT INTO ir_model_data (module, name, res_id, model, noupdate) \
  114. VALUES (%s, %s, %s, %s, %s)', ('base', xml_id, c_id, 'ir.module.category', True))
  115. else:
  116. c_id = c_id[0]
  117. p_id = c_id
  118. categories = categories[1:]
  119. return p_id
  120. class FunctionStatus(IntEnum):
  121. MISSING = 0 # function is not present (falsy)
  122. PRESENT = 1 # function is present but not indexable (not immutable)
  123. INDEXABLE = 2 # function is present and indexable (immutable)
  124. def has_unaccent(cr):
  125. """ Test whether the database has function 'unaccent' and return its status.
  126. The unaccent is supposed to be provided by the PostgreSQL unaccent contrib
  127. module but any similar function will be picked by OpenERP.
  128. :rtype: FunctionStatus
  129. """
  130. cr.execute("""
  131. SELECT p.provolatile
  132. FROM pg_proc p
  133. LEFT JOIN pg_catalog.pg_namespace ns ON p.pronamespace = ns.oid
  134. WHERE p.proname = 'unaccent'
  135. AND p.pronargs = 1
  136. AND ns.nspname = 'public'
  137. """)
  138. result = cr.fetchone()
  139. if not result:
  140. return FunctionStatus.MISSING
  141. # The `provolatile` of unaccent allows to know whether the unaccent function
  142. # can be used to create index (it should be 'i' - means immutable), see
  143. # https://www.postgresql.org/docs/current/catalog-pg-proc.html.
  144. return FunctionStatus.INDEXABLE if result[0] == 'i' else FunctionStatus.PRESENT
  145. def has_trigram(cr):
  146. """ Test if the database has the a word_similarity function.
  147. The word_similarity is supposed to be provided by the PostgreSQL built-in
  148. pg_trgm module but any similar function will be picked by Odoo.
  149. """
  150. cr.execute("SELECT proname FROM pg_proc WHERE proname='word_similarity'")
  151. return len(cr.fetchall()) > 0