jose-lehmkuhl commented on code in PR #51056:
URL: https://github.com/apache/airflow/pull/51056#discussion_r2107694148


##########
providers/postgres/tests/system/postgres/example_postgres_cdc_asset_watcher.py:
##########
@@ -0,0 +1,97 @@
+# 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 datetime import datetime, timezone
+
+import psycopg2
+
+from airflow.hooks.base import BaseHook
+from airflow.models import Variable
+from airflow.providers.postgres.triggers.postgres_cdc import 
PostgresCDCEventTrigger
+from airflow.sdk import DAG, Asset, AssetWatcher, task
+
+"""
+Example DAG that demonstrates how to use the PostgresCDCEventTrigger.
+
+This DAG sets up an AssetWatcher linked to a PostgreSQL CDC trigger, which 
monitors changes
+in a specific table based on a timestamp column (`cdc_column`). When a change 
is detected, it executes a task
+that processes new rows and updates the Airflow Variable used to track CDC 
state (`state_key`).
+
+The state variable is configurable via `state_key`.
+"""
+
+STATE_KEY = "my_table_cdc_last_value"
+
+cdc_trigger = PostgresCDCEventTrigger(
+    conn_id="postgres_default",
+    table="my_table",
+    cdc_column="updated_at",
+    polling_interval=20,
+    state_key=STATE_KEY,
+)
+
+cdc_asset = Asset(
+    "postgres_cdc_asset", watchers=[AssetWatcher(name="postgres_cdc_watcher", 
trigger=cdc_trigger)]
+)
+
+with DAG(
+    dag_id="example_postgres_cdc_watcher",
+    schedule=[cdc_asset],
+    catchup=False,
+    max_active_runs=1,
+) as dag:
+
+    @task
+    def extract_and_update_last_value():
+        """
+        Extracts new rows from the monitored table that have `updated_at` 
greater than the
+        last recorded value in Airflow Variable, then updates the Variable 
with the new maximum.
+        """
+        conn = BaseHook.get_connection("postgres_default")
+
+        with psycopg2.connect(
+            host=conn.host, port=conn.port, dbname=conn.schema, 
user=conn.login, password=conn.password
+        ) as pg_conn:
+            with pg_conn.cursor() as cursor:
+                last_value = Variable.get(STATE_KEY, default_var=None)

Review Comment:
   I've added a @property for state_key so users can access it directly. Thanks 
for the suggestion!



-- 
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]

Reply via email to