Browse Source

Add support for multiple PK

Yann Weber 8 years ago
parent
commit
6153ddd323
1 changed files with 31 additions and 26 deletions
  1. 31
    26
      DataSource/MySQL/migrationhandler.py

+ 31
- 26
DataSource/MySQL/migrationhandler.py View File

@@ -292,41 +292,46 @@ class MysqlMigrationHandler(DummyMigrationHandler):
292 292
 
293 293
     ## @brief Create a table with primary key
294 294
     # @param table_name str : table name
295
-    # @param pk_name str : pk column name
296
-    # @param pk_specs str : see @ref _field_to_sql()
295
+    # @param pk_name str | tuple : pk column name (give tuple for multi pk)
296
+    # @param pk_ftype fieldtype | tuple : pk fieldtype (give a tuple for multi pk)
297 297
     # @param engine str : The engine to use with this table
298 298
     # @param charset str : The charset of this table
299 299
     # @param if_exist str : takes values in ['nothing', 'drop']
300 300
     def _create_table(self, table_name, pk_name, pk_ftype, engine, charset='utf8', if_exists='nothing'):
301 301
         #Escaped table name
302 302
         etname = utils.escape_idname(table_name)
303
-        instr_type, pk_type, pk_specs = fieldtypes_utils.fieldtype_db_init(pk_ftype)
304
-        if instr_type != 'column':
305
-            raise ValueError("Migration handler doesn't support MultiValueFieldType as primary keys")
303
+        if not isinstance(pk_name, tuple):
304
+            pk_name = tuple([pk_name])
305
+            pk_ftype = tuple([pk_ftype])
306
+
307
+        if len(pk_name) != len(pk_ftype):
308
+            raise ValueError("You have to give as many pk_name as pk_ftype")
309
+        
310
+        pk_instr_cols = ''
311
+        pk_format = '{pk_name} {pk_type} {pk_specs},'
312
+        for i in range(len(pk_name)):
313
+            instr_type, pk_type, pk_specs = fieldtypes_utils.fieldtype_db_init(pk_ftype[i])
314
+            if instr_type != 'column':
315
+                raise ValueError("Migration handler doesn't support MultiValueFieldType as primary keys")
316
+            pk_instr_cols += pk_format.format(
317
+                                                pk_name = pk_name[i],
318
+                                                pk_type = pk_type,
319
+                                                pk_specs = pk_specs
320
+                                            )
321
+        pk_instr_cols += "\nPRIMARY KEY("+(','.join([utils.escape_idname(pkn) for pkn in pk_name]))+')\n'
306 322
 
307 323
         if if_exists == 'drop':
308 324
             self._query("""DROP TABLE IF EXISTS {table_name};""".format(table_name=etname))
309
-            qres = """
310
-CREATE TABLE {table_name} (
311
-{pk_name} {pk_type} {pk_specs},
312
-PRIMARY KEY({pk_name})
313
-) ENGINE={engine} DEFAULT CHARSET={charset};"""
314
-        elif if_exists == 'nothing':
315
-            qres = """CREATE TABLE IF NOT EXISTS {table_name} (
316
-{pk_name} {pk_type} {pk_specs},
317
-PRIMARY KEY({pk_name})
318
-) ENGINE={engine} DEFAULT CHARSET={charset};"""
319
-        else:
320
-            raise ValueError("Unexpected value for argument if_exists '%s'." % if_exists)
321
-
322
-        self._query(qres.format(
323
-            table_name=utils.escape_idname(table_name),
324
-            pk_name=utils.escape_idname(pk_name),
325
-            pk_type=pk_type,
326
-            pk_specs=pk_specs,
327
-            engine=engine,
328
-            charset=charset
329
-        ))
325
+
326
+        qres = """CREATE TABLE IF NOT EXISTS {table_name} (
327
+{pk_cols}
328
+) ENGINE={engine} DEFAULT CHARSET={charset};""".format(
329
+                                                        table_name = table_name,
330
+                                                        pk_cols = pk_instr_cols,
331
+                                                        engine = engine,
332
+                                                        charset = charset
333
+        )
334
+        self._query(qres)
330 335
 
331 336
     ## @brief Add a column to a table
332 337
     # @param table_name str : The table name

Loading…
Cancel
Save