Browse Source

Database initialization function written

Yann Weber 8 years ago
parent
commit
13a2d157e3
3 changed files with 33 additions and 14 deletions
  1. 28
    9
      install/lodel_admin.py
  2. 2
    2
      plugins/dummy_datasource/main.py
  3. 3
    3
      plugins/mongodb_datasource/main.py

+ 28
- 9
install/lodel_admin.py View File

48
 
48
 
49
 def init_all_dbs():
49
 def init_all_dbs():
50
     import loader
50
     import loader
51
+    loader.start()
51
     import leapi_dyncode as dyncode
52
     import leapi_dyncode as dyncode
52
     from lodel.settings.utils import SettingsError
53
     from lodel.settings.utils import SettingsError
53
     from lodel.leapi.leobject import LeObject
54
     from lodel.leapi.leobject import LeObject
54
     from lodel.plugin import Plugin
55
     from lodel.plugin import Plugin
56
+    from lodel.plugin.hooks import LodelHook
57
+    from lodel import logger
58
+
59
+    LodelHook.call_hook('datasources_migration_init', __name__, None)
60
+
55
     ds_cls = dict() # EmClass indexed by rw_datasource
61
     ds_cls = dict() # EmClass indexed by rw_datasource
56
     for cls in dyncode.dynclasses:
62
     for cls in dyncode.dynclasses:
57
         ds = cls._datasource_name
63
         ds = cls._datasource_name
58
         if ds not in ds_cls:
64
         if ds not in ds_cls:
59
             ds_cls[ds] = [cls]
65
             ds_cls[ds] = [cls]
60
         else:
66
         else:
61
-            ds_cls.append(cls)
67
+            ds_cls[ds].append(cls)
62
     
68
     
63
     for ds_name in ds_cls:  
69
     for ds_name in ds_cls:  
64
         # Fetching datasource plugin name and datasource connection 
70
         # Fetching datasource plugin name and datasource connection 
65
         # identifier
71
         # identifier
66
         try:
72
         try:
67
-            plugin_name, ds_indentifier = LeObject._get_ds_pugin_name(
73
+            plugin_name, ds_identifier = LeObject._get_ds_plugin_name(
68
                 ds_name, False)
74
                 ds_name, False)
69
         except (NameError, ValueError, RuntimeError):
75
         except (NameError, ValueError, RuntimeError):
70
             raise SettingsError("Datasource configuration error")
76
             raise SettingsError("Datasource configuration error")
72
         con_conf=LeObject._get_ds_connection_conf(ds_identifier, plugin_name)
78
         con_conf=LeObject._get_ds_connection_conf(ds_identifier, plugin_name)
73
         # Fetching migration handler class from plugin
79
         # Fetching migration handler class from plugin
74
         plugin_module = Plugin.get(plugin_name).loader_module()
80
         plugin_module = Plugin.get(plugin_name).loader_module()
75
-        mh_cls = plugin_module.migration_handler
81
+        try:
82
+            mh_cls = plugin_module.migration_handler_class()
83
+        except NameError as e:
84
+            raise RuntimeError("Malformed plugin '%s'. Missing \
85
+migration_handler_class() function in loader file" % ds_name)
76
         #Instanciate the migrationhandler and start db initialisation
86
         #Instanciate the migrationhandler and start db initialisation
77
-        mh = mh_cls(**con_conf)
78
-        mh.init_db(ds_cls[ds_name])
79
-        pass
87
+        try:
88
+            mh = mh_cls(**con_conf)
89
+        except Exception as e:
90
+            msg = "Migration failed for datasource %s(%s.%s) at migration \
91
+handler instanciation : %s"
92
+            msg %= (ds_name, plugin_name, ds_identifier, e)
93
+            raise RuntimeError(msg)
94
+        try:
95
+            mh.init_db(ds_cls[ds_name])
96
+        except Exception as e:
97
+            msg = "Migration failed for datasource %s(%s.%s) when running \
98
+init_db method: %s"
99
+            msg %= (ds_name, plugin_name, ds_identifier, e)
100
+        logger.info("Database initialisation done for %s(%s.%s)" % (
101
+            ds_name, plugin_name, ds_identifier))
80
         
102
         
81
-    #TODO : iter on datasource, fetch ds module and ds option
82
-    # then instanciate corresponfing MH with given options
83
-    pass

+ 2
- 2
plugins/dummy_datasource/main.py View File

3
 from lodel.plugin import LodelHook
3
 from lodel.plugin import LodelHook
4
 from .datasource import DummyDatasource as Datasource
4
 from .datasource import DummyDatasource as Datasource
5
 
5
 
6
-@LodelHook('datasources_migration_init')
7
-def dummy_migration_handler_init():
6
+def migration_handler_class():
8
     from .migration_handler import DummyMigrationHandler as migration_handler
7
     from .migration_handler import DummyMigrationHandler as migration_handler
8
+    return migration_handler
9
 
9
 

+ 3
- 3
plugins/mongodb_datasource/main.py View File

2
 
2
 
3
 from .datasource import MongoDbDatasource as Datasource
3
 from .datasource import MongoDbDatasource as Datasource
4
 
4
 
5
-@LodelHook('datasources_migration_init')
6
-def mongodb_migration_handler_init():
7
-    import plugins.mongodb_datasource.migration_handler as migration_handler
5
+def migration_handler_class():
6
+    from .migration_handler import MigrationHandler as migration_handler
7
+    return migration_handler
8
 
8
 

Loading…
Cancel
Save