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/04/26 08:23:48 UTC

[GitHub] [airflow] feluelle commented on a change in pull request #7903: Add property conn_name and connection to dbapi_hook

feluelle commented on a change in pull request #7903:
URL: https://github.com/apache/airflow/pull/7903#discussion_r415251335



##########
File path: airflow/hooks/dbapi_hook.py
##########
@@ -62,15 +63,29 @@ def __init__(self, *args, **kwargs):
             raise AirflowException("conn_name_attr is not defined")
         elif len(args) == 1:
             setattr(self, self.conn_name_attr, args[0])
-        elif self.conn_name_attr not in kwargs:
-            setattr(self, self.conn_name_attr, self.default_conn_name)
         else:
-            setattr(self, self.conn_name_attr, kwargs[self.conn_name_attr])
+            setattr(self, self.conn_name_attr, kwargs.get(self.conn_name_attr, self.default_conn_name))
+
+    @property
+    def conn_name(self) -> str:
+        """
+        Get the name of the database connection attribute which identifies the connection.
+        """
+        return getattr(self, self.conn_name_attr)
+
+    @property
+    def connection(self) -> Connection:
+        """
+        Get the database connection object.
+
+        :return: Connection
+        """
+        return super().get_connection(self.conn_name)

Review comment:
       I would prefer going for the first option and don't add another `connection` property. And use `self.get_connection(self.conn_name)` in hooks.




----------------------------------------------------------------
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