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



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

Review comment:
       ```python
   potential_connection_ids = {f"{base_conn_id}_copy{i}" for i in range(1, 11)}
   query = (
       session.query(Connection.connection_id)
       .filter(Connection.connection_id.in_(potential_connection_ids)
   )
   potential_connection_ids.symmetric_difference_update(connection_id for 
connection_id, in query)
   ```




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to