|
35 | 35 | from airflow.secrets.cache import SecretCache |
36 | 36 | from airflow.secrets.metastore import MetastoreBackend |
37 | 37 | from airflow.utils.log.logging_mixin import LoggingMixin |
38 | | -from airflow.utils.module_loading import import_string |
39 | 38 | from airflow.utils.session import create_session |
40 | 39 |
|
41 | 40 | if TYPE_CHECKING: |
@@ -239,62 +238,45 @@ def set( |
239 | 238 | with ctx as session: |
240 | 239 | new_variable = Variable(key=key, val=stored_value, description=description) |
241 | 240 |
|
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 |
244 | 243 |
|
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 |
256 | 251 |
|
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 | + ) |
259 | 259 |
|
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( |
263 | 264 | val=val, |
264 | 265 | description=description, |
265 | 266 | is_encrypted=is_encrypted, |
266 | 267 | ) |
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( |
272 | 273 | val=val, |
273 | 274 | description=description, |
274 | 275 | 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 | + ) |
286 | 278 |
|
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) |
298 | 280 | # invalidate key in cache for faster propagation |
299 | 281 | # we cannot save the value set because it's possible that it's shadowed by a custom backend |
300 | 282 | # (see call to check_for_write_conflict above) |
|
0 commit comments