Browse Source

The mongodb migration handler uses now the mongo_filename function

Roland Haroutiounian 8 years ago
parent
commit
c595ee23a1
1 changed files with 28 additions and 11 deletions
  1. 28
    11
      plugins/mongodb_datasource/migration_handler.py

+ 28
- 11
plugins/mongodb_datasource/migration_handler.py View File

3
 
3
 
4
 from lodel.editorial_model.components import EmClass, EmField
4
 from lodel.editorial_model.components import EmClass, EmField
5
 
5
 
6
-from .utils import get_connection_args, mongodbconnect, collection_prefix, object_collection_name
6
+from .utils import get_connection_args, mongodbconnect, collection_prefix, object_collection_name, mongo_fieldname
7
 from lodel.leapi.datahandlers.base_classes import DataHandler
7
 from lodel.leapi.datahandlers.base_classes import DataHandler
8
 
8
 
9
 class MigrationHandlerChangeError(Exception):
9
 class MigrationHandlerChangeError(Exception):
22
     # @param **kwargs : extra arguments
22
     # @param **kwargs : extra arguments
23
     def __init__(self, conn_args=None, **kwargs):
23
     def __init__(self, conn_args=None, **kwargs):
24
         conn_args = get_connection_args() if conn_args is None else conn_args
24
         conn_args = get_connection_args() if conn_args is None else conn_args
25
+
25
         self.connection_name = conn_args['name']
26
         self.connection_name = conn_args['name']
27
+
26
         self.database = mongodbconnect(self.connection_name)
28
         self.database = mongodbconnect(self.connection_name)
27
-        self.dry_run = kwargs['dry_run'] if 'dry_run' in kwargs else MongoDbMigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['dry_run']
28
-        self.foreign_keys = kwargs['foreign_keys'] if 'foreign_keys' in kwargs else MongoDbMigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['foreign_keys']
29
-        self.drop_if_exists = kwargs['drop_if_exists'] if 'drop_is_exists' in kwargs else MongoDbMigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['drop_if_exists']
29
+
30
+        self.dry_run = kwargs['dry_run'] if 'dry_run' in kwargs else \
31
+            MongoDbMigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['dry_run']
32
+
33
+        self.foreign_keys = kwargs['foreign_keys'] if 'foreign_keys' in kwargs else \
34
+            MongoDbMigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['foreign_keys']
35
+
36
+        self.drop_if_exists = kwargs['drop_if_exists'] if 'drop_is_exists' in kwargs else \
37
+            MongoDbMigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['drop_if_exists']
38
+
30
         self._install_collections()
39
         self._install_collections()
31
 
40
 
32
     ## @brief Installs the basis collections of the database
41
     ## @brief Installs the basis collections of the database
40
     # @param collection_name str
49
     # @param collection_name str
41
     # @param charset str : default value is "utf8"
50
     # @param charset str : default value is "utf8"
42
     # @param if_exists str : defines the behavior to have if the collection to create already exists (default value : "nothing")
51
     # @param if_exists str : defines the behavior to have if the collection to create already exists (default value : "nothing")
43
-    def _create_collection(self, collection_name, charset='utf8', if_exists=MongoDbMigrationHandler.COMMANDS_IFEXISTS_NOTHING):
52
+    def _create_collection(self, collection_name, charset='utf8',
53
+                           if_exists=MongoDbMigrationHandler.COMMANDS_IFEXISTS_NOTHING):
44
         if collection_name in self.database.collection_names(include_system_collections=False):
54
         if collection_name in self.database.collection_names(include_system_collections=False):
45
             if if_exists == MongoDbMigrationHandler.COMMANDS_IFEXISTS_DROP:
55
             if if_exists == MongoDbMigrationHandler.COMMANDS_IFEXISTS_DROP:
46
                 self._delete_collection(collection_name)
56
                 self._delete_collection(collection_name)
65
     def register_change(self, model, uid, initial_state, new_state):
75
     def register_change(self, model, uid, initial_state, new_state):
66
 
76
 
67
         if initial_state is None and new_state is None:
77
         if initial_state is None and new_state is None:
68
-            raise ValueError('An Editorial Model change should have at least one state precised (initial or new), none given here')
78
+            raise ValueError('An Editorial Model change should have at least one state precised (initial or new), '
79
+                             'none given here')
69
 
