From bc9b3e826b9b005b84f8dec78eb9cc50b41e506d Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Thu, 14 Oct 2021 17:30:51 +0530 Subject: [PATCH] fix: Rollback, Release locals & Close db connection When exitting console, rollback and destroy the Frappe connection. This rollback is added so that rollback_observers are executed in case methods are run in the console which shouldn't be committed. For instance, running File.optimize will make changes to the file on disk but may not update the corresponding data in the DB. --- frappe/commands/utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index 74af28e3ff..03713c9632 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -504,6 +504,12 @@ frappe.db.connect() ]) +def _console_cleanup(): + # Execute rollback_observers on console close + frappe.db.rollback() + frappe.destroy() + + @click.command('console') @click.option( '--autoreload', @@ -519,6 +525,9 @@ def console(context, autoreload=False): frappe.local.lang = frappe.db.get_default("lang") from IPython.terminal.embed import InteractiveShellEmbed + from atexit import register + + register(_console_cleanup) terminal = InteractiveShellEmbed() if autoreload: