-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_config.py
More file actions
59 lines (44 loc) · 1.5 KB
/
db_config.py
File metadata and controls
59 lines (44 loc) · 1.5 KB
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
import psycopg2
import os
# url = os.getenv('DATABASE_URL')
# test_url = os.getenv('DATABASE_URL')
url = "dbname='ireporter' host='localhost' port='5432' user='issa' password='Maina9176'"
def connection(url):
conn = psycopg2.connect(url)
return conn
def init_db():
conn = connection(url)
return conn
# def init_test_db():
# conn = connection(test_url)
# return conn
def create_tables():
conn = connection(url)
curr = conn.cursor()
queries = tables()
for query in queries:
curr.execute(query)
conn.commit()
def destroy_tables():
pass
def tables():
users = """CREATE TABLE IF NOT EXISTS users(
user_id serial PRIMARY KEY NOT NULL,
first_name character varying(50) NOT NULL,
last_name character varying(50) NOT NULL,
username character varying(50) NOT NULL,
email character varying(50) NOT NULL,
date_created timestamp with time zone DEFAULT ('now'::text):: date NOT NULL,
password character varying(50) NOT NULL
)"""
incidents = """CREATE TABLE IF NOT EXISTS incidents (
incident_id serial PRIMARY KEY NOT NULL,
created_by numeric NOT NULL,
type character varying(20) NOT NULL,
description character varying (200) NOT NULL,
status character varying(20) DEFAULT 0,
location character varying(50) NOT NULL,
created_on timestamp with time zone DEFAULT ('now'::text):: date NOT NULL
)"""
queries = [users, incidents]
return queries