-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_database.py
133 lines (113 loc) · 4.13 KB
/
create_database.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from posgres_manager import Client
from private import database_detail
a = Client(**database_detail)
list_of_commands = [
{'query': """
CREATE TABLE IF NOT EXISTS UserDetail (
id SERIAL PRIMARY KEY,
userID BIGINT NOT NULL UNIQUE,
first_name VARCHAR(100),
last_name VARCHAR(100),
user_name VARCHAR(50),
-- email VARCHAR(255),
phone_number VARCHAR(15),
credit BIGINT DEFAULT 0,
entered_with_refral_link BIGINT DEFAULT NULL,
number_of_invitations SMALLINT DEFAULT 0,
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_active BOOLEAN DEFAULT TRUE,
referral_link VARCHAR(50),
membership_status VARCHAR(50),
discount_code VARCHAR(50),
CONSTRAINT fk_referral FOREIGN KEY (entered_with_refral_link) REFERENCES UserDetail(userID) ON DELETE CASCADE
);
""", 'params': None},
{'query': """
CREATE TABLE IF NOT EXISTS DiscountCode (
discountID SERIAL PRIMARY KEY,
is_active BOOLEAN DEFAULT TRUE,
available_for_all_user BOOLEAN DEFAULT FALSE,
for_userID BIGINT DEFAULT NULL,
credit BIGINT DEFAULT 0 CHECK (credit > 0),
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
valid_until TIMESTAMP NOT NULL,
code VARCHAR(100) NOT NULL
);
""", 'params': None},
{'query': """
CREATE TABLE IF NOT EXISTS UseDiscount (
usediscountID SERIAL PRIMARY KEY,
discountID SMALLINT,
userID BIGINT,
code VARCHAR(100),
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_user FOREIGN KEY (userID) REFERENCES UserDetail(UserID) ON DELETE CASCADE,
CONSTRAINT fk_discount FOREIGN KEY (discountID) REFERENCES DiscountCode(discountID) ON DELETE CASCADE
);
""", 'params': None},
{'query': """
CREATE TABLE IF NOT EXISTS Course (
courseID SERIAL PRIMARY KEY,
status BOOLEAN DEFAULT TRUE,
content_type VARCHAR(50) NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
cover_type VARCHAR(10),
cover BYTEA,
media BYTEA,
channel_link TEXT,
channel_chat_id TEXT,
referral_requirement INT DEFAULT 0,
discount_percent_per_invite INT DEFAULT 0,
price BIGINT CHECK (price >= discount_percent),
discount_percent SMALLINT DEFAULT 0 CHECK (discount_percent >= 0) CHECK (discount_percent <= 100),
is_free BOOLEAN DEFAULT FALSE,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""", 'params': None},
{'query': """
CREATE TABLE IF NOT EXISTS Admin (
adminID SERIAL PRIMARY KEY,
userID BIGINT,
-- password_hash VARCHAR(255),
-- email VARCHAR(255) NOT NULL UNIQUE,
-- full_name VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP,
CONSTRAINT fk_user FOREIGN KEY (userID) REFERENCES UserDetail(UserID) ON DELETE CASCADE
);
""", 'params': None},
{'query': """
CREATE TABLE IF NOT EXISTS Invoice (
invoiceID SERIAL PRIMARY KEY,
userID BIGINT NOT NULL,
course_ID BIGINT,
discountID SMALLINT DEFAULT NULL,
amount BIGINT NOT NULL,
discount BIGINT DEFAULT 0,
payment_status VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
payment_method VARCHAR(50),
payment_for VARCHAR(50),
CONSTRAINT fk_user FOREIGN KEY (userID) REFERENCES UserDetail(userID) ON DELETE CASCADE,
CONSTRAINT fk_course FOREIGN KEY (course_ID) REFERENCES Course(courseID) ON DELETE CASCADE,
CONSTRAINT fk_discount FOREIGN KEY (discountID) REFERENCES DiscountCode(discountID) ON DELETE CASCADE
);""", 'params': None},
{'query': """
CREATE TABLE IF NOT EXISTS Accept_Private_Channel (
ID SERIAL PRIMARY KEY,
status BOOLEAN DEFAULT FALSE,
user_ID BIGINT NOT NULL,
course_ID BIGINT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
period_minut SMALLINT NOT NULL,
channel_chat_id BIGINT NOT NULL,
CONSTRAINT fk_user FOREIGN KEY (user_ID) REFERENCES UserDetail(userID) ON DELETE CASCADE,
CONSTRAINT fk_course FOREIGN KEY (course_ID) REFERENCES Course(courseID) ON DELETE CASCADE
);""", 'params': None},
]
def create():
result = a.execute('transaction', list_of_commands)
print(result)
# create()
# a.execute('transaction', [{'query': 'drop table UserDetail', 'params': None}])