From 7380ac57340653854bc2cfe0ed80298cdac6061d Mon Sep 17 00:00:00 2001 From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:51:31 +0200 Subject: Fixed #35688 -- Restored timezone and role setters to be PostgreSQL DatabaseWrapper methods. Following the addition of PostgreSQL connection pool support in Refs #33497, the methods for configuring the database role and timezone were moved to module-level functions. This change prevented subclasses of DatabaseWrapper from overriding these methods as needed, for example, when creating wrappers for other PostgreSQL-based backends. Thank you Christian Hardenberg for the report and to Florian Apolloner and Natalia Bidart for the review. Regression in fad334e1a9b54ea1acb8cce02a25934c5acfe99f. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> --- tests/backends/postgresql/tests.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'tests/backends/postgresql') diff --git a/tests/backends/postgresql/tests.py b/tests/backends/postgresql/tests.py index 0b4f580612..37c5ee562b 100644 --- a/tests/backends/postgresql/tests.py +++ b/tests/backends/postgresql/tests.py @@ -567,3 +567,49 @@ class Tests(TestCase): ) finally: new_connection.close() + + def test_bypass_timezone_configuration(self): + from django.db.backends.postgresql.base import DatabaseWrapper + + class CustomDatabaseWrapper(DatabaseWrapper): + def _configure_timezone(self, connection): + return False + + for Wrapper, commit in [ + (DatabaseWrapper, True), + (CustomDatabaseWrapper, False), + ]: + with self.subTest(wrapper=Wrapper, commit=commit): + new_connection = no_pool_connection() + self.addCleanup(new_connection.close) + + # Set the database default time zone to be different from + # the time zone in new_connection.settings_dict. + with new_connection.cursor() as cursor: + cursor.execute("RESET TIMEZONE") + cursor.execute("SHOW TIMEZONE") + db_default_tz = cursor.fetchone()[0] + new_tz = "Europe/Paris" if db_default_tz == "UTC" else "UTC" + new_connection.timezone_name = new_tz + + settings = new_connection.settings_dict.copy() + conn = new_connection.connection + self.assertIs(Wrapper(settings)._configure_connection(conn), commit) + + def test_bypass_role_configuration(self): + from django.db.backends.postgresql.base import DatabaseWrapper + + class CustomDatabaseWrapper(DatabaseWrapper): + def _configure_role(self, connection): + return False + + new_connection = no_pool_connection() + self.addCleanup(new_connection.close) + new_connection.connect() + + settings = new_connection.settings_dict.copy() + settings["OPTIONS"]["assume_role"] = "django_nonexistent_role" + conn = new_connection.connection + self.assertIs( + CustomDatabaseWrapper(settings)._configure_connection(conn), False + ) -- cgit v1.3