Skip to content

Commit 7bbfafa

Browse files
committed
rename file
1 parent 5b91812 commit 7bbfafa

File tree

2 files changed

+130
-67
lines changed

2 files changed

+130
-67
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
from mapswipe_workers import auth
2+
from mapswipe_workers.definitions import logger, sentry
3+
4+
5+
def get_project_ids_from_postgres():
6+
"""Get project ids."""
7+
8+
p_con = auth.postgresDB()
9+
10+
query = """
11+
SELECT project_id FROM projects
12+
LIMIT 10
13+
"""
14+
data = p_con.retr_query(query)
15+
project_ids = []
16+
for item in data:
17+
project_ids.append(item[0])
18+
19+
logger.info("Got projects from postgres.")
20+
logger.info(project_ids)
21+
return project_ids
22+
23+
24+
def update_tasks_table(project_id: str):
25+
"""Remove duplicates in 'project_types_specifics' attribute in tasks table."""
26+
27+
logger.info(f"Start process for project: '{project_id}'")
28+
p_con = auth.postgresDB()
29+
30+
query = """
31+
UPDATE tasks
32+
SET project_type_specifics = project_type_specifics::jsonb
33+
#- '{projectId}'
34+
#- '{groupId}'
35+
#- '{taskId}'
36+
#- '{geometry}'
37+
#- '{wkt}'
38+
WHERE project_id = %(project_id)s
39+
"""
40+
try:
41+
p_con.query(query, {"project_id": project_id})
42+
logger.info(f"Updated tasks table for project '{project_id}'.")
43+
except Exception as e:
44+
sentry.capture_exception(e)
45+
sentry.capture_message(
46+
f"Could NOT update tasks table for project '{project_id}'."
47+
)
48+
logger.exception(e)
49+
logger.warning(f"Could NOT update tasks table for project '{project_id}'.")
50+
51+
52+
def update_groups_table(project_id: str):
53+
"""Remove duplicates in 'project_types_specifics' attribute in groups table."""
54+
55+
logger.info(f"Start process for project: '{project_id}'")
56+
p_con = auth.postgresDB()
57+
58+
# removing only the duplicated keys would be better
59+
"""
60+
select
61+
project_type_specifics::jsonb #- '{projectId}' #- '{groupId}'
62+
from tasks
63+
limit 10
64+
"""
65+
66+
query = """
67+
UPDATE groups
68+
SET project_type_specifics = project_type_specifics::jsonb
69+
#- '{projectId}'
70+
#- '{id}'
71+
#- '{requiredCount}'
72+
#- '{finishedCount}'
73+
#- '{neededCount}'
74+
#- '{reportCount}'
75+
#- '{distributedCount}'
76+
WHERE project_id = %(project_id)s
77+
"""
78+
try:
79+
p_con.query(query, {"project_id": project_id})
80+
logger.info(f"Updated tasks table for project '{project_id}'.")
81+
except Exception as e:
82+
sentry.capture_exception(e)
83+
sentry.capture_message(
84+
f"Could NOT update tasks table for project '{project_id}'."
85+
)
86+
logger.exception(e)
87+
logger.warning(f"Could NOT update tasks table for project '{project_id}'.")
88+
89+
90+
def run_vacuum_tasks_table():
91+
"""Run vacuum to reclaim storage."""
92+
logger.info("Start vacuum on tasks table.")
93+
p_con = auth.postgresDB()
94+
# isolation_level 0 will move you out of a transaction block
95+
old_isolation_level = p_con._db_connection.isolation_level
96+
p_con._db_connection.set_isolation_level(0)
97+
query = """
98+
VACUUM FULL tasks
99+
"""
100+
p_con.query(query)
101+
# set isolation_level back to initial value
102+
p_con._db_connection.set_isolation_level(old_isolation_level)
103+
logger.info("Finish vacuum on tasks table.")
104+
105+
106+
def run_vacuum_groups_table():
107+
"""Run vacuum to reclaim storage."""
108+
logger.info("Start vacuum on groups table.")
109+
p_con = auth.postgresDB()
110+
# isolation_level 0 will move you out of a transaction block
111+
old_isolation_level = p_con._db_connection.isolation_level
112+
p_con._db_connection.set_isolation_level(0)
113+
query = """
114+
VACUUM FULL groups
115+
"""
116+
p_con.query(query)
117+
# set isolation_level back to initial value
118+
p_con._db_connection.set_isolation_level(old_isolation_level)
119+
logger.info("Finish vacuum on groups table.")
120+
121+
122+
if __name__ == "__main__":
123+
project_ids_list = get_project_ids_from_postgres()
124+
for i, project_id in enumerate(project_ids_list):
125+
update_tasks_table(project_id)
126+
update_groups_table(project_id)
127+
# vacuum every 50th project
128+
if i % 50 == 0 or i == len(project_ids_list) - 1:
129+
run_vacuum_tasks_table()
130+
run_vacuum_groups_table()

mapswipe_workers/python_scripts/update_tasks_table.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)