Browse Source

Bugfixes in sqlsetup

Put every uid columns and associated FK in INTEGER instead of VARCHAR(50)
Yann Weber 9 years ago
parent
commit
c901e7a643
2 changed files with 31 additions and 26 deletions
  1. 16
    16
      Database/sqlsetup.py
  2. 15
    10
      Database/sqlwrapper.py

+ 16
- 16
Database/sqlsetup.py View File

@@ -5,7 +5,7 @@ from Database.sqlwrapper import SqlWrapper
5 5
 class SQLSetup(object): 
6 6
 
7 7
     def initDb(self, dbconfname = 'default'):
8
-        db = SqlWrapper(read_db = dbconfname, write_db = dbconfname)
8
+        db = SqlWrapper(read_db = dbconfname, write_db = dbconfname, alchemy_logs=True)
9 9
         tables = self.get_schema()
10 10
         db.dropAll()
11 11
         db.createAllFromConf(tables)
@@ -14,7 +14,7 @@ class SQLSetup(object):
14 14
         tables = []
15 15
 
16 16
         default_columns = [
17
-            {"name":"uid",          "type":"VARCHAR(50)", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
17
+            {"name":"uid",          "type":"INTEGER", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
18 18
             {"name":"name",         "type":"VARCHAR(50)", "extra":{"nullable":False, "unique":True}},
19 19
             {"name":"string",       "type":"TEXT"},
20 20
             {"name":"help",         "type":"TEXT"},
@@ -47,7 +47,7 @@ class SQLSetup(object):
47 47
         # Table listing the types
48 48
         em_type = {"name":"em_type"}
49 49
         em_type['columns'] = default_columns + [
50
-            {"name":"class_id",     "type":"VARCHAR(50)", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
50
+            {"name":"class_id",     "type":"INTEGER", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
51 51
             {"name":"sortcolumn",   "type":"VARCHAR(50)", "extra":{"default":"rank"}},
52 52
             {"name":"icon",         "type":"INTEGER"},
53 53
         ]
@@ -56,8 +56,8 @@ class SQLSetup(object):
56 56
         # relation between types: which type can be a child of another
57 57
         em_type_hierarchy = {"name":"em_type_hierarchy"}
58 58
         em_type_hierarchy['columns'] = [
59
-            {"name":"superior_id",    "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
60
-            {"name":"subordinate_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
59
+            {"name":"superior_id",    "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
60
+            {"name":"subordinate_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
61 61
             {"name":"nature",         "type":"VARCHAR(50)"},
62 62
         ]
63 63
         tables.append(em_type_hierarchy)
@@ -65,17 +65,17 @@ class SQLSetup(object):
65 65
        # Table listing the fieldgroups of a class
66 66
         em_fieldgroup = {"name":"em_fieldgroup"}
67 67
         em_fieldgroup['columns'] = default_columns + [
68
-            {"name":"class_id",     "type":"VARCHAR(50)", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
68
+            {"name":"class_id",     "type":"INTEGER", "extra":{"foreignkey":"em_class.uid", "nullable":False}},
69 69
         ]
70 70
         tables.append(em_fieldgroup)
71 71
 
72 72
         # Table listing the fields of a fieldgroup
73 73
         em_field = {"name":"em_field"}
74 74
         em_field['columns'] = default_columns + [
75
-            {"name":"fieldtype_id",   "type":"VARCHAR(50)", "extra":{"nullable":False}},
76
-            {"name":"fieldgroup_id",  "type":"VARCHAR(50)", "extra":{"foreignkey":"em_fieldgroup.uid", "nullable":False}},
77
-            {"name":"rel_to_type_id", "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: type this field refer to
78
-            {"name":"rel_field_id",   "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: field that specify the rel_to_type_id
75
+            {"name":"fieldtype_id",   "type":"INTEGER", "extra":{"nullable":False}},
76
+            {"name":"fieldgroup_id",  "type":"INTEGER", "extra":{"foreignkey":"em_fieldgroup.uid", "nullable":False}},
77
+            {"name":"rel_to_type_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: type this field refer to
78
+            {"name":"rel_field_id",   "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False}}, # if relational: field that specify the rel_to_type_id
79 79
             {"name":"optional",       "type":"BOOLEAN"},
80 80
             {"name":"internal",       "type":"BOOLEAN"},
81 81
             {"name":"icon",           "type":"INTEGER"},
@@ -85,8 +85,8 @@ class SQLSetup(object):
85 85
         # selected field for each type
86 86
         em_field_type = {"name":"em_field_type"}
87 87
         em_field_type['columns'] = [
88
-            {"name":"type_id",   "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
89
-            {"name":"field_id",  "type":"VARCHAR(50)", "extra":{"foreignkey":"em_field.uid", "nullable":False, "primarykey":True}},
88
+            {"name":"type_id",   "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":False, "primarykey":True}},
89
+            {"name":"field_id",  "type":"INTEGER", "extra":{"foreignkey":"em_field.uid", "nullable":False, "primarykey":True}},
90 90
         ]
91 91
         tables.append(em_field_type)
92 92
 
@@ -94,10 +94,10 @@ class SQLSetup(object):
94 94
         objects = {
95 95
             "name":"objects",
96 96
             "columns":[
97
-                {"name":"uid",         "type":"VARCHAR(50)", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
97
+                {"name":"uid",         "type":"INTEGER", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
98 98
                 {"name":"string",      "type":"VARCHAR(50)"},
99
-                {"name":"class_id",    "type":"VARCHAR(50)", "extra":{"foreignkey":"em_class.uid"}},
100
-                {"name":"type_id",     "type":"VARCHAR(50)", "extra":{"foreignkey":"em_type.uid"}},
99
+                {"name":"class_id",    "type":"INTEGER", "extra":{"foreignkey":"em_class.uid"}},
100
+                {"name":"type_id",     "type":"INTEGER", "extra":{"foreignkey":"em_type.uid"}},
101 101
                 {"name":"date_update", "type":"DATE"},
102 102
                 {"name":"date_create", "type":"DATE"},
103 103
                 {"name":"history",     "type":"TEXT"}
@@ -110,7 +110,7 @@ class SQLSetup(object):
110 110
         files = {
111 111
             "name":"files",
112 112
             "columns":[
113
-                {"name":"uid",     "type":"VARCHAR(50)", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
113
+                {"name":"uid",     "type":"INTEGER", "extra":{"foreignkey":"uids.uid", "nullable":False, "primarykey":True}},
114 114
                 {"name":"field1",  "type":"VARCHAR(50)"}
115 115
             ]
116 116
         }

+ 15
- 10
Database/sqlwrapper.py View File

@@ -81,6 +81,10 @@ class SqlWrapper(object):
81 81
         logger.debug("New wrapper instance : <"+self.name+" read:"+str(self.r_engine)+" write:"+str(self.w_engine))
82 82
         pass
83 83
 
84
+    @classmethod
85
+    def getEngine(c, ename = 'default', logs = None):
86
+        return c(read_db = ename, write_db = ename, alchemy_logs = logs).r_engine
87
+
84 88
     @property
85 89
     def cfg(self):
86 90
         """ Return the SqlWrapper.config dict """
@@ -273,13 +277,10 @@ class SqlWrapper(object):
273 277
                     err.append('Missing "'+dbname+'" db configuration')
274 278
                 else:
275 279
                     db = self.cfg['db'][dbname]
276
-                    if 'ENGINE' not in db:
277
-                        err.append('Missing "ENGINE" in database "'+db+'"')
278
-                    else:
279
-                        if db['ENGINE'] not in self._engines_cfg:
280
-                            err.append('Unknown engine "'+db['ENGINE']+'"')
281
-                        elif db['ENGINE'] != 'sqlite' and 'USER' not in db:
282
-                            err.append('Missing "User" in configuration of database "'+dbname+'"')
280
+                    if db['ENGINE'] not in self._engines_cfg:
281
+                        err.append('Unknown engine "'+db['ENGINE']+'"')
282
+                    elif db['ENGINE'] != 'sqlite' and 'USER' not in db:
283
+                        err.append('Missing "User" in configuration of database "'+dbname+'"')
283 284
                     if 'NAME' not in db:
284 285
                         err.append('Missing "NAME" in database "'+dbname+'"')
285 286
                         
@@ -294,7 +295,8 @@ class SqlWrapper(object):
294 295
         if not settings.DEBUG:
295 296
             logger.critical("Trying to drop all tables but we are not in DEBUG !!!")
296 297
             raise RuntimeError("Trying to drop all tables but we are not in DEBUG !!!")
297
-        meta = sqla.MetaData(bind=self.w_engine, reflect = True)
298
+        meta = sqla.MetaData(bind=self.w_engine)
299
+        meta.reflect()
298 300
         meta.drop_all()
299 301
         pass
300 302
 
@@ -310,7 +312,7 @@ class SqlWrapper(object):
310 312
             if not isinstance(table, dict):
311 313
                 raise TypeError("Excepted a list of dict but got a "+str(type(schema))+" in the list")
312 314
             self.createTable(**table)
313
-
315
+        
314 316
         self.meta_crea.create_all(bind = self.w_engine)
315 317
         logger.info("All tables created")
316 318
         self.meta_crea = None
@@ -334,14 +336,17 @@ class SqlWrapper(object):
334 336
         if not isinstance(name, str):
335 337
             raise TypeError("<class str> excepted for table name, but got "+type(name))
336 338
 
339
+        #if not 'mysql_engine' in kw and self.w_engine.dialect.name == 'mysql':
340
+        #    kw['mysql_engine'] = 'InnoDB'
341
+
337 342
         res = sqla.Table(name, self.meta_crea, **kw)
338 343
         for i,col in enumerate(columns):
339 344
             res.append_column(self.createColumn(**col))
340 345
 
341 346
         if crea_now:
342 347
             self.meta_crea.create_all(self.w_engine)
348
+            logger.debug("Table '"+name+"' created")
343 349
 
344
-        #logger.debug("Table '"+name+"' created")
345 350
         pass
346 351
 
347 352
     def createColumn(self, **kwargs):

Loading…
Cancel
Save