-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbupdate.py
executable file
·71 lines (57 loc) · 2 KB
/
dbupdate.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
#! /usr/bin/python
import pymysql.cursors
import argparse
from configobj import ConfigObj
from lib.dbcheck import check_db_version
def migrate_db():
version = check_db_version(cursor)
print("Old Version {}".format(version))
if version < 1:
cursor.execute("alter table `user` add column lat double default 0")
cursor.execute("alter table `user` add column lon double default 0")
cursor.execute("alter table `user` add column dist double default 0")
version = 1
if version < 2:
cursor.execute("alter table `userassign` add column level int default 0")
version = 2
if version < 3:
cursor.execute('create table userallow (`chatid` varchar(45) not null, primary key (`chatid`)) engine=InnoDB '
'default charset=utf8mb4;')
version = 3
cursor.execute("update dbversion set version = '%s'" % version)
version = check_db_version(cursor)
print("New Version {}".format(version))
print("Migration complete")
############# MAIN ################
##################
# parsing arguments
parser = argparse.ArgumentParser()
parser.add_argument("-c", dest='configfile', help="configfile", type=str, default="config.ini")
args = parser.parse_args()
# read inifile
try:
config = ConfigObj(args.configfile)
token = config.get('token')
db = config['dbname']
dbhost = config['dbhost']
dbport = config.get('dbport', '3306')
dbuser = config['dbuser']
dbpassword = config['dbpassword']
except:
print("Inifile not given")
quit()
# connect to database
#
try:
connection = pymysql.connect(host=dbhost,
user=dbuser,
password=dbpassword,
db=db,
port=int(dbport),
charset='utf8mb4',
autocommit='True')
cursor = connection.cursor()
except:
print("can not connect to database")
quit()
migrate_db()