Skip to content

Commit 0ec69f1

Browse files
committed
Consolidate Pydantic to ORM update in connections API
1 parent bc917f0 commit 0ec69f1

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

airflow-core/src/airflow/api_fastapi/core_api/routes/public/connections.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
)
4545
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
4646
from airflow.api_fastapi.core_api.security import requires_access_connection
47-
from airflow.api_fastapi.core_api.services.public.connections import BulkConnectionService
47+
from airflow.api_fastapi.core_api.services.public.connections import (
48+
BulkConnectionService,
49+
update_orm_from_pydantic,
50+
)
4851
from airflow.api_fastapi.logging.decorators import action_logging
4952
from airflow.configuration import conf
5053
from airflow.models import Connection
@@ -200,23 +203,7 @@ def patch_connection(
200203
raise RequestValidationError(errors=e.errors())
201204

202205
# Not all fields match and some need setters, therefore copy manually
203-
if not update_mask or "conn_type" in update_mask:
204-
connection.conn_type = patch_body.conn_type
205-
if not update_mask or "description" in update_mask:
206-
connection.description = patch_body.description
207-
if not update_mask or "host" in update_mask:
208-
connection.host = patch_body.host
209-
if not update_mask or "schema" in update_mask:
210-
connection.schema = patch_body.schema_
211-
if not update_mask or "login" in update_mask:
212-
connection.login = patch_body.login
213-
if not update_mask or "password" in update_mask:
214-
connection.set_password(patch_body.password)
215-
if not update_mask or "port" in update_mask:
216-
connection.port = patch_body.port
217-
if not update_mask or "extra" in update_mask:
218-
connection.set_extra(patch_body.extra)
219-
206+
update_orm_from_pydantic(connection, patch_body, update_mask)
220207
return connection
221208

222209

airflow-core/src/airflow/api_fastapi/core_api/services/public/connections.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@
3434
from airflow.models.connection import Connection
3535

3636

37+
def update_orm_from_pydantic(
38+
orm_conn: Connection, pydantic_conn: ConnectionBody, update_mask: list[str] | None = None
39+
):
40+
"""Update ORM object from Pydantic object."""
41+
# Not all fields match and some need setters, therefore copy manually
42+
if not update_mask or "conn_type" in update_mask:
43+
orm_conn.conn_type = pydantic_conn.conn_type
44+
if not update_mask or "description" in update_mask:
45+
orm_conn.description = pydantic_conn.description
46+
if not update_mask or "host" in update_mask:
47+
orm_conn.host = pydantic_conn.host
48+
if not update_mask or "schema" in update_mask:
49+
orm_conn.schema = pydantic_conn.schema_
50+
if not update_mask or "login" in update_mask:
51+
orm_conn.login = pydantic_conn.login
52+
if not update_mask or "password" in update_mask:
53+
orm_conn.set_password(pydantic_conn.password)
54+
if not update_mask or "port" in update_mask:
55+
orm_conn.port = pydantic_conn.port
56+
if not update_mask or "extra" in update_mask:
57+
orm_conn.set_extra(pydantic_conn.extra)
58+
59+
3760
class BulkConnectionService(BulkService[ConnectionBody]):
3861
"""Service for handling bulk operations on connections."""
3962

@@ -118,14 +141,7 @@ def handle_bulk_update(
118141
ConnectionBody(**connection.model_dump())
119142

120143
# Not all fields match and some need setters, therefore copy manually
121-
old_connection.conn_type = connection.conn_type
122-
old_connection.description = connection.description
123-
old_connection.host = connection.host
124-
old_connection.schema = connection.schema_
125-
old_connection.login = connection.login
126-
old_connection.set_password(connection.password)
127-
old_connection.port = connection.port
128-
old_connection.set_extra(connection.extra)
144+
update_orm_from_pydantic(old_connection, connection)
129145
results.success.append(connection.connection_id)
130146

131147
except HTTPException as e:

0 commit comments

Comments
 (0)