-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_partitions.py
43 lines (33 loc) · 1.17 KB
/
update_partitions.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
import logging
from datetime import date, timedelta
from sqlalchemy import inspect, text
from server.sources import engine, session
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
def run():
session.commit()
today = date.today()
def part_name(day: date):
return f'stop_times_{day.strftime("%Y%m%d")}'
for i in range(3):
day: date = today + timedelta(days=i)
day_after: date = day + timedelta(days=1)
partition_name = part_name(day)
if not inspect(engine).has_table(partition_name):
session.execute(text(f"CREATE TABLE {partition_name} PARTITION OF stop_times FOR VALUES FROM ('{day}') TO ('{day_after}')"))
session.commit()
# start from the day before yesterday for detaching partitions
i = 2
while True:
day = today - timedelta(days=i)
try:
session.execute(text(f'ALTER TABLE stop_times DETACH PARTITION {part_name(day)}'))
session.commit()
except:
session.rollback()
break
i += 1
if __name__ == '__main__':
run()