You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/03/04 14:12:00 UTC

[GitHub] [airflow] feluelle commented on a change in pull request #6576: [AIRFLOW-5922] Add option to specify the mysql client library used in MySqlHook

feluelle commented on a change in pull request #6576: [AIRFLOW-5922] Add option to specify the mysql client library used in MySqlHook
URL: https://github.com/apache/airflow/pull/6576#discussion_r387690189
 
 

 ##########
 File path: airflow/providers/mysql/hooks/mysql.py
 ##########
 @@ -113,8 +107,44 @@ def get_conn(self):
             conn_config['unix_socket'] = conn.extra_dejson['unix_socket']
         if local_infile:
             conn_config["local_infile"] = 1
-        conn = MySQLdb.connect(**conn_config)
-        return conn
+        return conn_config
+
+    def _get_conn_config_mysql_connector_python(self, conn):
+        conn_config = {
+            'user': conn.login,
+            'password': conn.password or '',
+            'host': conn.host or 'localhost',
+            'database': self.schema or conn.schema or '',
+            'port': int(conn.port) if conn.port else 3306
+        }
+
+        if conn.extra_dejson.get('allow_local_infile', False):
+            conn_config["allow_local_infile"] = True
+
+        return conn_config
+
+    def get_conn(self):
+        """
+        Establishes a connection to a mysql database
+        by extracting the connection configuration from the Airflow connection.
+
+        .. note:: By default it connects to the database via the mysqlclient library.
+            But you can also choose the mysql-connector-python library which lets you connect through ssl
+            without any further ssl parameters required.
+
+        :return: a mysql connection object
+        """
+        conn = self.connection or self.get_connection(self.mysql_conn_id)  # pylint: disable=no-member
+
+        client_name = conn.extra_dejson.get('client', 'mysqlclient')
+
+        if client_name == 'mysql-connector-python':
+            import mysql.connector
+            conn_config = self._get_conn_config_mysql_connector_python(conn)
+            return mysql.connector.connect(**conn_config)
+
+        conn_config = self._get_conn_config_mysql_client(conn)
 
 Review comment:
   Like this?
   
   ```python
   client_name = conn.extra_dejson.get('client', 'mysqlclient')
   
   if client_name == 'mysqlclient':
       ...
   
   if client_name == 'mysql-connector-python':
       ...
   
   raise ValueError('Unknown MySQL client name provided!')
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services