This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_db.py
48 lines (43 loc) · 1.34 KB
/
setup_db.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
import psycopg2
import os
from dotenv import load_dotenv
load_dotenv()
# database connection
try:
conn = psycopg2.connect(database=os.environ.get('DB_NAME'),
host=os.environ.get('DB_HOST'),
user=os.environ.get('DB_USER'),
port=os.environ.get('DB_PORT'))
except:
print("unable to connect to db")
cursor = conn.cursor()
try:
cursor.execute(
"""
CREATE TABLE "prefixes"(
"prefix" VARCHAR(255) NOT NULL,
"carrier" VARCHAR(255) NULL,
"created_at" TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
"updated_at" TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL
);
CREATE INDEX "prefixes_carrier_index" ON
"prefixes"("carrier");
ALTER TABLE
"prefixes" ADD PRIMARY KEY("prefix");
CREATE TABLE "carriers"(
"name" VARCHAR(255) NOT NULL,
"created_at" TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
"updated_at" TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL
);
ALTER TABLE
"carriers" ADD PRIMARY KEY("name");
ALTER TABLE
"prefixes" ADD CONSTRAINT "prefixes_carrier_foreign" FOREIGN KEY("carrier") REFERENCES "carriers"("name");
""")
except:
print("unable to setup db")
# TODO: add script for loading initial carriers
# INSERT INTO "carriers"("name", "created_at", "updated_at") VALUES('smart', 'NOW()', 'NOW()')
conn.commit()
cursor.close()
conn.close()