No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

migration_handler.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. from lodel.datasource.generic.migrationhandler import GenericMigrationHandler
  3. from lodel.datasource.mongodb.datasource import MongoDbDataSource
  4. import lodel.datasource.mongodb.utils as utils
  5. from lodel.editorial_model.components import EmClass, EmField
  6. class MigrationHandlerChangeError(Exception):
  7. pass
  8. ## @brief Modifies a MongoDb database given editorial model changes
  9. class MongoDbMigrationHandler(GenericMigrationHandler):
  10. ## @brief constructs a MongoDbMigrationHandler
  11. # @param conn_args dict : a dictionary containing connection options
  12. # @param **kwargs : extra arguments given to the connection methods
  13. def __init__(self, conn_args=None, **kwargs):
  14. if conn_args is None:
  15. conn_args = {} # TODO : récupérer les options de connexion dans les settings
  16. self.connection_name = conn_args['name']
  17. # del conn_args['module']
  18. self.db_conn = MongoDbDataSource(self.connection_name)
  19. self.database = self.db_conn.database
  20. # TODO Réimplémenter la partie sur les settings
  21. mh_settings = {}
  22. self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun']
  23. self.foreign_keys = kwargs['foreign_keys'] if 'foreign_keys' in kwargs else mh_settings['foreign_keys']
  24. self.drop_if_exists = kwargs['drop_if_exists'] if 'drop_if_exists' in kwargs else mh_settings['drop_if_exists']
  25. self._create_default_collections(self.drop_if_exists)
  26. ## @brief Modify the database given an EM change
  27. #
  28. # @param em model : The EditorialModel.model object to provide the global context.
  29. # @param uid str : The uid of the changed component.
  30. # @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.
  31. # @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.
  32. # @throw MigrationHandlerChangeError if the change was refused
  33. def register_change(self, em, uid, initial_state, new_state):
  34. if isinstance(em.classes(uid), EmClass):
  35. collection_name = utils.object_collection_name(em.classes(uid).display_name)
  36. if initial_state is None:
  37. self._create_collection(collection_name)
  38. elif new_state is None:
  39. self._delete_collection(collection_name)
  40. def _create_default_collections(self, drop_if_exist=False):
  41. if_exists = 'drop' if drop_if_exist else 'nothing'
  42. # Object collection
  43. collection_name = utils.common_collections['object']
  44. self._create_collection(collection_name, if_exists=if_exists)
  45. # TODO triggers ?
  46. object_collection_name = collection_name
  47. # TODO collections de relations
  48. # TODO clés étrangères
  49. def _create_collection(self, collection_name, if_exists='nothing'):
  50. if if_exists == 'drop':
  51. if collection_name in self.database.collection_names():
  52. self.database[collection_name].drop()
  53. else:
  54. if collection_name not in self.database.collection_names():
  55. self.database.create_collection(collection_name)
  56. # TODO Relational fields
  57. def _delete_collection(self, collection_name):
  58. # TODO : delete the triggers ?
  59. if collection_name in self.database.collection_names():
  60. self.database[collection_name].drop()