Skip to content

Commit af3e498

Browse files
authored
Merge pull request #538 from mapswipe/dev
Dev
2 parents 128f03b + 1c4a85c commit af3e498

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/transfer_results.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,34 @@ def save_results_to_postgres(results_file, project_id, filter_mode: bool):
298298
logger.warn(f"trying to remove invalid tasks from {project_id}.")
299299

300300
filter_query = """
301-
DELETE FROM results_temp
302-
where task_id in (
303-
select task_id from results_temp where task_id not in (
304-
select r.task_id from results_temp r join (
305-
select * from tasks where project_id = %(project_id)s
306-
) as t
307-
on r.group_id = t.group_id
308-
and r.task_id = t.task_id
309-
)
301+
with project_tasks as (
302+
select
303+
task_id
304+
,group_id
305+
from tasks
306+
where project_id = %(project_id)s
307+
),
308+
-- Results for which we can't join a task from the tasks table
309+
-- are invalid. For these invalid results the group_id set by the app
310+
-- is not correct. Hence, we delete these results from the
311+
-- results_temp table.
312+
results_to_delete as (
313+
select
314+
r.task_id
315+
,r.group_id
316+
,r.user_id
317+
from results_temp r
318+
left join project_tasks t on
319+
r.task_id = t.task_id and
320+
r.group_id = t.group_id
321+
where t.task_id is null or t.group_id is null
310322
)
323+
delete from results_temp r1
324+
using results_to_delete r2
325+
where
326+
r1.task_id = r2.task_id and
327+
r1.group_id = r2.group_id and
328+
r1.user_id = r2.user_id
311329
"""
312330
p_con.query(filter_query, {"project_id": project_id})
313331

0 commit comments

Comments
 (0)