forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
208 lines (161 loc) · 6.02 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#
# 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 airflow.auth.managers.fab.models import Permission, Resource, assoc_permission_role
from airflow.jobs.job import Job
from airflow.models import (
Connection,
DagModel,
DagRun,
DagTag,
DbCallbackRequest,
Log,
Pool,
RenderedTaskInstanceFields,
SlaMiss,
TaskFail,
TaskInstance,
TaskReschedule,
Trigger,
Variable,
XCom,
errors,
)
from airflow.models.dag import DagOwnerAttributes
from airflow.models.dagcode import DagCode
from airflow.models.dagwarning import DagWarning
from airflow.models.dataset import (
DagScheduleDatasetReference,
DatasetDagRunQueue,
DatasetEvent,
DatasetModel,
TaskOutletDatasetReference,
)
from airflow.models.serialized_dag import SerializedDagModel
from airflow.security.permissions import RESOURCE_DAG_PREFIX
from airflow.utils.db import add_default_pool_if_not_exists, create_default_connections, reflect_tables
from airflow.utils.session import create_session
def clear_db_runs():
with create_session() as session:
session.query(Job).delete()
session.query(Trigger).delete()
session.query(DagRun).delete()
session.query(TaskInstance).delete()
def clear_db_datasets():
with create_session() as session:
session.query(DatasetEvent).delete()
session.query(DatasetModel).delete()
session.query(DatasetDagRunQueue).delete()
session.query(DagScheduleDatasetReference).delete()
session.query(TaskOutletDatasetReference).delete()
def clear_db_dags():
with create_session() as session:
session.query(DagTag).delete()
session.query(DagOwnerAttributes).delete()
session.query(DagModel).delete()
def drop_tables_with_prefix(prefix):
with create_session() as session:
metadata = reflect_tables(None, session)
for table_name, table in metadata.tables.items():
if table_name.startswith(prefix):
table.drop(session.bind)
def clear_db_serialized_dags():
with create_session() as session:
session.query(SerializedDagModel).delete()
def clear_db_sla_miss():
with create_session() as session:
session.query(SlaMiss).delete()
def clear_db_pools():
with create_session() as session:
session.query(Pool).delete()
add_default_pool_if_not_exists(session)
def clear_db_connections(add_default_connections_back=True):
with create_session() as session:
session.query(Connection).delete()
if add_default_connections_back:
create_default_connections(session)
def clear_db_variables():
with create_session() as session:
session.query(Variable).delete()
def clear_db_dag_code():
with create_session() as session:
session.query(DagCode).delete()
def clear_db_callbacks():
with create_session() as session:
session.query(DbCallbackRequest).delete()
def set_default_pool_slots(slots):
with create_session() as session:
default_pool = Pool.get_default_pool(session)
default_pool.slots = slots
def clear_rendered_ti_fields():
with create_session() as session:
session.query(RenderedTaskInstanceFields).delete()
def clear_db_import_errors():
with create_session() as session:
session.query(errors.ImportError).delete()
def clear_db_dag_warnings():
with create_session() as session:
session.query(DagWarning).delete()
def clear_db_xcom():
with create_session() as session:
session.query(XCom).delete()
def clear_db_logs():
with create_session() as session:
session.query(Log).delete()
def clear_db_jobs():
with create_session() as session:
session.query(Job).delete()
def clear_db_task_fail():
with create_session() as session:
session.query(TaskFail).delete()
def clear_db_task_reschedule():
with create_session() as session:
session.query(TaskReschedule).delete()
def clear_dag_specific_permissions():
with create_session() as session:
dag_resources = session.query(Resource).filter(Resource.name.like(f"{RESOURCE_DAG_PREFIX}%")).all()
dag_resource_ids = [d.id for d in dag_resources]
dag_permissions = session.query(Permission).filter(Permission.resource_id.in_(dag_resource_ids)).all()
dag_permission_ids = [d.id for d in dag_permissions]
session.query(assoc_permission_role).filter(
assoc_permission_role.c.permission_view_id.in_(dag_permission_ids)
).delete(synchronize_session=False)
session.query(Permission).filter(Permission.resource_id.in_(dag_resource_ids)).delete(
synchronize_session=False
)
session.query(Resource).filter(Resource.id.in_(dag_resource_ids)).delete(synchronize_session=False)
def clear_all():
clear_db_runs()
clear_db_datasets()
clear_db_dags()
clear_db_serialized_dags()
clear_db_sla_miss()
clear_db_dag_code()
clear_db_callbacks()
clear_rendered_ti_fields()
clear_db_import_errors()
clear_db_dag_warnings()
clear_db_logs()
clear_db_jobs()
clear_db_task_fail()
clear_db_task_reschedule()
clear_db_xcom()
clear_db_variables()
clear_db_pools()
clear_db_connections(add_default_connections_back=True)
clear_dag_specific_permissions()