Przeglądaj źródła

Database initialization function written

Yann Weber 8 lat temu
rodzic
commit
13a2d157e3

+ 28
- 9
install/lodel_admin.py Wyświetl plik

@@ -48,23 +48,29 @@ def refresh_dyncode():
48 48
 
49 49
 def init_all_dbs():
50 50
     import loader
51
+    loader.start()
51 52
     import leapi_dyncode as dyncode
52 53
     from lodel.settings.utils import SettingsError
53 54
     from lodel.leapi.leobject import LeObject
54 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 61
     ds_cls = dict() # EmClass indexed by rw_datasource
56 62
     for cls in dyncode.dynclasses:
57 63
         ds = cls._datasource_name
58 64
         if ds not in ds_cls:
59 65
             ds_cls[ds] = [cls]
60 66
         else:
61
-            ds_cls.append(cls)
67
+            ds_cls[ds].append(cls)
62 68
     
63 69
     for ds_name in ds_cls:  
64 70
         # Fetching datasource plugin name and datasource connection 
65 71
         # identifier
66 72
         try:
67
-            plugin_name, ds_indentifier = LeObject._get_ds_pugin_name(
73
+            plugin_name, ds_identifier = LeObject._get_ds_plugin_name(
68 74
                 ds_name, False)
69 75
         except (NameError, ValueError, RuntimeError):
70 76
             raise SettingsError("Datasource configuration error")
@@ -72,12 +78,25 @@ def init_all_dbs():
72 78
         con_conf=LeObject._get_ds_connection_conf(ds_identifier, plugin_name)
73 79
         # Fetching migration handler class from plugin
74 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 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 Wyświetl plik

@@ -3,7 +3,7 @@
3 3
 from lodel.plugin import LodelHook
4 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 7
     from .migration_handler import DummyMigrationHandler as migration_handler
8
+    return migration_handler
9 9
 

+ 3
- 3
plugins/mongodb_datasource/main.py Wyświetl plik

@@ -2,7 +2,7 @@ from lodel.plugin import LodelHook
2 2
 
3 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…
Anuluj
Zapisz