Skip to content

Commit 7380bee

Browse files
Reverted changes that had overwritten the previous linting of this file.
1 parent bd3739c commit 7380bee

1 file changed

Lines changed: 67 additions & 69 deletions

File tree

docker/airflow/dags/run_dot_project.py

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121

2222
def get_object(
23-
object_name_in,
24-
earliest_date_to_sync,
25-
date_field,
26-
source_conn_in,
27-
columns_to_exclude,
23+
object_name_in,
24+
earliest_date_to_sync,
25+
date_field,
26+
source_conn_in,
27+
columns_to_exclude,
2828
):
2929
"""
3030
@@ -47,9 +47,18 @@ def get_object(
4747

4848
connection = BaseHook.get_connection(source_conn_in)
4949

50-
sql_stmt = "SELECT * FROM " + connection.schema + "." + object_name_in
50+
sql_stmt = (
51+
"SELECT * FROM "
52+
+ connection.schema
53+
+ "."
54+
+ object_name_in
55+
)
5156
if date_field != None:
52-
sql_stmt += " WHERE " + date_field + " >= '" + earliest_date_to_sync + "'"
57+
sql_stmt += (" WHERE "
58+
+ date_field
59+
+ " >= '"
60+
+ earliest_date_to_sync
61+
+ "'")
5362
print(sql_stmt)
5463
pg_hook = PostgresHook(postgres_conn_id=source_conn_in, schema=source_conn_in)
5564
pg_conn = pg_hook.get_conn()
@@ -58,13 +67,13 @@ def get_object(
5867
data = cursor.fetchall()
5968

6069
sql_stmt = (
61-
"SELECT column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"
62-
+ object_name_in
63-
+ "'"
64-
+ "AND table_schema = '"
65-
+ connection.schema
66-
+ "' "
67-
+ " ORDER BY ordinal_position "
70+
"SELECT column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"
71+
+ object_name_in
72+
+ "'"
73+
+ "AND table_schema = '"
74+
+ connection.schema
75+
+ "' "
76+
+ " ORDER BY ordinal_position "
6877
)
6978
print(sql_stmt)
7079
pg_hook = PostgresHook(postgres_conn_id=source_conn_in, schema=source_conn_in)
@@ -76,20 +85,16 @@ def get_object(
7685
# Fail back to views
7786
if len(columns) == 0:
7887
sql_stmt = (
79-
'SELECT a.attname as "column_name",'
80-
+ ' pg_catalog.format_type(a.atttypid, a.atttypmod) as "data_type" '
81-
+ " FROM pg_attribute a "
82-
+ " JOIN pg_class t on a.attrelid = t.oid "
83-
+ " JOIN pg_namespace s on t.relnamespace = s.oid "
84-
+ " WHERE a.attnum > 0 "
85-
+ " AND NOT a.attisdropped "
86-
+ " AND t.relname = '"
87-
+ object_name_in
88-
+ "' "
89-
+ " AND s.nspname = '"
90-
+ connection.schema
91-
+ "' "
92-
+ " ORDER BY a.attnum; "
88+
"SELECT a.attname as \"column_name\","
89+
+ " pg_catalog.format_type(a.atttypid, a.atttypmod) as \"data_type\" "
90+
+ " FROM pg_attribute a "
91+
+ " JOIN pg_class t on a.attrelid = t.oid "
92+
+ " JOIN pg_namespace s on t.relnamespace = s.oid "
93+
+ " WHERE a.attnum > 0 "
94+
+ " AND NOT a.attisdropped "
95+
+ " AND t.relname = '" + object_name_in + "' "
96+
+ " AND s.nspname = '" + connection.schema + "' "
97+
+ " ORDER BY a.attnum; "
9398
)
9499
print(sql_stmt)
95100
pg_hook = PostgresHook(postgres_conn_id=source_conn_in, schema=source_conn_in)
@@ -131,7 +136,7 @@ def get_object(
131136

132137

133138
def save_object(
134-
object_name_in, target_conn_in, data_in, column_list_in, type_list_in, source_db_in
139+
object_name_in, target_conn_in, data_in, column_list_in, type_list_in, source_db_in
135140
):
136141
"""
137142
@@ -159,16 +164,16 @@ def save_object(
159164

160165
connection = BaseHook.get_connection(target_conn_in)
161166
connection_string = (
162-
"postgresql://"
163-
+ str(connection.login)
164-
+ ":"
165-
+ str(connection.password)
166-
+ "@"
167-
+ str(connection.host)
168-
+ ":"
169-
+ str(connection.port)
170-
+ "/"
171-
+ target_conn_in
167+
"postgresql://"
168+
+ str(connection.login)
169+
+ ":"
170+
+ str(connection.password)
171+
+ "@"
172+
+ str(connection.host)
173+
+ ":"
174+
+ str(connection.port)
175+
+ "/"
176+
+ target_conn_in
172177
)
173178

174179
engine = create_engine(
@@ -185,7 +190,7 @@ def save_object(
185190
# This will also drop any DOT model views onto this data
186191
if MODE == "replace":
187192
with PostgresHook(
188-
postgres_conn_id=target_conn_in, schema=target_conn_in
193+
postgres_conn_id=target_conn_in, schema=target_conn_in
189194
).get_conn() as conn:
190195
cur = conn.cursor()
191196
query = f"DROP TABLE IF EXISTS {schema}.{object_name_in} CASCADE;"
@@ -197,7 +202,7 @@ def save_object(
197202

198203
# Test to see if schema exists, if not, create
199204
with PostgresHook(
200-
postgres_conn_id=target_conn_in, schema=target_conn_in
205+
postgres_conn_id=target_conn_in, schema=target_conn_in
201206
).get_conn() as conn:
202207
cur = conn.cursor()
203208
query = f"CREATE SCHEMA IF NOT EXISTS {schema};"
@@ -223,12 +228,12 @@ def save_object(
223228

224229

225230
def sync_object(
226-
object_name_in,
227-
earliest_date_to_sync,
228-
date_field,
229-
source_conn_in,
230-
target_conn_in,
231-
columns_to_exclude,
231+
object_name_in,
232+
earliest_date_to_sync,
233+
date_field,
234+
source_conn_in,
235+
target_conn_in,
236+
columns_to_exclude,
232237
):
233238
"""
234239
@@ -264,7 +269,6 @@ def sync_object(
264269
object_name_in, target_conn_in, data, column_list, type_list, source_conn_in
265270
)
266271

267-
268272
def drop_tables_in_dot_tests_schema(target_conn_in, schema_to_drop_from):
269273
"""
270274
We are syncing new data where new columns and columns types might change.
@@ -285,24 +289,21 @@ def drop_tables_in_dot_tests_schema(target_conn_in, schema_to_drop_from):
285289
"""
286290

287291
with PostgresHook(
288-
postgres_conn_id=target_conn_in, schema=target_conn_in
292+
postgres_conn_id=target_conn_in, schema=target_conn_in
289293
).get_conn() as conn:
290294
cur = conn.cursor()
291295
query1 = f"SET search_path TO {schema_to_drop_from}"
292-
query2 = (
293-
f"DO $$ DECLARE "
294-
f"r RECORD; "
295-
f"BEGIN "
296-
f"FOR r IN (SELECT table_name FROM information_schema.tables WHERE table_schema = current_schema()) "
297-
f"LOOP "
298-
f"EXECUTE 'DROP TABLE IF EXISTS ' || QUOTE_IDENT(r.table_name) || ' CASCADE'; "
299-
f"END LOOP; "
300-
f"END $$; "
301-
)
296+
query2 = f"DO $$ DECLARE " \
297+
f"r RECORD; " \
298+
f"BEGIN " \
299+
f"FOR r IN (SELECT table_name FROM information_schema.tables WHERE table_schema = current_schema()) " \
300+
f"LOOP " \
301+
f"EXECUTE 'DROP TABLE IF EXISTS ' || QUOTE_IDENT(r.table_name) || ' CASCADE'; " \
302+
f"END LOOP; " \
303+
f"END $$; "
302304
cur.execute(query1)
303305
cur.execute(query2)
304306

305-
306307
def run_dot_app(project_id_in):
307308
"""
308309
Method to run the DOT.
@@ -335,10 +336,10 @@ def default_config():
335336

336337

337338
with DAG(
338-
dag_id="run_dot_project",
339-
schedule_interval="@weekly",
340-
start_date=datetime(year=2022, month=3, day=1),
341-
catchup=False,
339+
dag_id="run_dot_project",
340+
schedule_interval="@weekly",
341+
start_date=datetime(year=2022, month=3, day=1),
342+
catchup=False,
342343
) as dag:
343344
config = json.loads(Variable.get("dot_config", default_var=default_config().read()))
344345

@@ -374,7 +375,7 @@ def default_config():
374375
python_callable=drop_tables_in_dot_tests_schema,
375376
op_kwargs={
376377
"target_conn_in": target_conn,
377-
"schema_to_drop_from": schema_to_drop_from,
378+
"schema_to_drop_from": schema_to_drop_from
378379
},
379380
dag=dag,
380381
)
@@ -384,10 +385,7 @@ def default_config():
384385
for i in range(len(objects_to_sync)):
385386

386387
object_name = objects_to_sync[i]["object"]
387-
if (
388-
"date_field" in objects_to_sync[i]
389-
and objects_to_sync[i]["date_field"] != ""
390-
):
388+
if "date_field" in objects_to_sync[i] and objects_to_sync[i]["date_field"] != "":
391389
date_field = objects_to_sync[i]["date_field"]
392390
else:
393391
date_field = None

0 commit comments

Comments
 (0)