|
@@ -2,10 +2,14 @@
|
2
|
2
|
|
3
|
3
|
from lodel.datasource.generic.migrationhandler import GenericMigrationHandler
|
4
|
4
|
from lodel.datasource.mongodb.datasource import MongoDbDataSource
|
|
5
|
+import lodel.datasource.mongodb.utils as utils
|
5
|
6
|
from lodel.editorial_model.components import EmClass, EmField
|
|
7
|
+
|
|
8
|
+
|
6
|
9
|
class MigrationHandlerChangeError(Exception):
|
7
|
10
|
pass
|
8
|
11
|
|
|
12
|
+
|
9
|
13
|
## @brief Modifies a MongoDb database given editorial model changes
|
10
|
14
|
class MongoDbMigrationHandler(GenericMigrationHandler):
|
11
|
15
|
|
|
@@ -19,6 +23,7 @@ class MongoDbMigrationHandler(GenericMigrationHandler):
|
19
|
23
|
# del conn_args['module']
|
20
|
24
|
|
21
|
25
|
self.db_conn = MongoDbDataSource(self.connection_name)
|
|
26
|
+ self.database = self.db_conn.database
|
22
|
27
|
# TODO Réimplémenter la partie sur les settings
|
23
|
28
|
mh_settings = {}
|
24
|
29
|
self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun']
|
|
@@ -34,8 +39,35 @@ class MongoDbMigrationHandler(GenericMigrationHandler):
|
34
|
39
|
# @param new_state dict|None : dict with field name as key and field value as value. Represents the new state. None means it's a component deletion.
|
35
|
40
|
# @throw MigrationHandlerChangeError if the change was refused
|
36
|
41
|
def register_change(self, em, uid, initial_state, new_state):
|
37
|
|
- pass
|
38
|
|
-
|
|
42
|
+ if isinstance(em.classes(uid), EmClass):
|
|
43
|
+ collection_name = utils.object_collection_name(em.classes(uid).display_name)
|
|
44
|
+ if initial_state is None:
|
|
45
|
+ self._create_collection(collection_name)
|
|
46
|
+ elif new_state is None:
|
|
47
|
+ self._delete_collection(collection_name)
|
39
|
48
|
|
40
|
49
|
def _create_default_collections(self, drop_if_exist=False):
|
41
|
|
- pass
|
|
50
|
+ if_exists = 'drop' if drop_if_exist else 'nothing'
|
|
51
|
+ # Object collection
|
|
52
|
+ collection_name = utils.common_collections['object']
|
|
53
|
+ self._create_collection(collection_name, if_exists=if_exists)
|
|
54
|
+ # TODO triggers ?
|
|
55
|
+ object_collection_name = collection_name
|
|
56
|
+
|
|
57
|
+ # TODO collections de relations
|
|
58
|
+ # TODO clés étrangères
|
|
59
|
+
|
|
60
|
+ def _create_collection(self, collection_name, if_exists='nothing'):
|
|
61
|
+ if if_exists == 'drop':
|
|
62
|
+ if collection_name in self.database.collection_names():
|
|
63
|
+ self.database[collection_name].drop()
|
|
64
|
+ else:
|
|
65
|
+ if collection_name not in self.database.collection_names():
|
|
66
|
+ self.database.create_collection(collection_name)
|
|
67
|
+
|
|
68
|
+ # TODO Relational fields
|
|
69
|
+
|
|
70
|
+ def _delete_collection(self, collection_name):
|
|
71
|
+ # TODO : delete the triggers ?
|
|
72
|
+ if collection_name in self.database.collection_names():
|
|
73
|
+ self.database[collection_name].drop()
|