浏览代码

Add support for multivalue field deletion (unstested)

Yann Weber 8 年前
父节点
当前提交
aaaf0af558
共有 1 个文件被更改,包括 26 次插入0 次删除
  1. 26
    0
      DataSource/MySQL/migrationhandler.py

+ 26
- 0
DataSource/MySQL/migrationhandler.py 查看文件

@@ -220,6 +220,9 @@ class MysqlMigrationHandler(DummyMigrationHandler):
220 220
         if not isinstance(emfield, EditorialModel.fields.EmField):
221 221
             raise ValueError("The given uid is not an EmField uid")
222 222
 
223
+        if isinstance(emfield.fieldtype_instance(), MultiValueFieldType):
224
+            return self._del_column_multivalue(emfield)
225
+
223 226
         emclass = emfield.em_class
224 227
         tname = utils.object_table_name(emclass.name)
225 228
         # Delete the table triggers to prevent errors
@@ -455,6 +458,22 @@ ADD COLUMN {col_name} {col_type} {col_specs};"""
455 458
         # adding the column
456 459
         self._add_column(table_name, col_name, col_ftype)
457 460
 
461
+    ## @brief Delete a multivalue column
462
+    # @param emfield EmField : EmField instance
463
+    # @note untested
464
+    def _del_column_multivalue(self, emfield):
465
+        ftype = emfield.fieldtype_instance()
466
+        if not isinstance(ftype, MultiValueFieldType):
467
+            raise ValueError("Except an emfield with multivalue fieldtype")
468
+        tname = utils.object_table_name(emfield.em_class.name)
469
+        tname = utils.multivalue_table_name(tname, ftype.keyname)
470
+        self._del_column(tname, emfield.name)
471
+        if len([ f for f in emfield.em_class.fields()]) == 0:
472
+            try:
473
+                self._query("DROP TABLE %s;" % utils.escape_idname(tname))
474
+            except self._dbmodule.err.InternalError as expt:
475
+                print(expt)
476
+
458 477
 
459 478
     ## @brief Add a foreign key
460 479
     # @param src_table_name str : The name of the table where we will add the FK
@@ -549,6 +568,13 @@ FOR EACH ROW SET {col_val_list};""".format(
549 568
     ## @brief Delete all table created by the MH
550 569
     # @param model Model : the Editorial model
551 570
     def __purge_db(self, model):
571
+        for uid in [
572
+                    field
573
+                    for field in model.components('EmField')
574
+                    if isinstance(field.fieldtype_instance(), MultiValueFieldType)
575
+        ]:
576
+            self._del_column_multivalue(field)
577
+
552 578
         for uid in [c.uid for c in model.components('EmClass')]:
553 579
             try:
554 580
                 self.delete_emclass_table(model, uid)

正在加载...
取消
保存