forked from Team-DDev/bitsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpremain_database_initialization.py
More file actions
87 lines (67 loc) · 2.36 KB
/
premain_database_initialization.py
File metadata and controls
87 lines (67 loc) · 2.36 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
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
import os
import time
import mariadb
import secret
FLAGS = _ = None
DEBUG = False
STIME = time.time()
def main():
if DEBUG:
print(f'Parsed arguments {FLAGS}')
print(f'Unparsed arguments {_}')
try:
conn = mariadb.connect(user=secret.db_username,
password=secret.db_password,
host=secret.db_host,
port=secret.db_port,
database=secret.db_databasename)
cur = conn.cursor()
except mariadb.Error as e:
print(f'Error connecting to MariaDB: {e}')
sys.exit(0)
if DEBUG:
print(f'[{int(time.time()-STIME)}] Connect to database')
if FLAGS.reset:
cur.execute('''DROP TABLE IF EXISTS blk;''')
if DEBUG:
print(f'[{int(time.time()-STIME)}] DROP all tables of {secret.db_databasename}')
cur.execute('''CREATE TABLE blk (
id INT NOT NULL,
blkhash CHAR(64) NOT NULL,
miningtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE (blkhash)
);''')
cur.execute('''CREATE INDEX idx_miningtime ON blk (miningtime);''')
conn.commit()
cur.execute('''SHOW TABLES;''')
res = cur.fetchall()
tables = []
if DEBUG:
print(f'[{int(time.time()-STIME)}] CREATE TABLES:')
for row in res:
tables.append(row[0])
if DEBUG:
print(f'[{int(time.time()-STIME)}] > {table}')
if DEBUG:
for table in tables:
print(f'[{int(time.time()-STIME)}] > DESCRIBE {table}')
cur.execute(f'''DESCRIBE {table};''')
res = cur.fetchall()
for row in res:
print(f'[{int(time.time()-STIME)}] >> {row}')
conn.commit()
conn.close()
if __name__ == '__main__':
import argparse
root_path = os.path.abspath(__file__)
root_dir = os.path.dirname(root_path)
os.chdir(root_dir)
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true',
help='The present debug message')
parser.add_argument('--reset', action='store_true',
help='Clear exists tables before creation')
FLAGS, _ = parser.parse_known_args()
DEBUG = FLAGS.debug
main()