[IMP] core: install PG extension "pg_trgm" only if necessary

Task 2742526

closes odoo/odoo#83274

Signed-off-by: Raphael Collet <rco@odoo.com>
This commit is contained in:
Raphael Collet
2022-01-28 14:10:02 +00:00
parent a1904aa6f6
commit 75e6b645ac
3 changed files with 9 additions and 7 deletions
+2 -2
View File
@@ -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):
+4 -2
View File
@@ -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:
+3 -3
View File
@@ -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: