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 2021/10/02 13:44:50 UTC

[GitHub] [airflow] subkanthi commented on a change in pull request #18161: Duplicate Connection: Added logic to query if a connection id exists before creating one

subkanthi commented on a change in pull request #18161:
URL: https://github.com/apache/airflow/pull/18161#discussion_r720681535



##########
File path: airflow/www/views.py
##########
@@ -3350,39 +3350,67 @@ def action_muldelete(self, items):
             (permissions.ACTION_CAN_READ, permissions.RESOURCE_CONNECTION),
         ]
     )
+
     def action_mulduplicate(self, connections, session=None):
         """Duplicate Multiple connections"""
         for selected_conn in connections:
             new_conn_id = selected_conn.conn_id
             match = re.search(r"_copy(\d+)$", selected_conn.conn_id)
+
+            base_conn_id = selected_conn.conn_id
             if match:
-                conn_id_prefix = selected_conn.conn_id[: match.start()]
-                new_conn_id = f"{conn_id_prefix}_copy{int(match.group(1)) + 1}"
+                base_conn_id = base_conn_id.split('_copy')[0]
+
+            potential_connection_ids = []
+            # Before creating a new connection
+            # Query to see if connection exists.
+            sql_query_string = 'select conn_id from connection where '
+            for i in range(1, 11):
+                connection_id = f'{base_conn_id}_copy{i}'
+                potential_connection_ids.append(connection_id)
+                sql_query_string += f"conn_id='{connection_id}'"
+                if i < 10:
+                    sql_query_string += f" or "
+
+            results = session.execute(sql_query_string)
+
+            for result in results:
+                for column, value in result.items():
+                    potential_connection_ids.remove(value)
+
+            if len(potential_connection_ids) == 0:
+                # No connections available
+                    flash(
+                        f"Connection {new_conn_id} can't be added because it already exists, "
+                        f"Please rename the existing connections",
+                        "warning",
+                    )

Review comment:
       Thanks, made the change, I now understand the next is really just to get the first element




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

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org