Skip to content

Commit 3157e98

Browse files
committed
[FIX] util/pg: drop indexes when removing constraints
If a constraint with the same name was found. Attempt as well to drop any existing index with the same name. Otherwise we may leave behind constraints implemented as unique indexes. See odoo/upgrade#8318 closes #310 Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
1 parent 98a959e commit 3157e98

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/util/pg.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,15 +795,17 @@ def remove_constraint(cr, table, name, cascade=False, warn=True):
795795
"""
796796
_validate_table(table)
797797
log = _logger.warning if warn else _logger.info
798-
cascade = "CASCADE" if cascade else ""
799-
cr.execute('ALTER TABLE "{}" DROP CONSTRAINT IF EXISTS "{}" {}'.format(table, name, cascade))
798+
cascade = SQLStr("CASCADE" if cascade else "")
799+
cr.execute(format_query(cr, "ALTER TABLE {} DROP CONSTRAINT IF EXISTS {} {}", table, name, cascade))
800800
# Exceptionally remove Odoo records, even if we are in PG land on this file. This is somehow
801801
# valid because ir.model.constraint are ORM low-level objects that relate directly to table
802802
# constraints.
803803
cr.execute("DELETE FROM ir_model_constraint WHERE name = %s RETURNING id", [name])
804804
if cr.rowcount:
805805
ids = tuple(c for (c,) in cr.fetchall())
806806
cr.execute("DELETE FROM ir_model_data WHERE model = 'ir.model.constraint' AND res_id IN %s", [ids])
807+
# The constraint was found remove any index with same name
808+
cr.execute(format_query(cr, "DROP INDEX IF EXISTS {}", name))
807809
return True
808810
if name.startswith(table + "_"):
809811
log("%r not found in ir_model_constraint, table=%r", name, table)

0 commit comments

Comments
 (0)