|
@@ -3,7 +3,7 @@ import datetime
|
3
|
3
|
|
4
|
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
|
7
|
from lodel.leapi.datahandlers.base_classes import DataHandler
|
8
|
8
|
|
9
|
9
|
class MigrationHandlerChangeError(Exception):
|
|
@@ -22,11 +22,20 @@ class MongoDbMigrationHandler(object):
|
22
|
22
|
# @param **kwargs : extra arguments
|
23
|
23
|
def __init__(self, conn_args=None, **kwargs):
|
24
|
24
|
conn_args = get_connection_args() if conn_args is None else conn_args
|
|
25
|
+
|
25
|
26
|
self.connection_name = conn_args['name']
|
|
27
|
+
|
26
|
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
|
39
|
self._install_collections()
|
31
|
40
|
|
32
|
41
|
## @brief Installs the basis collections of the database
|
|
@@ -40,7 +49,8 @@ class MongoDbMigrationHandler(object):
|
40
|
49
|
# @param collection_name str
|
41
|
50
|
# @param charset str : default value is "utf8"
|
42
|
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
|
54
|
if collection_name in self.database.collection_names(include_system_collections=False):
|
45
|
55
|
if if_exists == MongoDbMigrationHandler.COMMANDS_IFEXISTS_DROP:
|
46
|
56
|
self._delete_collection(collection_name)
|
|
@@ -65,7 +75,8 @@ class MongoDbMigrationHandler(object):
|
65
|
75
|
def register_change(self, model, uid, initial_state, new_state):
|
66
|
76
|
|
67
|
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
|
81
|
if initial_state is None:
|
71
|
82
|
state_change = 'new'
|
|
@@ -85,7 +96,8 @@ class MongoDbMigrationHandler(object):
|
85
|
96
|
if hasattr(self, handler_func):
|
86
|
97
|
getattr(self, handler_func)(model, uid, initial_state, new_state)
|
87
|
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
|
102
|
def register_model_state(self, em, state_hash):
|
91
|
103
|
pass
|
|
@@ -118,17 +130,18 @@ class MongoDbMigrationHandler(object):
|
118
|
130
|
# @see register_change()
|
119
|
131
|
def _emfield_del(self, model, uid, initial_state, new_state):
|
120
|
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
|
134
|
self._delete_field_in_collection(collection_name, field_name)
|
123
|
135
|
|
124
|
136
|
## @brief upgrades a field
|
125
|
137
|
def _emfield_upgrade(self, model, uid, initial_state, new_state):
|
126
|
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
|
140
|
self._check_field_in_collection(collection_name, field_name, initial_state, new_state)
|
129
|
141
|
|
130
|
142
|
def _check_field_in_collection(self,collection_name, field_name, initial_sate, new_state):
|
131
|
143
|
collection = self.database[collection_name]
|
|
144
|
+ field_name = mongo_fieldname(field_name)
|
132
|
145
|
cursor = collection.find({field_name: {'$exists': True}}, {field_name: 1})
|
133
|
146
|
for document in cursor:
|
134
|
147
|
# TODO vérifier que le champ contient une donnée compatible (document[field_name])
|
|
@@ -161,11 +174,15 @@ class MongoDbMigrationHandler(object):
|
161
|
174
|
# @param options dict
|
162
|
175
|
def _create_field_in_collection(self, collection_name, field, options):
|
163
|
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
|
181
|
## @brief Deletes a field in a collection
|
167
|
182
|
# @param collection_name str
|
168
|
183
|
# @param field_name str
|
169
|
184
|
def _delete_field_in_collection(self, collection_name, field_name):
|
170
|
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)
|