Skip to content

Commit b781e7b

Browse files
committed
add script to remove project types specifics attribute for all tasks
1 parent 0ec1250 commit b781e7b

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
"""
13+
data = p_con.retr_query(query)
14+
project_ids = []
15+
for item in data:
16+
project_ids.append(item[0])
17+
18+
logger.info("Got projects from postgres.")
19+
logger.info(project_ids)
20+
return project_ids
21+
22+
23+
def update_tasks_table(project_id: str):
24+
"""Set 'project_types_specifics' attribute to NULL in tasks table."""
25+
26+
logger.info(f"Start process for project: '{project_id}'")
27+
p_con = auth.postgresDB()
28+
query = """
29+
UPDATE tasks
30+
SET project_type_specifics = NULL
31+
WHERE project_id = %(project_id)s
32+
"""
33+
try:
34+
p_con.query(query, {"project_id": project_id})
35+
logger.info(f"Updated tasks table for project '{project_id}'.")
36+
except Exception as e:
37+
sentry.capture_exception(e)
38+
sentry.capture_message(
39+
f"Could NOT update tasks table for project '{project_id}'."
40+
)
41+
logger.exception(e)
42+
logger.warning(f"Could NOT update tasks table for project '{project_id}'.")
43+
44+
45+
def run_vacuum_tasks_table():
46+
"""Run vacuum to reclaim storage."""
47+
logger.info("Start vacuum on tasks table.")
48+
p_con = auth.postgresDB()
49+
# isolation_level 0 will move you out of a transaction block
50+
old_isolation_level = p_con._db_connection.isolation_level
51+
p_con._db_connection.set_isolation_level(0)
52+
query = """
53+
VACUUM FULL tasks
54+
"""
55+
p_con.query(query)
56+
# set isolation_level back to initial value
57+
p_con._db_connection.set_isolation_level(old_isolation_level)
58+
logger.info("Finish vacuum on tasks table.")
59+
60+
61+
if __name__ == "__main__":
62+
project_ids_list = get_project_ids_from_postgres()
63+
for i, project_id in enumerate(project_ids_list):
64+
update_tasks_table(project_id)
65+
# vacuum every 50th project
66+
if i % 25 == 0 or i == len(project_ids_list):
67+
run_vacuum_tasks_table()

0 commit comments

Comments
 (0)