Skip to content

Commit 650f175

Browse files
committed
Simplify insert function imports with static approach
1 parent 15df12d commit 650f175

1 file changed

Lines changed: 28 additions & 46 deletions

File tree

airflow-core/src/airflow/models/variable.py

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from airflow.secrets.cache import SecretCache
3636
from airflow.secrets.metastore import MetastoreBackend
3737
from airflow.utils.log.logging_mixin import LoggingMixin
38-
from airflow.utils.module_loading import import_string
3938
from airflow.utils.session import create_session
4039

4140
if TYPE_CHECKING:
@@ -239,62 +238,45 @@ def set(
239238
with ctx as session:
240239
new_variable = Variable(key=key, val=stored_value, description=description)
241240

242-
# Perform dialect-specific upsert operation
243-
dialect_name = session.get_bind().dialect.name
241+
val = new_variable._val
242+
is_encrypted = new_variable.is_encrypted
244243

245-
# Map of dialect names to their corresponding module paths
246-
dialect_insert_map = {
247-
"postgresql": "sqlalchemy.dialects.postgresql.insert",
248-
"mysql": "sqlalchemy.dialects.mysql.insert",
249-
"sqlite": "sqlalchemy.dialects.sqlite.insert",
250-
}
251-
252-
# Use SQLAlchemy Core for supported dialects
253-
if dialect_name in dialect_insert_map:
254-
val = new_variable._val
255-
is_encrypted = new_variable.is_encrypted
244+
# Import dialect-specific insert function
245+
if (dialect_name := session.get_bind().dialect.name) == "postgresql":
246+
from sqlalchemy.dialects.postgresql import insert
247+
elif dialect_name == "mysql":
248+
from sqlalchemy.dialects.mysql import insert
249+
else:
250+
from sqlalchemy.dialects.sqlite import insert
256251

257-
# Dynamically import dialect-specific insert function
258-
insert = import_string(dialect_insert_map[dialect_name])
252+
# Create the insert statement (common for all dialects)
253+
stmt = insert(Variable).values(
254+
key=key,
255+
val=val,
256+
description=description,
257+
is_encrypted=is_encrypted,
258+
)
259259

260-
# Create the insert statement (common for all dialects)
261-
stmt = insert(Variable).values(
262-
key=key,
260+
# Apply dialect-specific upsert
261+
if dialect_name == "mysql":
262+
# MySQL: ON DUPLICATE KEY UPDATE
263+
stmt = stmt.on_duplicate_key_update(
263264
val=val,
264265
description=description,
265266
is_encrypted=is_encrypted,
266267
)
267-
268-
# Apply dialect-specific upsert
269-
if dialect_name == "mysql":
270-
# MySQL: ON DUPLICATE KEY UPDATE
271-
stmt = stmt.on_duplicate_key_update(
268+
else:
269+
# PostgreSQL and SQLite: ON CONFLICT DO UPDATE
270+
stmt = stmt.on_conflict_do_update(
271+
index_elements=["key"],
272+
set_=dict(
272273
val=val,
273274
description=description,
274275
is_encrypted=is_encrypted,
275-
)
276-
else:
277-
# PostgreSQL and SQLite: ON CONFLICT DO UPDATE
278-
stmt = stmt.on_conflict_do_update(
279-
index_elements=["key"],
280-
set_=dict(
281-
val=val,
282-
description=description,
283-
is_encrypted=is_encrypted,
284-
),
285-
)
276+
),
277+
)
286278

287-
session.execute(stmt)
288-
else:
289-
# Default implementation using SQLAlchemy ORM for non-supported dialects
290-
existing_var = session.query(Variable).filter(Variable.key == key).first()
291-
if existing_var:
292-
existing_var.val = stored_value
293-
existing_var.description = description
294-
else:
295-
session.add(new_variable)
296-
297-
session.flush()
279+
session.execute(stmt)
298280
# invalidate key in cache for faster propagation
299281
# we cannot save the value set because it's possible that it's shadowed by a custom backend
300282
# (see call to check_for_write_conflict above)

0 commit comments

Comments
 (0)