From 5e53f9d0301ec2aa575776821df722d6999d6224 Mon Sep 17 00:00:00 2001 From: Sander van de Graaf Date: Fri, 8 Jul 2022 09:24:08 +0200 Subject: [PATCH 1/2] if extra init_command options are given, load them --- lib/mysql/connector/django/client.py | 5 +++-- tests/test_django.py | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/mysql/connector/django/client.py b/lib/mysql/connector/django/client.py index 8bf8fa81..fa0b8b37 100644 --- a/lib/mysql/connector/django/client.py +++ b/lib/mysql/connector/django/client.py @@ -51,8 +51,9 @@ def settings_to_cmd_args(cls, settings_dict): if defaults_file: args.append('--defaults-file={0}'.format(defaults_file)) - # We force SQL_MODE to TRADITIONAL - args.append('--init-command=SET @@session.SQL_MODE=TRADITIONAL') + # Load any custom init_commands. We always force SQL_MODE to TRADITIONAL + init_command = settings_dict['OPTIONS'].get('init_command', '') + args.append('--init-command=SET @@session.SQL_MODE=TRADITIONAL;{0}'.format(init_command)) if user: args.append('--user={0}'.format(user)) diff --git a/tests/test_django.py b/tests/test_django.py index 0de4c331..59a84c56 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -435,7 +435,14 @@ def test_converter_class(self): } cnx = self.create_connection() cnx.close() - del settings.DATABASES['default']['OPTIONS'] + + def test_init_command(self): + settings.DATABASES["default"]["OPTIONS"] = { + "init_command": "SET foo='bar'", + } + cnx = self.create_connection() + cnx.close() + del settings.DATABASES["default"]["OPTIONS"] class BugOra20106629(tests.MySQLConnectorTests): From 83c707cdaa38061d9b4e2b637fb2d31439f689a2 Mon Sep 17 00:00:00 2001 From: Sander van de Graaf Date: Thu, 4 Aug 2022 14:57:06 +0200 Subject: [PATCH 2/2] added init_command option --- lib/mysql/connector/abstracts.py | 9 +++++++++ lib/mysql/connector/constants.py | 1 + 2 files changed, 10 insertions(+) diff --git a/lib/mysql/connector/abstracts.py b/lib/mysql/connector/abstracts.py index 17a4fc23..a758e8ae 100644 --- a/lib/mysql/connector/abstracts.py +++ b/lib/mysql/connector/abstracts.py @@ -138,6 +138,8 @@ def __init__(self, **kwargs): self._consume_results = False + self._init_command = None + def __enter__(self): return self @@ -546,6 +548,11 @@ def config(self, **kwargs): raise errors.InterfaceError("Clear password authentication is not " "supported over insecure channels") + # If an init_command is set, keep it, so we can execute it in _post_connection + if "init_command" in config: + self._init_command = config["init_command"] + del config["init_command"] + # Other configuration set_ssl_flag = False for key, value in config.items(): @@ -1031,6 +1038,8 @@ def _post_connection(self): self.time_zone = self._time_zone if self._sql_mode: self.sql_mode = self._sql_mode + if self._init_command: + self._execute_query(self._init_command) @abstractmethod def disconnect(self): diff --git a/lib/mysql/connector/constants.py b/lib/mysql/connector/constants.py index 208e237c..7a03a25f 100644 --- a/lib/mysql/connector/constants.py +++ b/lib/mysql/connector/constants.py @@ -91,6 +91,7 @@ 'krb_service_principal': None, 'oci_config_file': None, 'fido_callback': None, + 'init_command': None, } CNX_POOL_ARGS = ('pool_name', 'pool_size', 'pool_reset_session')