0
0
mirror of https://github.com/django/django.git synced 2024-11-28 21:43:13 +01:00

Simplified using DATABASES["OPTIONS"].

DATABASES["OPTIONS"] are always configured.
This commit is contained in:
Florian Apolloner 2024-02-23 07:44:55 +01:00 committed by Mariusz Felisiak
parent b9d539cca7
commit 50e95ad536
3 changed files with 6 additions and 8 deletions

View File

@ -190,9 +190,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
def get_connection_params(self): def get_connection_params(self):
settings_dict = self.settings_dict settings_dict = self.settings_dict
# None may be used to connect to the default 'postgres' db # None may be used to connect to the default 'postgres' db
if settings_dict["NAME"] == "" and not settings_dict.get("OPTIONS", {}).get( if settings_dict["NAME"] == "" and not settings_dict["OPTIONS"].get("service"):
"service"
):
raise ImproperlyConfigured( raise ImproperlyConfigured(
"settings.DATABASES is improperly configured. " "settings.DATABASES is improperly configured. "
"Please supply the NAME or OPTIONS['service'] value." "Please supply the NAME or OPTIONS['service'] value."
@ -215,7 +213,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
} }
elif settings_dict["NAME"] is None: elif settings_dict["NAME"] is None:
# Connect to the default 'postgres' db. # Connect to the default 'postgres' db.
settings_dict.get("OPTIONS", {}).pop("service", None) settings_dict["OPTIONS"].pop("service", None)
conn_params = {"dbname": "postgres", **settings_dict["OPTIONS"]} conn_params = {"dbname": "postgres", **settings_dict["OPTIONS"]}
else: else:
conn_params = {**settings_dict["OPTIONS"]} conn_params = {**settings_dict["OPTIONS"]}
@ -300,7 +298,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
def ensure_role(self): def ensure_role(self):
if self.connection is None: if self.connection is None:
return False return False
if new_role := self.settings_dict.get("OPTIONS", {}).get("assume_role"): if new_role := self.settings_dict["OPTIONS"].get("assume_role"):
with self.connection.cursor() as cursor: with self.connection.cursor() as cursor:
sql = self.ops.compose_sql("SET ROLE %s", [new_role]) sql = self.ops.compose_sql("SET ROLE %s", [new_role])
cursor.execute(sql) cursor.execute(sql)
@ -324,8 +322,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
def create_cursor(self, name=None): def create_cursor(self, name=None):
if name: if name:
if is_psycopg3 and ( if is_psycopg3 and (
self.settings_dict.get("OPTIONS", {}).get("server_side_binding") self.settings_dict["OPTIONS"].get("server_side_binding") is not True
is not True
): ):
# psycopg >= 3 forces the usage of server-side bindings for # psycopg >= 3 forces the usage of server-side bindings for
# named cursors so a specialized class that implements # named cursors so a specialized class that implements

View File

@ -9,7 +9,7 @@ class DatabaseClient(BaseDatabaseClient):
@classmethod @classmethod
def settings_to_cmd_args_env(cls, settings_dict, parameters): def settings_to_cmd_args_env(cls, settings_dict, parameters):
args = [cls.executable_name] args = [cls.executable_name]
options = settings_dict.get("OPTIONS", {}) options = settings_dict["OPTIONS"]
host = settings_dict.get("HOST") host = settings_dict.get("HOST")
port = settings_dict.get("PORT") port = settings_dict.get("PORT")

View File

@ -14,6 +14,7 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
def settings_to_cmd_args_env(self, settings_dict, parameters=None): def settings_to_cmd_args_env(self, settings_dict, parameters=None):
if parameters is None: if parameters is None:
parameters = [] parameters = []
settings_dict.setdefault("OPTIONS", {})
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters) return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
def test_basic(self): def test_basic(self):