-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_creator.py
183 lines (149 loc) · 5.9 KB
/
db_creator.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import argparse
import sys
import psycopg2
deafult_conn = psycopg2.connect(dbname='postgres', user='postgres', host='localhost', password='password')
my_db_conn = psycopg2.connect(dbname="meta_x5", user="postgres", host='localhost', password='password')
def create_db(conn):
dbname = "meta_x5"
conn.autocommit = True
cursor = conn.cursor()
cursor.execute('CREATE DATABASE ' + dbname)
cursor.close()
conn.close()
print(f'{dbname} created')
def delete_tables(conn):
conn.autocommit = True
cursor = conn.cursor()
cursor.execute('''DROP TABLE process''')
cursor.execute('''DROP TABLE process_param''')
cursor.execute('''DROP TABLE process_run_condition''')
cursor.execute('''DROP TABLE process_user''')
cursor.execute('''DROP TABLE process_quota''')
print('all tables deleted')
def clean_tables(conn):
conn.autocommit = True
cursor = conn.cursor()
cursor.execute('''DELETE FROM process''')
cursor.execute('''DELETE FROM process_param''')
cursor.execute('''DELETE FROM process_run_condition''')
cursor.execute('''DELETE FROM process_user''')
cursor.execute('''DELETE FROM process_quota''')
print('all tables cleaned')
def create_tables(conn):
conn.autocommit = True
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE process_param(
name TEXT PRIMARY KEY,
value TEXT
)''')
cursor.execute('''
CREATE TABLE process_run_condition(
type TEXT PRIMARY KEY,
value TEXT
)''')
cursor.execute('''
CREATE TABLE process_user(
id INTEGER PRIMARY KEY,
name TEXT,
description TEXT
)''')
cursor.execute('''
CREATE TABLE process_quota(
type TEXT PRIMARY KEY,
value INTEGER
)''')
cursor.execute('''
CREATE TABLE process(
id INTEGER PRIMARY KEY,
name TEXT,
description TEXT,
flag INTEGER,
parameter TEXT REFERENCES process_param (name),
run_condition TEXT REFERENCES process_run_condition (type),
userID INTEGER REFERENCES process_user (id),
qouta TEXT REFERENCES process_quota (type)
)''')
conn.commit()
cursor.close()
conn.close()
print(f'tables created')
def test_data(conn):
conn.autocommit = True
cursor = conn.cursor()
cursor.execute("""
INSERT INTO process_param(name, value)
VALUES
('param1', '100'),
('param2', '200'),
('param3', '300'),
('param4', '400'),
('param5', '500')
""")
cursor.execute("""
INSERT INTO process_run_condition(type, value)
VALUES
('time1', '10:00'),
('time2', '11:00'),
('time3', '12:00'),
('time4', '13:00'),
('time5', '14:00')
""")
cursor.execute("""
INSERT INTO process_user(id, name, description)
VALUES
(55, 'Flex_Fom', 'Best_boy'),
(44, 'Arny', 'Lol'),
(777, 'Hello', 'World'),
(12, 'Jony', 'Tot_samyi'),
(1337, 'Gpooo', 'description')
""")
cursor.execute("""
INSERT INTO process_quota(type, value)
VALUES
('aaa', 10),
('bbb', 7),
('ccc', 5),
('ddd', 2),
('eee', 15)
""")
cursor.execute('''
INSERT INTO process(
id, name, description, flag,
parameter, run_condition, userID, qouta
)
VALUES
(1, 'Start_server', 'first', 1, 'param1', 'time1', 1337, 'aaa'),
(2, 'Start_server2', 'second', 0, 'param2', 'time2', 12, 'bbb'),
(3, 'Stop_server', 'descr1', 0, 'param3', 'time3', 777, 'ccc'),
(4, 'Kek', 'lol', 0, 'param4', 'time4', 44, 'ddd'),
(5, 'Privet', 'da', 1, 'param5', 'time5', 55, 'eee')
''')
conn.commit()
cursor.close()
conn.close()
print(f'test data loaded')
def main():
main_arg_parser = argparse.ArgumentParser(description="parser for db")
subparsers = main_arg_parser.add_subparsers(title="subcommands", dest="subcommand")
subparsers.add_parser("create_db", help="parser for create db")
subparsers.add_parser("create_tab", help="parser for create tables")
subparsers.add_parser("clean", help="parser for clean tables")
subparsers.add_parser("del", help="parser for del tables")
subparsers.add_parser("test_data", help="parser for test data load")
args = main_arg_parser.parse_args()
if args.subcommand is None:
print("ERROR: specify either create, del or clean")
sys.exit(1)
if args.subcommand == "create_db":
create_db(deafult_conn)
elif args.subcommand == "create_tab":
create_tables(my_db_conn)
elif args.subcommand == "clean":
clean_tables(my_db_conn)
elif args.subcommand == "del":
delete_tables(my_db_conn)
elif args.subcommand == "test_data":
test_data(my_db_conn)
if __name__ == "__main__":
main()