From a8278314c79f8469a2fddf5b71f6b2b99fcef286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Fri, 5 Apr 2024 09:40:41 +0200 Subject: [PATCH] [ADD] clean_transient_models --- openupgradelib/openupgrade.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index cae812e7..4280e9bc 100644 --- a/openupgradelib/openupgrade.py +++ b/openupgradelib/openupgrade.py @@ -194,6 +194,7 @@ def do_raise(error): "convert_to_company_dependent", "cow_templates_mark_if_equal_to_upstream", "cow_templates_replicate_upstream", + "clean_transient_models", ] @@ -3492,3 +3493,32 @@ def cow_templates_replicate_upstream(cr, mark_colname=None): """ ).format(mark_identifier), ) + + +def clean_transient_models(cr): + """Clean transient models to prevent possible issues due to + chained data. + + To be run at the base pre-migration script for having a general scope. + Only works on > v8. + + :param cr: Database cursor. + """ + if version_info[0] < 9: + raise Exception("Not supported Odoo version for this method.") + cr.execute("SELECT model FROM ir_model WHERE transient") + table_names = [get_model2table(x[0]) for x in cr.fetchall()] + for table_name in table_names: + if not table_exists(cr, table_name): + continue + try: + with cr.savepoint(): + table = sql.Identifier(table_name) + query = sql.SQL( + """DELETE FROM {} WHERE + COALESCE(write_date, create_date, (now() at time zone 'UTC'))::timestamp + < ((now() at time zone 'UTC') - interval '1 seconds')""" + ).format(table) + cr.execute(query) + except Exception as e: + logger.warning("Failed to clean transient table %s\n%s", table_name, str(e))