From f70d515905dce1e8abfb93e3db3da15bbbd24755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Tue, 28 Oct 2025 10:57:07 +0100 Subject: [PATCH] Add a clear cache method to the Database ExtensionTestCase uses this new method to clear all the caches of the Database. --- trytond/CHANGELOG | 1 + trytond/doc/ref/backend.rst | 4 ++++ trytond/trytond/backend/database.py | 4 ++++ trytond/trytond/backend/postgresql/database.py | 15 +++++++++++++-- trytond/trytond/backend/sqlite/database.py | 5 +++++ trytond/trytond/tests/test_tryton.py | 8 ++------ 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index f00f723ccc..9fad814356 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Add a clear cache method to the Database * Add sequence_reorder to model order * Remove has_window_functions from Database * Set minimal supported version of SQLite to 3.30.0 diff --git a/trytond/doc/ref/backend.rst b/trytond/doc/ref/backend.rst index 07d6c782c7..9486ddfecf 100644 --- a/trytond/doc/ref/backend.rst +++ b/trytond/doc/ref/backend.rst @@ -50,6 +50,10 @@ Database Drop the named database using the connection. +.. classmethod:: Database.clear_cache() + + Clear the cached values regarding the database properties. + .. method:: Database.list([hostname]) Return a list of Tryton database names. diff --git a/trytond/trytond/backend/database.py b/trytond/trytond/backend/database.py index 4c76db15a8..efcc649031 100644 --- a/trytond/trytond/backend/database.py +++ b/trytond/trytond/backend/database.py @@ -45,6 +45,10 @@ class DatabaseInterface(object): def drop(cls, connection, database_name): raise NotImplementedError + @classmethod + def clear_cache(cls): + pass + def list(self, hostname=None): raise NotImplementedError diff --git a/trytond/trytond/backend/postgresql/database.py b/trytond/trytond/backend/postgresql/database.py index 28aef43fa3..7e4c0d5ec4 100644 --- a/trytond/trytond/backend/postgresql/database.py +++ b/trytond/trytond/backend/postgresql/database.py @@ -330,9 +330,20 @@ class Database(DatabaseInterface): cursor = connection.cursor() cursor.execute(SQL("DROP DATABASE {}") .format(Identifier(database_name))) + cls.clear_cache() + + @classmethod + def clear_cache(cls): cls._list_cache.clear() - cls._has_proc.pop(database_name, None) - cls._search_full_text_languages.pop(database_name, None) + cls._list_cache_timestamp.clear() + cls._search_path = None + cls._current_user = None + cls._has_returning = None + cls._has_insert_on_conflict = None + cls._has_select_for_skip_locked = None + cls._has_proc.clear() + cls._extensions.clear() + cls._search_full_text_languages.clear() def get_version(self, connection): version = connection.server_version diff --git a/trytond/trytond/backend/sqlite/database.py b/trytond/trytond/backend/sqlite/database.py index 89e129b67b..5503b74355 100644 --- a/trytond/trytond/backend/sqlite/database.py +++ b/trytond/trytond/backend/sqlite/database.py @@ -613,7 +613,12 @@ class Database(DatabaseInterface): os.remove(file + suffix) except FileNotFoundError: pass + cls.clear_cache() + + @classmethod + def clear_cache(cls): cls._list_cache.clear() + cls._list_cache_timestamp.clear() def list(self, hostname=None): now = time.time() diff --git a/trytond/trytond/tests/test_tryton.py b/trytond/trytond/tests/test_tryton.py index 3a7bceaf64..ab424ea31e 100644 --- a/trytond/trytond/tests/test_tryton.py +++ b/trytond/trytond/tests/test_tryton.py @@ -1265,7 +1265,7 @@ class ExtensionTestCase(TestCase): cursor = connection.cursor() cursor.execute('CREATE EXTENSION "%s"' % cls.extension) connection.commit() - cls._clear_cache() + backend.Database.clear_cache() @classmethod @with_transaction() @@ -1274,11 +1274,7 @@ class ExtensionTestCase(TestCase): cursor = connection.cursor() cursor.execute('DROP EXTENSION "%s"' % cls.extension) connection.commit() - cls._clear_cache() - - @classmethod - def _clear_cache(cls): - backend.Database._has_proc.clear() + backend.Database.clear_cache() def drop_db(name=DB_NAME):