|
@@ -197,7 +197,7 @@ class MysqlMigrationHandler(DummyMigrationHandler):
|
197
|
197
|
emclass = em.component(uid)
|
198
|
198
|
if not isinstance(emclass, EditorialModel.classes.EmClass):
|
199
|
199
|
raise ValueError("The given uid is not an EmClass uid")
|
200
|
|
- pkname, pktype = self._common_field_pk
|
|
200
|
+ pkname, pktype = self._object_pk
|
201
|
201
|
table_name = self._emclass2table_name(emclass)
|
202
|
202
|
self._create_table(table_name, pkname, pktype, engine=engine)
|
203
|
203
|
|
|
@@ -281,7 +281,7 @@ class MysqlMigrationHandler(DummyMigrationHandler):
|
281
|
281
|
if_exists = 'drop' if drop_if_exist else 'nothing'
|
282
|
282
|
#Object table
|
283
|
283
|
tname = utils.common_tables['object']
|
284
|
|
- pk_name, pk_ftype = self._common_field_pk
|
|
284
|
+ pk_name, pk_ftype = self._object_pk
|
285
|
285
|
self._create_table(tname, pk_name, pk_ftype, engine=self.db_engine, if_exists=if_exists)
|
286
|
286
|
#Adding columns
|
287
|
287
|
cols = {fname: self._common_field_to_ftype(fname) for fname in EditorialModel.classtypes.common_fields}
|
|
@@ -290,6 +290,7 @@ class MysqlMigrationHandler(DummyMigrationHandler):
|
290
|
290
|
self._add_column(tname, fname, ftype)
|
291
|
291
|
#Creating triggers
|
292
|
292
|
self._generate_triggers(tname, cols)
|
|
293
|
+ object_tname = tname
|
293
|
294
|
|
294
|
295
|
#Relation table
|
295
|
296
|
tname = utils.common_tables['relation']
|
|
@@ -301,6 +302,25 @@ class MysqlMigrationHandler(DummyMigrationHandler):
|
301
|
302
|
#Creating triggers
|
302
|
303
|
self._generate_triggers(tname, self._relation_cols)
|
303
|
304
|
|
|
305
|
+ # Creating foreign keys between relation and object table
|
|
306
|
+ sup_cname, sub_cname = self.get_sup_and_sub_cols()
|
|
307
|
+ self._add_fk(tname, object_tname, sup_cname, self._object_pk[0], 'fk_relations_superiors')
|
|
308
|
+ self._add_fk(tname, object_tname, sub_cname, self._object_pk[0], 'fk_relations_subordinate')
|
|
309
|
+
|
|
310
|
+ ## @brief Returns the fieldname for superior and subordinate in relation table
|
|
311
|
+ # @return a tuple (superior_name, subordinate_name)
|
|
312
|
+ @classmethod
|
|
313
|
+ def get_sup_and_sub_cols(cls):
|
|
314
|
+ sup = None
|
|
315
|
+ sub = None
|
|
316
|
+ for fname, finfo in EditorialModel.classtypes.relations_common_fields.items():
|
|
317
|
+ if finfo['fieldtype'] == 'leo':
|
|
318
|
+ if finfo['superior']:
|
|
319
|
+ sup = fname
|
|
320
|
+ else:
|
|
321
|
+ sub = fname
|
|
322
|
+ return utils.column_name(sup),utils.column_name(sub)
|
|
323
|
+
|
304
|
324
|
## @return true if the name changes
|
305
|
325
|
def _name_change(self, initial_state, new_state):
|
306
|
326
|
return 'name' in initial_state and initial_state['name'] != new_state['name']
|
|
@@ -377,13 +397,14 @@ ADD COLUMN {col_name} {col_type} {col_specs};"""
|
377
|
397
|
# @param dst_table_name str : The name of the table the FK will point on
|
378
|
398
|
# @param src_col_name str : The name of the concerned column in the src_table
|
379
|
399
|
# @param dst_col_name str : The name of the concerned column in the dst_table
|
380
|
|
- def _add_fk(self, src_table_name, dst_table_name, src_col_name, dst_col_name):
|
|
400
|
+ def _add_fk(self, src_table_name, dst_table_name, src_col_name, dst_col_name, fk_name = None):
|
381
|
401
|
stname = utils.escape_idname(src_table_name)
|
382
|
402
|
dtname = utils.escape_idname(dst_table_name)
|
383
|
403
|
scname = utils.escape_idname(src_col_name)
|
384
|
404
|
dcname = utils.escape_idname(dst_col_name)
|
385
|
405
|
|
386
|
|
- fk_name = utils.get_fk_name(src_table_name, dst_table_name)
|
|
406
|
+ if fk_name is None:
|
|
407
|
+ fk_name = utils.get_fk_name(src_table_name, dst_table_name)
|
387
|
408
|
|
388
|
409
|
self._del_fk(src_table_name, dst_table_name)
|
389
|
410
|
|
|
@@ -530,7 +551,7 @@ FOR EACH ROW SET {col_val_list};""".format(
|
530
|
551
|
|
531
|
552
|
## @brief Returns a tuple (pkname, pk_ftype)
|
532
|
553
|
@property
|
533
|
|
- def _common_field_pk(self):
|
|
554
|
+ def _object_pk(self):
|
534
|
555
|
return self.extract_pk(EditorialModel.classtypes.common_fields)
|
535
|
556
|
|
536
|
557
|
## @brief Returns a tuple (rel_pkname, rel_ftype)
|