Skip to content

Commit

Permalink
Merge pull request #363 from ForgeFlow/master-add-clean_transient_models
Browse files Browse the repository at this point in the history
[ADD] clean_transient_models
  • Loading branch information
pedrobaeza authored Apr 5, 2024
2 parents d341a6a + a827831 commit a6a665a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 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,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))

0 comments on commit a6a665a

Please sign in to comment.