diff --git a/lodel/datasource/mongodb/datasource.py b/lodel/datasource/mongodb/datasource.py index 7100d59..ae13b31 100644 --- a/lodel/datasource/mongodb/datasource.py +++ b/lodel/datasource/mongodb/datasource.py @@ -38,13 +38,7 @@ class MongoDbDataSource(GenericDataSource): # @return dict # @TODO Change the return value using the Lodel 2 settings module def _get_connection_args(self, connection_name): - return { - 'host': 'localhost', - 'port': 27017, - 'login': 'login', # TODO modifier la valeur - 'password': 'password', # TODO modifier la valeur - 'dbname': 'lodel' - } + return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'} ## @brief checks if the connection args are valid and complete # @param connection_args dict @@ -59,7 +53,7 @@ class MongoDbDataSource(GenericDataSource): if len(errors) > 0 : raise MongoDbDataSourceError("\r\n-".join(errors)) - return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'], + return (connection_args['login'], connection_args['password'], connection_args['host'], connection_args['port'], connection_args['dbname']) ## @brief returns a selection of documents from the datasource diff --git a/lodel/datasource/mongodb/migration_handler.py b/lodel/datasource/mongodb/migration_handler.py index 9301fc9..960b5c3 100644 --- a/lodel/datasource/mongodb/migration_handler.py +++ b/lodel/datasource/mongodb/migration_handler.py @@ -2,10 +2,14 @@ from lodel.datasource.generic.migrationhandler import GenericMigrationHandler from lodel.datasource.mongodb.datasource import MongoDbDataSource +import lodel.datasource.mongodb.utils as utils from lodel.editorial_model.components import EmClass, EmField + + class MigrationHandlerChangeError(Exception): pass + ## @brief Modifies a MongoDb database given editorial model changes class MongoDbMigrationHandler(GenericMigrationHandler): @@ -19,6 +23,7 @@ class MongoDbMigrationHandler(GenericMigrationHandler): # del conn_args['module'] self.db_conn = MongoDbDataSource(self.connection_name) + self.database = self.db_conn.database # TODO Réimplémenter la partie sur les settings mh_settings = {} self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun'] @@ -34,8 +39,35 @@ class MongoDbMigrationHandler(GenericMigrationHandler): # @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. # @throw MigrationHandlerChangeError if the change was refused def register_change(self, em, uid, initial_state, new_state): - pass - + if isinstance(em.classes(uid), EmClass): + collection_name = utils.object_collection_name(em.classes(uid).display_name) + if initial_state is None: + self._create_collection(collection_name) + elif new_state is None: + self._delete_collection(collection_name) def _create_default_collections(self, drop_if_exist=False): - pass + if_exists = 'drop' if drop_if_exist else 'nothing' + # Object collection + collection_name = utils.common_collections['object'] + self._create_collection(collection_name, if_exists=if_exists) + # TODO triggers ? + object_collection_name = collection_name + + # TODO collections de relations + # TODO clés étrangères + + def _create_collection(self, collection_name, if_exists='nothing'): + if if_exists == 'drop': + if collection_name in self.database.collection_names(): + self.database[collection_name].drop() + else: + if collection_name not in self.database.collection_names(): + self.database.create_collection(collection_name) + + # TODO Relational fields + + def _delete_collection(self, collection_name): + # TODO : delete the triggers ? + if collection_name in self.database.collection_names(): + self.database[collection_name].drop() diff --git a/lodel/datasource/mongodb/utils.py b/lodel/datasource/mongodb/utils.py index f67c3ee..dac6310 100644 --- a/lodel/datasource/mongodb/utils.py +++ b/lodel/datasource/mongodb/utils.py @@ -2,6 +2,10 @@ import pymongo +common_collections = { + 'object': 'object' +} + collection_prefix = { 'relation': 'rel_', 'collection': 'class_' @@ -30,6 +34,7 @@ MONGODB_SORT_OPERATORS_MAP = { 'DESC': -1 } + ## @brief Returns a collection name given a Emclass name # @param class_name str : The class name # @return str