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



##########
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:
       ```suggestion
               if not potential_connection_ids:
                   # No connections available
                       flash(
                           f"Connection {new_conn_id} can't be added because it 
already exists, "
                           f"Please rename the existing connections",
                           "warning",
                       )
   ```




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