amoghrajesh commented on code in PR #65759: URL: https://github.com/apache/airflow/pull/65759#discussion_r3154063774
########## airflow-core/src/airflow/state/metastore.py: ########## @@ -0,0 +1,334 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from typing import TYPE_CHECKING + +from sqlalchemy import delete, select + +from airflow._shared.state import AssetScope, BaseStateBackend, StateScope, TaskScope +from airflow._shared.timezones import timezone +from airflow.models.asset_state import AssetStateModel +from airflow.models.dagrun import DagRun +from airflow.models.task_state import TaskStateModel +from airflow.utils.session import NEW_SESSION, create_session_async, provide_session +from airflow.utils.sqlalchemy import get_dialect_name + +if TYPE_CHECKING: + from sqlalchemy.dialects.mysql.dml import Insert as MySQLInsert + from sqlalchemy.dialects.postgresql.dml import Insert as PostgreSQLInsert + from sqlalchemy.dialects.sqlite.dml import Insert as SQLiteInsert + from sqlalchemy.ext.asyncio import AsyncSession + from sqlalchemy.orm import Session + + +def _build_upsert_stmt( + dialect: str | None, + model: type, + conflict_cols: list[str], + values: dict, + update_fields: dict, +): + """Return a dialect-specific INSERT ... ON CONFLICT UPDATE statement.""" + stmt: MySQLInsert | PostgreSQLInsert | SQLiteInsert + if dialect == "postgresql": + from sqlalchemy.dialects.postgresql import insert as pg_insert + + stmt = pg_insert(model).values(**values) + stmt = stmt.on_conflict_do_update(index_elements=conflict_cols, set_=update_fields) + elif dialect == "mysql": + from sqlalchemy.dialects.mysql import insert as mysql_insert + + stmt = mysql_insert(model).values(**values) + stmt = stmt.on_duplicate_key_update(**update_fields) + else: + from sqlalchemy.dialects.sqlite import insert as sqlite_insert + + stmt = sqlite_insert(model).values(**values) + stmt = stmt.on_conflict_do_update(index_elements=conflict_cols, set_=update_fields) + return stmt + + +class MetastoreStateBackend(BaseStateBackend): + """Default state backend for tasks and assets. Stores task and asset state in the Airflow metadata database.""" + + @provide_session + def get(self, scope: StateScope, key: str, *, session: Session = NEW_SESSION) -> str | None: + match scope: Review Comment: Ooh nice, didn't know about `assert_never`, handling it with that: ```python value = 10 match value: case 1: print("one") case 2: print("two") case _: assert_never(value) Traceback (most recent call last): File "/Users/amoghdesai/Documents/OSS/repos/airflow/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py", line 3747, in run_code exec(code_obj, self.user_global_ns, self.user_ns) ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<ipython-input-3-21b042fd93f7>", line 8, in <module> assert_never(value) ~~~~~~~~~~~~^^^^^^^ File "/opt/homebrew/Cellar/[email protected]/3.13.3_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 2627, in assert_never raise AssertionError(f"Expected code to be unreachable, but got: {value}") AssertionError: Expected code to be unreachable, but got: 10 ``` -- 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]
