diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index cae812e7..da5c29d3 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,29 @@ def cow_templates_replicate_upstream(cr, mark_colname=None): """ ).format(mark_identifier), ) + + +def clean_transient_models(env): + """Clean transient models to prevent possible issues due to + chained data. + + To be run at the base post-migration script for having a general scope. Only + assured to work on > v9. + + :param env: Environment/pool variable. + """ + for mname in env: + model = env[mname] + if model.is_transient(): + try: + with env._cr.savepoint(): + table = sql.Identifier(model._table) + 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) + env._cr.execute(query) + env._cr.commit() + except Exception as e: + logger.warning("Failed to clean transient model %s\n%s", model, str(e))