-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_generate.py
51 lines (45 loc) · 1.63 KB
/
db_generate.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
import psycopg2
import sys
import datetime
from config import DB
def flush_db(con):
cur = con.cursor()
cur.execute('DROP DATABASE IF EXISTS {};'.format(DB['dbname']))
def create_db(con):
cur = con.cursor()
cur.execute('CREATE DATABASE {};'.format(DB['dbname']))
con.close()
con = psycopg2.connect(host=sys.argv[1], user=DB['username'], password=DB['password'], dbname=DB['dbname'])
con.autocommit = True
cur = con.cursor()
cur.execute("""
CREATE TABLE cookies (
id SERIAL UNIQUE,
url varchar(255) NOT NULL,
cookie text NOT NULL,
CONSTRAINT cookies_pk PRIMARY KEY (id)
);
CREATE TABLE urls (
id SERIAL UNIQUE,
url varchar(255) NOT NULL,
url_source varchar(255) NOT NULL,
datetime timestamp NOT NULL,
checked integer NOT NULL,
CONSTRAINT url UNIQUE (url) NOT DEFERRABLE INITIALLY IMMEDIATE,
CONSTRAINT urls_pk PRIMARY KEY (id)
);
ALTER TABLE cookies ADD CONSTRAINT cookies_urls
FOREIGN KEY (url)
REFERENCES urls (url)
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE
INITIALLY IMMEDIATE
;""")
cur.execute("INSERT INTO urls (url, url_source, datetime, checked) VALUES ('https://google.com', 'Manual', '{}', 0);".format(datetime.datetime.now()))
con.close()
if __name__ == "__main__":
con = psycopg2.connect(host=sys.argv[1], user=DB['username'], password=DB['password'], dbname='postgres')
con.autocommit = True
flush_db(con)
create_db(con)