From 57f078b68b79bfdb53d4d7bb71b6672b4ad8e661 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Alix?= <sebastien.alix@camptocamp.com>
Date: Fri, 20 Aug 2021 12:12:32 +0000
Subject: [PATCH] [FIX] registry: check if index exists before logging

The `update_db_index` method is called even if the field has no index
(the check on `index` field attribute is here on purpose).

Before 13f02a60c870 was applied, the call of `sql.drop_index` was taking
care of the index existence if a `IF EXISTS` SQL statement, so even if
the field had no index it wasn't causing any issue.

With 13f02a60c870 applied, the log 'Keep unexpected index' is spam even if
the field has actually no index at all in the database.

This commit ensures to check the existence of the index before logging.

closes odoo/odoo#75387

Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
---
 odoo/fields.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/odoo/fields.py b/odoo/fields.py
index 195cc269d7f1..3f7b6b7bd787 100644
--- a/odoo/fields.py
+++ b/odoo/fields.py
@@ -1002,7 +1002,7 @@ class Field(MetaField('DummyField', (object,), {})):
                 sql.create_index(model._cr, indexname, model._table, ['"%s"' % self.name])
             except psycopg2.OperationalError:
                 _schema.error("Unable to add index for %s", self)
-        else:
+        elif sql.index_exists(model._cr, indexname):
             _schema.info("Keep unexpected index %s on table %s", indexname, model._table)
 
     def update_db_related(self, model):
-- 
GitLab