Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] clean_transient_models #363

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Loading