-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_db.py
More file actions
129 lines (98 loc) · 4.33 KB
/
update_db.py
File metadata and controls
129 lines (98 loc) · 4.33 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
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
#!/usr/bin/env python3
################################################################################
# #
# This file is part of JmpAPI. #
# #
# Copyright (c) 2014-2026, Cauldron Development Oy #
# All rights reserved. #
# #
# The JmpAPI Webserver is free software: you can redistribute #
# it and/or modify it under the terms of the GNU General Public #
# License as published by the Free Software Foundation, either #
# version 2 of the License, or (at your option) any later version. #
# #
# The JmpAPI Webserver is distributed in the hope that it will #
# be useful, but WITHOUT ANY WARRANTY; without even the implied #
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR #
# PURPOSE. See the GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this software. If not, see #
# <http://www.gnu.org/licenses/>. #
# #
# For information regarding this software email: #
# Joseph Coffland #
# joseph@cauldrondevelopment.com #
# #
################################################################################
# Process options
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-u', '--user', dest = 'user', help = 'DB user name',
default = 'root')
parser.add_option('', '--host', dest = 'host', help = 'DB host name',
default = 'localhost')
parser.add_option('-d', '--db', dest = 'db', help = 'DB name',
default = 'jmpapi')
parser.add_option('-r', '--reset', dest = 'reset', help = 'Reset DB',
default = False, action = 'store_true')
parser.add_option('-p', '--path', dest = 'path', help = 'Path to DB schema',
default = 'src/sql')
(options, args) = parser.parse_args()
# Ask pass
from getpass import getpass
db_pass = getpass('Password: ')
if db_pass == '': db_pass = None
# Connect to DB
from pymysql import connect
db = connect(host = options.host, user = options.user, password = db_pass,
database = options.db)
db.begin()
cur = db.cursor()
# Get schema version
version = None
try:
cur.execute('SELECT value FROM config WHERE name = "version"')
if cur.with_rows:
version = int(cur.fetchone()[0])
print('DB version', version)
except: pass
# Read updates
from glob import glob
import re
updateRE = re.compile(r'^.*-(\d+\.\d+\.\d+)\.sql')
updates = []
for path in glob(options.path + '/update-*.sql'):
m = updateRE.match(path)
v = int(m.group(1))
updates.append((v, path))
updates = sorted(updates)
# Latest version
if len(updates): latest = updates[-1]
else: latest = 0
# Update
def exec_file(filename):
sql = open(filename, 'r').read()
results = cur.executemany(sql, ())
if results:
for result in results:
if result.with_rows:
print(result.fetchall())
if version is None or options.reset:
# Init DB
exec_file(options.path + '/schema.sql')
exec_file(options.path + '/procedures.sql')
else:
# Apply DB updates
for v, path in updates:
if version < v:
print('Applying update', v)
exec_file(path)
# Update version
if version is None or version < latest or options.reset:
sql = 'REPLACE INTO config (name, value) VALUES ("version", "%s")'
cur.execute(sql % latest)
# Commit
cur.close()
db.commit()
db.close()