This is an automated email from the ASF dual-hosted git repository.

xddeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/master by this push:
     new 74ed92b  Drop random.choice() in BaseHook.get_connection() (#12573)
74ed92b is described below

commit 74ed92b3ff34fa3b6af27c17f44ad4573b517bbd
Author: Xiaodong DENG <[email protected]>
AuthorDate: Tue Nov 24 06:34:08 2020 +0100

    Drop random.choice() in BaseHook.get_connection() (#12573)
    
    https://github.com/apache/airflow/pull/9067 made conn_id unique,
    and this is effective from 2.0.*.
    Due to this change, BaseHook.get_connections() will return a List of length 
1, or raise Exception.
    
    In such a case, we should simply always get the only element
    from the result from BaseHook.get_connections(),
    and drop random.choice() in BaseHook.get_connection(), which was only 
applicable for the earlier
    setting (multiple connections is allowed for single conn_id)
---
 airflow/hooks/base_hook.py | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/airflow/hooks/base_hook.py b/airflow/hooks/base_hook.py
index cb9f6dc..9042095 100644
--- a/airflow/hooks/base_hook.py
+++ b/airflow/hooks/base_hook.py
@@ -17,7 +17,6 @@
 # under the License.
 """Base class for all hooks"""
 import logging
-import random
 from typing import Any, List
 
 from airflow.models.connection import Connection
@@ -38,7 +37,7 @@ class BaseHook(LoggingMixin):
     @classmethod
     def get_connections(cls, conn_id: str) -> List[Connection]:
         """
-        Get all connections as an iterable.
+        Get all connections as an iterable, given the connection id.
 
         :param conn_id: connection id
         :return: array of connections
@@ -48,12 +47,12 @@ class BaseHook(LoggingMixin):
     @classmethod
     def get_connection(cls, conn_id: str) -> Connection:
         """
-        Get random connection selected from all connections configured with 
this connection id.
+        Get connection, given connection id.
 
         :param conn_id: connection id
         :return: connection
         """
-        conn = random.choice(cls.get_connections(conn_id))
+        conn = cls.get_connections(conn_id)[0]
         if conn.host:
             log.info(
                 "Using connection to: id: %s. Host: %s, Port: %s, Schema: %s, 
Login: %s, Password: %s, "

Reply via email to