-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade-database.py
executable file
·65 lines (55 loc) · 1.87 KB
/
upgrade-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
#!/usr/bin/python
import pymysql.cursors
import re
import sys
import os
from os import walk
def main():
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='tdb',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
db_version = ''
try:
with connection.cursor() as cursor:
sql = "select version from ecs order by version desc limit 1;"
cursor.execute(sql)
db_version = cursor.fetchone().get("version")
except:
connection.close()
sys.exit()
files = []
for (dirpath, dirnames, filenames) in walk('./upgrades'):
files.extend(filenames)
break
files.sort()
for file in files:
match = re.search('^(\d(?:\.\d+)+)(\.|\ )+?\w+\.sql$', file)
found = match.group(1) if match else None
print("Found is " + found)
print("db_version is " + str(db_version))
if found is None:
continue
if found < db_version:
continue
if found > db_version:
new_db_version = found
try:
with connection.cursor() as cursor:
sql = open('./upgrades/' + file, 'r').read()
cursor.execute(sql)
sql = "update ecs set version=%s"
cursor.execute(sql, new_db_version)
except:
connection.rollback()
connection.close()
raise
finally:
connection.close()
if found == db_version:
print("I have made all of the upgrades to the current version. Exitting.");
sys.exit()
if __name__ == '__main__':
main()