uranusjr commented on a change in pull request #18161:
URL: https://github.com/apache/airflow/pull/18161#discussion_r716148625
##########
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:
This is the correct syntax. `next()` accepts an iterator, and either
returns the next element in the iterator, or raises `StopIteration` if there is
no next element (i.e. the iterator has been exhausted).
The thing inside `next()` is called an iterator comprehension. The entire
function call is roughly equivalent to
```python
found_conn_id_set = {connection_id for connection_id, in query}
possible_conn_ids = []
for connection_id in potential_connection_ids:
if connection_id not in found_conn_id_set:
possible_conn_ids.append(connection_id)
possible_conn_id_iter = iter(possible_conn_ids)
try:
new_conn_id = next(possible_conn_id_iter)
except StopIteration:
flash(...)
```
But with better performance.
--
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]