-
Notifications
You must be signed in to change notification settings - Fork 1
/
MySQLSchema.py
337 lines (277 loc) · 13.8 KB
/
MySQLSchema.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import re
import pymysql
class MySQLSchema():
def __init__(self, conn):
self._conn = conn
#-----------------------------------------------------------------------------------------------------------
def _get_table_columns(self, table_name):
table_columns = None
with self._conn.cursor() as cursor:
sql = "SHOW COLUMNS FROM {0}".format(table_name)
try:
cursor.execute(sql)
result = cursor.fetchall()
table_columns = {}
prev_name = None
for row in result:
name = row[0]
type = row[1]
is_notnull = (row[2] == 'NO')
is_in_primary_key = (row[3] == 'PRI')
default_value = row[4]
is_autoincrement = (row[5] == 'auto_increment')
if type.upper().startswith('ENUM('):
column_definition = type[:5].upper() + type[5:]
else:
column_definition = type.upper()
if is_autoincrement:
column_definition += ' AUTO_INCREMENT'
if is_notnull:
column_definition += ' NOT NULL'
if default_value is not None:
column_definition += ' DEFAULT '
column_definition += self._conn.escape(str(default_value))
table_columns[name] = {
'column_definition': column_definition,
'is_in_primary_key': is_in_primary_key,
'prev_name': prev_name
}
prev_name = name
except pymysql.err.ProgrammingError as e:
if e.args[0] == pymysql.constants.ER.NO_SUCH_TABLE:
table_columns = {}
if table_columns is None:
raise pymysql.err.DatabaseError('Cannot get schema for table ' + table_name)
return table_columns
#-----------------------------------------------------------------------------------------------------------
def _get_table_indexes(self, table_name):
table_indexes = None
with self._conn.cursor() as cursor:
sql = "SHOW INDEXES FROM {0}".format(table_name)
try:
cursor.execute(sql)
result = cursor.fetchall()
table_indexes = {}
for row in result:
non_unique = row[1]
index_name = row[2]
seq_in_index = row[3]
column_name = '`{0}`'.format(row[4])
type = None
if index_name == 'PRIMARY':
type = 'PRIMARY'
elif non_unique == 0:
type = 'UNIQUE'
if index_name not in table_indexes:
table_indexes[index_name] = { 'columns': [] }
table_indexes[index_name]['type'] = type
while len(table_indexes[index_name]['columns']) < seq_in_index:
table_indexes[index_name]['columns'].append('')
table_indexes[index_name]['columns'][seq_in_index - 1] = column_name
except pymysql.err.ProgrammingError as e:
if e.args[0] == pymysql.constants.ER.NO_SUCH_TABLE:
table_indexes = {}
if table_indexes is None:
raise pymysql.err.DatabaseError('Cannot get indexes for table ' + table_name)
return table_indexes
#-----------------------------------------------------------------------------------------------------------
def _column_definition_matches(self, sql_field_definition, table_column_definition, is_in_primary_key):
current_column_definition = table_column_definition
if sql_field_definition == current_column_definition:
return True
# MySQL implicitly adds NOT NULL/DEFAULT to columns in PRIMARY KEY.
if is_in_primary_key:
current_column_definition = current_column_definition.replace(' NOT NULL', '')
if sql_field_definition == current_column_definition:
return True
idx = current_column_definition.find(' DEFAULT')
if idx > 0:
current_column_definition = current_column_definition[:idx]
if sql_field_definition == current_column_definition:
return True
return False
def _update_table_columns(self, table_name, sql_fields):
table_columns = self._get_table_columns(table_name)
with self._conn.cursor() as cursor:
if len(table_columns) == 0:
sql = "CREATE TABLE {0} ({1})".format(
table_name,
','.join('`{0}` {1}'.format(f['name'], f['column_definition']) for f in sql_fields))
sql = sql.replace('AUTO_INCREMENT', '')
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
else:
for name, metadata in table_columns.items():
if name not in [field['name'] for field in sql_fields]:
sql = 'ALTER TABLE {0} DROP COLUMN `{1}`'.format(
table_name, name)
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
prev_field_name = None
for field in sql_fields:
if field['name'] not in table_columns:
sql = 'ALTER TABLE {0} ADD COLUMN `{1}` {2} {3}'.format(
table_name, field['name'], field['column_definition'],
'FIRST' if prev_field_name is None else 'AFTER `{0}`'.format(prev_field_name))
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
else:
table_column = table_columns[field['name']]
if not self._column_definition_matches(field['column_definition'],
table_column['column_definition'],
table_column['is_in_primary_key']) or \
prev_field_name != table_column['prev_name']:
sql = 'ALTER TABLE {0} CHANGE COLUMN `{1}` `{2}` {3} {4}'.format(
table_name, field['name'], field['name'], field['column_definition'],
'FIRST' if prev_field_name is None else 'AFTER `{0}`'.format(prev_field_name))
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
prev_field_name = field['name']
return True
#-----------------------------------------------------------------------------------------------------------
def _update_table_indexes(self, table_name, sql_indexes):
table_indexes = self._get_table_indexes(table_name)
with self._conn.cursor() as cursor:
drop_primary_key_pending = False
for index_name, index_schema in table_indexes.items():
if index_schema not in sql_indexes:
if index_schema['type'] == 'PRIMARY':
drop_primary_key_pending = True
continue
sql = 'ALTER TABLE {0} DROP INDEX {1}'.format(
table_name, index_name)
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
for index in sql_indexes:
index_exists = False
for _, index_schema in table_indexes.items():
if index_schema == index:
index_exists = True
break
if not index_exists:
if index['type'] == 'PRIMARY':
sql = 'ALTER TABLE {0} {1} ADD PRIMARY KEY ({2})'.format(
table_name,
' DROP PRIMARY KEY,' if drop_primary_key_pending else '',
','.join(index['columns']))
drop_primary_key_pending = False
elif index['type'] == 'UNIQUE':
sql = 'ALTER TABLE {0} ADD UNIQUE INDEX ({1})'.format(
table_name, ','.join(index['columns']))
else:
sql = 'ALTER TABLE {0} ADD INDEX ({1})'.format(
table_name, ','.join(index['columns']))
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
if drop_primary_key_pending:
sql = 'ALTER TABLE {0} DROP PRIMARY KEY'.format(
table_name)
try:
cursor.execute(sql)
except pymysql.err.ProgrammingError as e:
raise
return True
#-----------------------------------------------------------------------------------------------------------
def UpdateTableSchema(self, table_name, schema):
field_pattern = r'^\s*(\w+)\s+(\w+)(\(([^)]+)\))?(\s+UNSIGNED)?(\s+AUTO_INCREMENT)?(\s+NOTNULL)?(\s+DEFAULT\s+\'?(\w*)\'?)?\s*$'
index_pattern = r'^\s*INDEX\s+((\w+)\s+)?\(([^\)]+)\)\s*$'
column_types = {
'I': 'INT(11)',
'I1': 'TINYINT(4)',
'I2': 'SMALLINT(6)',
'I8': 'BIGINT(20)',
'F': 'DOUBLE',
'N': 'DECIMAL(10,2)',
'C': 'VARCHAR',
'MX': 'MEDIUMTEXT',
'X': 'LONGTEXT',
'MB': 'MEDIUMBLOB',
'B': 'LONGBLOB',
'J': 'JSON',
'BIN': 'BINARY',
'D': 'DATE',
'T': 'DATETIME',
'ENUM': 'ENUM',
'INT8': 'TINYINT(4)',
'INT16': 'SMALLINT(6)',
'INT32': 'INT(11)',
'INT64': 'BIGINT(20)',
'CHAR': 'VARCHAR',
'DOUBLE': 'DOUBLE',
'TEXT': 'LONGTEXT',
'BLOB': 'LONGBLOB',
'JSON': 'JSON',
'BOOL': 'TINYINT(1)',
'BOOLEAN': 'TINYINT(1)',
}
fields = [x.strip() if not x.strip().endswith(',') else x.strip()[:-1].strip() for x in schema.splitlines()]
sql_fields = []
sql_indexes = []
for field in fields:
if len(field) == 0:
continue
matches = re.match(field_pattern, field)
if not matches:
matches = re.match(index_pattern, field)
if not matches:
raise ValueError('Invalid field specifier: ' + field)
index_type = matches.group(2)
index_fields = matches.group(3)
if index_type not in (None, 'PRIMARY', 'UNIQUE'):
raise ValueError('Invalid index type: ' + field)
sql_indexes.append({'type': index_type, 'columns': ['`{}`'.format(x.strip()) for x in index_fields.split(',')] })
continue
name = matches.group(1)
type = matches.group(2)
type_arguments = matches.group(4)
is_unsigned = matches.group(5) is not None
is_autoincrement = matches.group(6) is not None
is_notnull = matches.group(7) is not None
default_value = matches.group(9)
if type not in column_types:
raise ValueError('Invalid type specifier: ' + field)
if type in ['C', 'CHAR'] and not type_arguments:
raise ValueError('Char type requires size: ' + field)
if type == 'ENUM' and not type_arguments:
raise ValueError('Enum type requires list of possible values: ' + field)
if type == 'BIN' and not type_arguments:
raise ValueError('Binary type requires size: ' + field)
if type not in ['C', 'CHAR', 'ENUM', 'BIN'] and type_arguments:
raise ValueError('Only char or enum type can have arguments: ' + field)
column_definition = column_types[type]
if type in ['C', 'CHAR', 'ENUM', 'BIN']:
if type == 'ENUM':
# Strip whitespace between enum values for canonical form.
type_arguments = ','.join("'{}'".format(x) for x in re.findall(r"'([^']*?)'", type_arguments))
column_definition += '({0})'.format(type_arguments)
if is_unsigned:
column_definition += ' UNSIGNED'
if is_autoincrement:
column_definition += ' AUTO_INCREMENT'
if is_notnull:
column_definition += ' NOT NULL'
if default_value is not None:
column_definition += ' DEFAULT '
column_definition += self._conn.escape(str(default_value))
sql_fields.append({'name': name, 'column_definition': column_definition})
if not self._update_table_columns(table_name, sql_fields):
return False
if not self._update_table_indexes(table_name, sql_indexes):
return False
# AUTO_INCREMENT can be set only after primary index is created.
if any('AUTO_INCREMENT' in f['column_definition'] for f in sql_fields):
if not self._update_table_columns(table_name, sql_fields):
return False
return True