|
@@ -4,6 +4,7 @@ import copy
|
4
|
4
|
import pymysql
|
5
|
5
|
|
6
|
6
|
import EditorialModel
|
|
7
|
+from DataSource.MySQL.MySQL import MySQL
|
7
|
8
|
|
8
|
9
|
# The global MH algorithm is as follow :
|
9
|
10
|
# A create_table(table_name, pk_name, pk_opt) method that create a table
|
|
@@ -34,9 +35,9 @@ import EditorialModel
|
34
|
35
|
class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigrationHandler):
|
35
|
36
|
|
36
|
37
|
## @brief Object table name
|
37
|
|
- _object_tname = 'object'
|
|
38
|
+ #_object_tname = 'object'
|
38
|
39
|
## @brief Relation table name
|
39
|
|
- _relation_tname = 'relation'
|
|
40
|
+ #_relation_tname = 'relation'
|
40
|
41
|
|
41
|
42
|
## @brief Construct a MysqlMigrationHandler
|
42
|
43
|
# @param host str : The db host
|
|
@@ -44,6 +45,7 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
44
|
45
|
# @param password str : The db password
|
45
|
46
|
# @param db str : The db name
|
46
|
47
|
def __init__(self, host, user, password, db, db_engine = 'InnoDB', foreign_keys = True, debug = False, dryrun = False, drop_if_exists = False):
|
|
48
|
+ self.datasource = MySQL
|
47
|
49
|
#Connect to MySQL
|
48
|
50
|
self.db = pymysql.connect(host=host, user=user, passwd=password, db=db)
|
49
|
51
|
self.debug = debug
|
|
@@ -162,7 +164,6 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
162
|
164
|
cols_l = self._class2cols(emclass)
|
163
|
165
|
self._generate_triggers(tname, cols_l)
|
164
|
166
|
|
165
|
|
-
|
166
|
167
|
## @brief Given a class uid create the coressponding table
|
167
|
168
|
def create_emclass_table(self, em, uid, engine):
|
168
|
169
|
emclass = em.component(uid)
|
|
@@ -180,7 +181,7 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
180
|
181
|
emclass = emcomponent(uid)
|
181
|
182
|
if not isinstance(emclass, EditorialModel.classes.EmClass):
|
182
|
183
|
raise ValueError("The give uid is not an EmClass uid")
|
183
|
|
- tname = self._idname_escape(self._emclass2table_name(emclass.name))
|
|
184
|
+ tname = self.datasource.escape_idname(self._emclass2table_name(emclass))
|
184
|
185
|
# Delete the table triggers to prevent errors
|
185
|
186
|
self._generate_triggers(tname, dict())
|
186
|
187
|
|
|
@@ -208,8 +209,8 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
208
|
209
|
# @param tname str : The table name
|
209
|
210
|
# @param fname str : The column name
|
210
|
211
|
def _del_column(self, tname, fname):
|
211
|
|
- tname = self._idname_escape(tname)
|
212
|
|
- fname = self._idname_escape(fname)
|
|
212
|
+ tname = self.datasource.escape_idname(tname)
|
|
213
|
+ fname = self.datasource.escape_idname(fname)
|
213
|
214
|
|
214
|
215
|
self._query("""ALTER TABLE {table_name} DROP COLUMN {col_name};""".format(table_name = tname, col_name = fname))
|
215
|
216
|
|
|
@@ -217,7 +218,8 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
217
|
218
|
# @param emclass EmClass : An EmClass instance
|
218
|
219
|
# @return a table name
|
219
|
220
|
def _emclass2table_name(self, emclass):
|
220
|
|
- return "class_%s"%emclass.name
|
|
221
|
+ return self.datasource.get_table_name_from_class(emclass.name)
|
|
222
|
+ #return "class_%s"%emclass.name
|
221
|
223
|
|
222
|
224
|
## @brief Construct a table name given a rela2type EmField instance
|
223
|
225
|
# @param em Model : A Model instance
|
|
@@ -226,7 +228,8 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
226
|
228
|
def _r2t2table_name(self, em, emfield):
|
227
|
229
|
emclass = emfield.em_class
|
228
|
230
|
emtype = em.component(emfield.rel_to_type_id)
|
229
|
|
- return "%s_%s_%s"%(emclass.name, emtype.name, emfield.name)
|
|
231
|
+ return self.datasource.get_r2t2table_name(emclass.name, emtype.name, emfield.name)
|
|
232
|
+ #return "%s_%s_%s"%(emclass.name, emtype.name, emfield.name)
|
230
|
233
|
|
231
|
234
|
## @brief Generate a columns_fieldtype dict given a rel2type EmField
|
232
|
235
|
# @param em Model : an @ref EditorialModel.model.Model instance
|
|
@@ -282,7 +285,7 @@ class MysqlMigrationHandler(EditorialModel.migrationhandler.dummy.DummyMigration
|
282
|
285
|
# @return None
|
283
|
286
|
def _create_table(self, table_name, pk_name, pk_ftype, engine, charset = 'utf8', if_exists = 'nothing'):
|
284
|
287
|
#Escaped table name
|
285
|
|
- etname = self._idname_escape(table_name)
|
|
288
|
+ etname = self.datasource.escape_idname(table_name)
|
286
|
289
|
pk_type = self._field_to_type(pk_ftype)
|
287
|
290
|
pk_specs = self._field_to_specs(pk_ftype)
|
288
|
291
|
|
|
@@ -302,8 +305,8 @@ PRIMARY KEY({pk_name})
|
302
|
305
|
raise ValueError("Unexpected value for argument if_exists '%s'."%if_exists)
|
303
|
306
|
|
304
|
307
|
self._query(qres.format(
|
305
|
|
- table_name = self._idname_escape(table_name),
|
306
|
|
- pk_name = self._idname_escape(pk_name),
|
|
308
|
+ table_name = self.datasource.escape_idname(table_name),
|
|
309
|
+ pk_name = self.datasource.escape_idname(pk_name),
|
307
|
310
|
pk_type = pk_type,
|
308
|
311
|
pk_specs = pk_specs,
|
309
|
312
|
engine = engine,
|
|
@@ -319,8 +322,8 @@ PRIMARY KEY({pk_name})
|
319
|
322
|
add_col = """ALTER TABLE {table_name}
|
320
|
323
|
ADD COLUMN {col_name} {col_type} {col_specs};"""
|
321
|
324
|
|
322
|
|
- etname = self._idname_escape(table_name)
|
323
|
|
- ecname = self._idname_escape(col_name)
|
|
325
|
+ etname = self.datasource.escape_idname(table_name)
|
|
326
|
+ ecname = self.datasource.escape_idname(col_name)
|
324
|
327
|
|
325
|
328
|
add_col = add_col.format(
|
326
|
329
|
table_name = etname,
|
|
@@ -344,19 +347,19 @@ ADD COLUMN {col_name} {col_type} {col_specs};"""
|
344
|
347
|
# @param src_col_name str : The name of the concerned column in the src_table
|
345
|
348
|
# @param dst_col_name str : The name of the concerned column in the dst_table
|
346
|
349
|
def _add_fk(self, src_table_name, dst_table_name, src_col_name, dst_col_name):
|
347
|
|
- stname = self._idname_escape(src_table_name)
|
348
|
|
- dtname = self._idname_escape(dst_table_name)
|
349
|
|
- scname = self._idname_escape(src_col_name)
|
350
|
|
- dcname = self._idname_escape(dst_col_name)
|
|
350
|
+ stname = self.datasource.escape_idname(src_table_name)
|
|
351
|
+ dtname = self.datasource.escape_idname(dst_table_name)
|
|
352
|
+ scname = self.datasource.escape_idname(src_col_name)
|
|
353
|
+ dcname = self.datasource.escape_idname(dst_col_name)
|
351
|
354
|
|
352
|
|
- fk_name = self._fk_name(src_table_name, dst_table_name)
|
|
355
|
+ fk_name = self.datasource.get_fk_name(src_table_name, dst_table_name)
|
353
|
356
|
|
354
|
357
|
self._del_fk(src_table_name, dst_table_name)
|
355
|
358
|
|
356
|
359
|
self._query("""ALTER TABLE {src_table}
|
357
|
360
|
ADD CONSTRAINT {fk_name}
|
358
|
361
|
FOREIGN KEY ({src_col}) references {dst_table}({dst_col});""".format(
|
359
|
|
- fk_name = self._idname_escape(fk_name),
|
|
362
|
+ fk_name = self.datasource.escape_idname(fk_name),
|
360
|
363
|
src_table = stname,
|
361
|
364
|
src_col = scname,
|
362
|
365
|
dst_table = dtname,
|
|
@@ -371,13 +374,13 @@ FOREIGN KEY ({src_col}) references {dst_table}({dst_col});""".format(
|
371
|
374
|
try:
|
372
|
375
|
self._query("""ALTER TABLE {src_table}
|
373
|
376
|
DROP FOREIGN KEY {fk_name}""".format(
|
374
|
|
- src_table = self._idname_escape(src_table_name),
|
375
|
|
- fk_name = self._idname_escape(self._fk_name(src_table_name, dst_table_name))
|
|
377
|
+ src_table = self.datasource.escape_idname(src_table_name),
|
|
378
|
+ fk_name = self.datasource.escape_idname(self.datasource.get_fk_name(src_table_name, dst_table_name))
|
376
|
379
|
))
|
377
|
380
|
except pymysql.err.InternalError: pass
|
378
|
381
|
|
379
|
|
- def _fk_name(self, src_table_name, dst_table_name):
|
380
|
|
- return "fk_%s_%s"%(src_table_name, dst_table_name)
|
|
382
|
+ #def _fk_name(self, src_table_name, dst_table_name):
|
|
383
|
+ # return "fk_%s_%s"%(src_table_name, dst_table_name)
|
381
|
384
|
|
382
|
385
|
|
383
|
386
|
## @brief Generate triggers given a table_name and its columns fieldtypes
|
|
@@ -413,18 +416,18 @@ DROP FOREIGN KEY {fk_name}""".format(
|
413
|
416
|
# @param cols_val dict : Dict with column name as key and column value as value
|
414
|
417
|
# @return None
|
415
|
418
|
def _table_trigger(self, table_name, moment, cols_val):
|
416
|
|
- trigger_name = self._idname_escape("%s_%s_trig"%(table_name, moment))
|
|
419
|
+ trigger_name = self.datasource.escape_idname("%s_%s_trig"%(table_name, moment))
|
417
|
420
|
#Try to delete the trigger
|
418
|
421
|
drop_trig = """DROP TRIGGER IF EXISTS {trigger_name};""".format(trigger_name = trigger_name)
|
419
|
422
|
self._query(drop_trig)
|
420
|
423
|
|
421
|
|
- col_val_l = ', '.join([ "NEW.%s = %s"%(self._idname_escape(cname), cval)for cname, cval in cols_val.items() ])
|
|
424
|
+ col_val_l = ', '.join([ "NEW.%s = %s"%(self.datasource.escape_idname(cname), cval)for cname, cval in cols_val.items() ])
|
422
|
425
|
#Create a trigger if needed
|
423
|
426
|
if len(col_val_l) > 0:
|
424
|
427
|
trig_q = """CREATE TRIGGER {trigger_name} BEFORE {moment} ON {table_name}
|
425
|
428
|
FOR EACH ROW SET {col_val_list};""".format(
|
426
|
429
|
trigger_name = trigger_name,
|
427
|
|
- table_name = self._idname_escape(table_name),
|
|
430
|
+ table_name = self.datasource.escape_idname(table_name),
|
428
|
431
|
moment = moment,
|
429
|
432
|
col_val_list = col_val_l
|
430
|
433
|
)
|
|
@@ -432,10 +435,10 @@ FOR EACH ROW SET {col_val_list};""".format(
|
432
|
435
|
|
433
|
436
|
## @brief Identifier escaping
|
434
|
437
|
# @param idname str : An SQL identifier
|
435
|
|
- def _idname_escape(self, idname):
|
436
|
|
- if '`' in idname:
|
437
|
|
- raise ValueError("Invalid name : '%s'"%idname)
|
438
|
|
- return '`%s`'%idname
|
|
438
|
+ #def _idname_escape(self, idname):
|
|
439
|
+ # if '`' in idname:
|
|
440
|
+ # raise ValueError("Invalid name : '%s'"%idname)
|
|
441
|
+ # return '`%s`'%idname
|
439
|
442
|
|
440
|
443
|
## @brief Returns column specs from fieldtype
|
441
|
444
|
# @param emfieldtype EmFieldType : An EmFieldType insance
|