80
 
70
         if initial_state is None:
81
         if initial_state is None:
71
             state_change = 'new'
82
             state_change = 'new'
85
             if hasattr(self, handler_func):
96
             if hasattr(self, handler_func):
86
                 getattr(self, handler_func)(model, uid, initial_state, new_state)
97
                 getattr(self, handler_func)(model, uid, initial_state, new_state)
87
         else:
98
         else:
88
-            raise ValueError("The component concerned in this change should be an EmClass or EmField instance, %s given", model.classes(uid).__class__)
99
+            raise ValueError("The component concerned should be an EmClass or EmField instance, %s given",
100
+                             model.classes(uid).__class__)
89
 
101
 
90
     def register_model_state(self, em, state_hash):
102
     def register_model_state(self, em, state_hash):
91
         pass
103
         pass
118
     # @see register_change()
130
     # @see register_change()
119
     def _emfield_del(self, model, uid, initial_state, new_state):
131
     def _emfield_del(self, model, uid, initial_state, new_state):
120
         collection_name = self._class_collection_name_from_field(model, initial_state)
132
         collection_name = self._class_collection_name_from_field(model, initial_state)
121
-        field_name = model.field(uid).name
133
+        field_name = mongo_fieldname(model.field(uid).name)
122
         self._delete_field_in_collection(collection_name, field_name)
134
         self._delete_field_in_collection(collection_name, field_name)
123
 
135
 
124
     ## @brief upgrades a field
136
     ## @brief upgrades a field
125
     def _emfield_upgrade(self, model, uid, initial_state, new_state):
137
     def _emfield_upgrade(self, model, uid, initial_state, new_state):
126
         collection_name = self._class_collection_name_from_field(model, initial_state)
138
         collection_name = self._class_collection_name_from_field(model, initial_state)
127
-        field_name = model.field(uid).name
139
+        field_name = mongo_fieldname(model.field(uid).name)
128
         self._check_field_in_collection(collection_name, field_name, initial_state, new_state)
140
         self._check_field_in_collection(collection_name, field_name, initial_state, new_state)
129
 
141
 
130
     def _check_field_in_collection(self,collection_name, field_name, initial_sate, new_state):
142
     def _check_field_in_collection(self,collection_name, field_name, initial_sate, new_state):
131
         collection = self.database[collection_name]
143
         collection = self.database[collection_name]
144
+        field_name = mongo_fieldname(field_name)
132
         cursor = collection.find({field_name: {'$exists': True}}, {field_name: 1})
145
         cursor = collection.find({field_name: {'$exists': True}}, {field_name: 1})
133
         for document in cursor:
146
         for document in cursor:
134
             # TODO vérifier que le champ contient une donnée compatible (document[field_name])
147
             # TODO vérifier que le champ contient une donnée compatible (document[field_name])
161
     # @param options dict
174
     # @param options dict
162
     def _create_field_in_collection(self, collection_name, field, options):
175
     def _create_field_in_collection(self, collection_name, field, options):
163
         emfield = EmField(field)
176
         emfield = EmField(field)
164
-        self.database[collection_name].update_many({'uid': emfield.get_emclass_uid(), field: {'$exists': False}}, {'$set': {field: options['default']}}, False)
177
+        field_name = mongo_fieldname(field)
178
+        self.database[collection_name].update_many({'uid': emfield.get_emclass_uid(), field_name: {'$exists': False}},
179
+                                                   {'$set': {field_name: options['default']}}, False)
165
 
180
 
166
     ## @brief Deletes a field in a collection
181
     ## @brief Deletes a field in a collection
167
     # @param collection_name str
182
     # @param collection_name str
168
     # @param field_name str
183
     # @param field_name str
169
     def _delete_field_in_collection(self, collection_name, field_name):
184
     def _delete_field_in_collection(self, collection_name, field_name):
170
         if field_name != '_id':
185
         if field_name != '_id':
171
-            self.database[collection_name].update_many({field_name:{'$exists': True}}, {'$unset':{field_name:1}}, False)
186
+            field_name = mongo_fieldname(field_name)
187
+            self.database[collection_name].update_many({field_name: {'$exists': True}},
188
+                                                       {'$unset': {field_name:1}}, False)

Loading…
Cancel
Save