uranusjr commented on a change in pull request #20838:
URL: https://github.com/apache/airflow/pull/20838#discussion_r783671395



##########
File path: airflow/utils/metastore_cleanup.py
##########
@@ -0,0 +1,289 @@
+# 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.
+
+"""
+orm_model: the table
+recency_column: date column to filter by
+keep_last: whether the last record should be kept even if it's older than 
clean_before_timestamp
+keep_last_filters:
+keep_last_group_by: if keeping the last record, can keep the last record for 
each group
+"""
+
+
+from pendulum import DateTime
+from sqlalchemy.orm import Query
+
+from airflow import settings
+from airflow.configuration import conf
+from airflow.models import (
+    DagModel,
+    DagRun,
+    ImportError,
+    Log,
+    RenderedTaskInstanceFields,
+    SlaMiss,
+    TaskFail,
+    TaskInstance,
+    XCom,
+)
+
+try:
+    from airflow.jobs import BaseJob
+except Exception as e:
+    from airflow.jobs.base_job import BaseJob
+
+import logging
+
+from sqlalchemy import and_, func
+
+from airflow.models import TaskReschedule
+from airflow.utils import timezone
+
+now = timezone.utcnow
+
+
+objects = {
+    'job': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': BaseJob,
+        'recency_column': BaseJob.latest_heartbeat,
+    },
+    'dag': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': DagModel,
+        'recency_column': DagModel.last_parsed_time,
+    },
+    'dag_run': {
+        'keep_last': True,
+        "keep_last_filters": [DagRun.external_trigger.is_(False)],
+        "keep_last_group_by": DagRun.dag_id,
+        'orm_model': DagRun,
+        'recency_column': DagRun.execution_date,
+    },
+    'import_error': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': ImportError,
+        'recency_column': ImportError.timestamp,
+    },
+    'log': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': Log,
+        'recency_column': Log.dttm,
+    },
+    'rendered_task_instance_fields': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': RenderedTaskInstanceFields,
+        'recency_column': RenderedTaskInstanceFields.execution_date,
+    },
+    'sla_miss': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': SlaMiss,
+        'recency_column': SlaMiss.execution_date,
+    },
+    'task_fail': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': TaskFail,
+        'recency_column': TaskFail.execution_date,
+    },
+    'task_instance': {
+        'keep_last': False,
+        'keep_last_filters': None,
+        'keep_last_group_by': None,
+        'orm_model': TaskInstance,
+        'recency_column': TaskInstance.execution_date,

Review comment:
       This should work, there are existing code doing it. The association 
proxy does not always work like real columns, but does work for this specific 
usage.




-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to