Skip to content

Commit

Permalink
[ADD] clean_transient_models
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Apr 5, 2024
1 parent d341a6a commit d7c777a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]


Expand Down Expand Up @@ -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))

0 comments on commit d7c777a

Please sign in to comment.