-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.py
60 lines (51 loc) · 2.15 KB
/
migration.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
# /migtation.py
# create the tables for the application
from project.config import conn
from werkzeug.security import generate_password_hash
def migration():
"""
create the tables on the database
"""
cur = conn.cursor()
try:
# delete tables if they exist
cur.execute("DROP TABLE IF EXISTS users,requests;")
cur.execute("DROP TABLE IF EXISTS tokens;")
# create user table
users = """CREATE TABLE users(
id SERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(50) UNIQUE,
password_hash VARCHAR(100),
role INT
);"""
# create requests table
requests = """CREATE TABLE requests(
id SERIAL PRIMARY KEY,
title VARCHAR(50),
description TEXT,
request_type VARCHAR(50),
status VARCHAR(50),
feedback VARCHAR(50),
date_created TIMESTAMP,
user_id INT references users(id)
);"""
# create black listed tokens table
tokens = """CREATE TABLE tokens(
id SERIAL PRIMARY KEY,
expired_tokens VARCHAR(150)
);"""
cur.execute(users)
cur.execute(requests)
cur.execute(tokens)
password_hash = generate_password_hash('12345678')
create_user_admin = """INSERT INTO
users (email, first_name, last_name, password_hash, role)
VALUES ( '%s', '%s', '%s', '%s', %d)""" \
% ('[email protected]', 'jane', 'joseph', password_hash, 1)
cur.execute(create_user_admin)
conn.commit()
except Exception as e:
print('error', e)
migration()