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 2023/01/05 14:52:28 UTC

[GitHub] [airflow] natanweinberger commented on a diff in pull request #28738: Add --overwrite option to `connections import` CLI command

natanweinberger commented on code in PR #28738:
URL: https://github.com/apache/airflow/pull/28738#discussion_r1062560512


##########
airflow/cli/commands/connection_command.py:
##########
@@ -301,19 +301,26 @@ def connections_delete(args):
 def connections_import(args):
     """Imports connections from a file."""
     if os.path.exists(args.file):
-        _import_helper(args.file)
+        _import_helper(args.file, args.overwrite)
     else:
         raise SystemExit("Missing connections file.")
 
 
-def _import_helper(file_path):
-    """Load connections from a file and save them to the DB. On collision, skip."""
+def _import_helper(file_path, overwrite):
+    """
+    Load connections from a file and save them to the DB.
+    If `overwrite` is set to true, overwrite on collision.
+    """
     connections_dict = load_connections_dict(file_path)
     with create_session() as session:
         for conn_id, conn in connections_dict.items():
-            if session.query(Connection).filter(Connection.conn_id == conn_id).first():
+            conn_id_already_exists = session.query(Connection).filter(Connection.conn_id == conn_id).first()
+            if not overwrite and conn_id_already_exists:
                 print(f"Could not import connection {conn_id}: connection already exists.")
                 continue
+            if overwrite and conn_id_already_exists:
+                session.delete(conn_id_already_exists)
+                session.flush()
 
             session.add(conn)

Review Comment:
   That's fair. I tried to get updates working, but ran into IntegrityErrors a bunch due to how flushes work. I fell back on delete/flush/create, but I'll take another look today to see if I can get update working.
   
   Thanks for the review!



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