From dfc16d78c4a1104c610ebb6de44baf8cfbad8ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Tue, 19 Jul 2022 16:33:01 +0200 Subject: [PATCH] [ADD] delete_constraint_safely: method to delete constraints --- openupgradelib/openupgrade.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index ecbaffe9..b3961763 100644 --- a/openupgradelib/openupgrade.py +++ b/openupgradelib/openupgrade.py @@ -179,6 +179,7 @@ def do_raise(error): 'disable_invalid_filters', 'safe_unlink', 'delete_records_safely_by_xml_id', + 'delete_sql_constraint_safely', 'set_xml_ids_noupdate_value', 'convert_to_company_dependent', 'cow_templates_mark_if_equal_to_upstream', @@ -2828,6 +2829,25 @@ def delete_records_safely_by_xml_id(env, xml_ids): logger.info("XML-ID %s changed to noupdate.", xml_id) +def delete_sql_constraint_safely(env, module, table, name): + """In case of obsolete constraints, run this in pre-migration script. + Useful from v14 onwards. + :param module: Module where the sql constraint was declared + :param table: Table where the sql constraint belongs + :param name: Name of the sql constraint as it was declared""" + logged_query( + env.cr, + """ALTER TABLE {} + DROP CONSTRAINT IF EXISTS {}""".format( + table, table + "_" + name + ), + ) + if version_info[0] > 13: + delete_records_safely_by_xml_id( + env, [module + ".constraint_" + table + "_" + name] + ) + + def chunked(records, single=True): """ Memory and performance friendly method to iterate over a potentially large number of records. Yields either a whole chunk or a single record