From 75e6b645acdc95aece506ce0249fd0760838281c Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 24 Jan 2022 14:44:32 +0000 Subject: [PATCH] [IMP] core: install PG extension "pg_trgm" only if necessary Task 2742526 closes odoo/odoo#83274 Signed-off-by: Raphael Collet --- addons/account/models/account_move.py | 4 ++-- odoo/modules/registry.py | 6 ++++-- odoo/tools/sql.py | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 2e551cb99f0..a8484dba4d2 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -381,8 +381,8 @@ class AccountMove(models.Model): # We neeed the btree index for unicity constraint (on field) AND this one for human searches def _auto_init(self): super(AccountMove, self)._auto_init() - if sql.has_pg_trgm(self._cr): - sql.create_index(self._cr, 'account_move_name_gin_index', self._table, ['name gin_trgm_ops'], 'gin') + if sql.install_pg_trgm(self._cr): + sql.create_index(self._cr, 'account_move_name_trigram_index', self._table, ['"name" gin_trgm_ops'], 'gin') @api.model def _field_will_change(self, record, vals, field_name): diff --git a/odoo/modules/registry.py b/odoo/modules/registry.py index 6eb5032734c..258681acb3c 100644 --- a/odoo/modules/registry.py +++ b/odoo/modules/registry.py @@ -468,11 +468,13 @@ class Registry(Mapping): if not expected: return - trgm = sql.has_pg_trgm(cr) cr.execute("SELECT indexname FROM pg_indexes WHERE indexname IN %s", [tuple(row[0] for row in expected)]) existing = {row[0] for row in cr.fetchall()} + if not self.has_trigram and any(row[3] == 'trigram' for row in expected): + self.has_trigram = sql.install_pg_trgm(cr) + for indexname, tablename, columnname, index in expected: assert index in ('btree', 'btree_not_null', 'trigram', True, False, None) if index and indexname not in existing: @@ -481,7 +483,7 @@ class Registry(Mapping): where = '' if index == 'btree_not_null': where = f'"{columnname}" IS NOT NULL' - elif index == 'trigram' and trgm: + elif index == 'trigram' and self.has_trigram: method = 'gin' operator = 'gin_trgm_ops' try: diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py index 44e176668a1..8cbf8c883da 100644 --- a/odoo/tools/sql.py +++ b/odoo/tools/sql.py @@ -217,14 +217,14 @@ def fix_foreign_key(cr, tablename1, columnname1, tablename2, columnname2, ondele if not found: return add_foreign_key(cr, tablename1, columnname1, tablename2, columnname2, ondelete) -def has_pg_trgm(cr): - cr.execute("select installed_version from pg_available_extensions where name='pg_trgm'") +def install_pg_trgm(cr): + cr.execute("SELECT installed_version FROM pg_available_extensions WHERE name='pg_trgm'") version = cr.fetchone() if version is None: return False if version[0]: return True - cr.execute('select usesuper from pg_user where usename = CURRENT_USER') + cr.execute('SELECT usesuper FROM pg_user WHERE usename = CURRENT_USER') if not cr.fetchone()[0]: return False try: