uranusjr commented on code in PR #62501:
URL: https://github.com/apache/airflow/pull/62501#discussion_r3425189902
##########
airflow-core/src/airflow/assets/manager.py:
##########
@@ -137,6 +138,44 @@ def _add_one(asset: SerializedAsset) -> AssetModel:
return [_add_one(a) for a in assets]
+ @classmethod
+ def create_asset_event(cls, *, session: Session, **event_kwargs) ->
AssetEvent:
+ """
+ Persist an :class:`AssetEvent` row and return it, bound to *session*.
+
+ On SQLite the event is added directly to the caller's *session* and
+ flushed. SQLite serialises writes at the database-file level: opening
+ a second connection here would compete with any write locks the
+ caller's transaction already holds (for example, an UPDATE on
+ ``dag_run`` flushed earlier in ``register_asset_changes_in_db``) and
+ deadlock with ``database is locked``.
+
+ On Postgres/MySQL a short-lived independent session is used so the
+ row is committed — and therefore visible to the scheduler's session
+ via MVCC — before the caller continues. The committed row is then
+ re-loaded into the caller's *session* so subsequent relationship
+ operations work correctly.
+ """
+ if get_dialect_name(session) == "sqlite":
+ asset_event = AssetEvent(**event_kwargs)
+ session.add(asset_event)
+ session.flush()
+ return asset_event
+
+ # Create a short-lived session to populate the asset event in db.
+ # This is to ensure the asset event is committed and visible to other
sessions
+ # (e.g. Scheduler's session when it looks for new asset events to
trigger dags via ADRQ).
+ # Use ``scoped=False`` to get a truly independent session with its own
connection/transaction.
+ with create_session(scoped=False) as ae_session:
+ _asset_event = AssetEvent(**event_kwargs)
+ ae_session.add(_asset_event)
+ ae_session.flush()
+ asset_event_id = _asset_event.id
Review Comment:
```suggestion
asset_event = AssetEvent(**event_kwargs)
ae_session.add(asset_event)
ae_session.flush()
asset_event_id = asset_event.id
```
--
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]