-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_table.py
67 lines (52 loc) · 1.71 KB
/
create_table.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import argparse
from sqlalchemy import text
from utils import get_sqlalchemy_engine
def parse_args():
parser = argparse.ArgumentParser(
description="Create a weather table in Postgres."
)
parser.add_argument(
"--drop-table",
action="store_true",
default=False,
help="Drop the table if it exists beforehand."
)
parser.add_argument(
"--table-type",
choices=["regular", "hyper"],
help="Create a regular PostgreSQL table or a TimescaleDB hypertable.",
required=True
)
parser.add_argument(
"--unlogged",
action="store_true",
default=False,
help="Make the table unlogged."
)
return parser.parse_args()
def main(args):
engine = get_sqlalchemy_engine()
with engine.connect() as conn:
if args.drop_table:
conn.execute(text("drop table if exists weather;"))
unlogged = "unlogged" if args.unlogged else ""
table_creation_query = f"""--sql
create {unlogged} table if not exists weather (
time timestamptz not null,
location_id int,
latitude float4,
longitude float4,
temperature_2m float4,
zonal_wind_10m float4,
meridional_wind_10m float4,
total_cloud_cover float4,
total_precipitation float4,
snowfall float4
);
"""
conn.execute(text(table_creation_query))
if args.table_type == "hyper":
conn.execute(text("select create_hypertable('weather', 'time');"))
conn.commit()
if __name__ == "__main__":
main(parse_args())