|
@@ -10,64 +10,5 @@ class MigrationHandlerChangeError(Exception):
|
10
|
10
|
pass
|
11
|
11
|
|
12
|
12
|
|
13
|
|
-## @brief Modifies a MongoDb database given editorial model changes
|
14
|
13
|
class MongoDbMigrationHandler(GenericMigrationHandler):
|
15
|
|
-
|
16
|
|
- ## @brief constructs a MongoDbMigrationHandler
|
17
|
|
- # @param conn_args dict : a dictionary containing connection options
|
18
|
|
- # @param **kwargs : extra arguments given to the connection methods
|
19
|
|
- def __init__(self, conn_args=None, **kwargs):
|
20
|
|
- if conn_args is None:
|
21
|
|
- conn_args = {} # TODO : récupérer les options de connexion dans les settings
|
22
|
|
- self.connection_name = conn_args['name']
|
23
|
|
- # del conn_args['module']
|
24
|
|
-
|
25
|
|
- self.db_conn = MongoDbDataSource(self.connection_name)
|
26
|
|
- self.database = self.db_conn.database
|
27
|
|
- # TODO Réimplémenter la partie sur les settings
|
28
|
|
- mh_settings = {}
|
29
|
|
- self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun']
|
30
|
|
- self.foreign_keys = kwargs['foreign_keys'] if 'foreign_keys' in kwargs else mh_settings['foreign_keys']
|
31
|
|
- self.drop_if_exists = kwargs['drop_if_exists'] if 'drop_if_exists' in kwargs else mh_settings['drop_if_exists']
|
32
|
|
- self._create_default_collections(self.drop_if_exists)
|
33
|
|
-
|
34
|
|
- ## @brief Modify the database given an EM change
|
35
|
|
- #
|
36
|
|
- # @param em model : The EditorialModel.model object to provide the global context.
|
37
|
|
- # @param uid str : The uid of the changed component.
|
38
|
|
- # @param initial_state dict|None : dict with field name as key and field value as value. Represents the original state. None means it's a creation of a new component.
|
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.
|
40
|
|
- # @throw MigrationHandlerChangeError if the change was refused
|
41
|
|
- def register_change(self, em, uid, initial_state, new_state):
|
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)
|
48
|
|
-
|
49
|
|
- def _create_default_collections(self, drop_if_exist=False):
|
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()
|
|
14
|
+ pass
|