|
@@ -3,7 +3,7 @@ import datetime
|
3
|
3
|
|
4
|
4
|
from lodel.editorial_model.components import EmClass, EmField
|
5
|
5
|
from lodel.editorial_model.model import EditorialModel
|
6
|
|
-from .utils import get_connection_args, connect, collection_prefix, object_collection_name, mongo_fieldname
|
|
6
|
+from .utils import connect, object_collection_name, mongo_fieldname
|
7
|
7
|
from lodel.leapi.datahandlers.base_classes import DataHandler
|
8
|
8
|
from lodel.plugin import LodelHook
|
9
|
9
|
from leapi_dyncode import *
|
|
@@ -17,51 +17,22 @@ class MigrationHandlerChangeError(Exception):
|
17
|
17
|
class MigrationHandlerError(Exception):
|
18
|
18
|
pass
|
19
|
19
|
|
20
|
|
-@LodelHook('mongodb_mh_init_db')
|
21
|
|
-def mongodb_mh_init_db(classes_list, conn_args=None):
|
22
|
|
- connection_args = get_connection_args('default') if conn_args is None else get_connection_args(conn_args['name'])
|
23
|
|
- migration_handler = MigrationHandler(conn_args=connection_args)
|
24
|
|
- migration_handler.init_db(classes_list)
|
25
|
|
- migration_handler.database.close()
|
26
|
|
-
|
27
|
20
|
class MigrationHandler(object):
|
28
|
21
|
|
29
|
|
- COMMANDS_IFEXISTS_DROP = 'drop'
|
30
|
|
- COMMANDS_IFEXISTS_NOTHING = 'nothing'
|
31
|
|
- MIGRATION_HANDLER_DEFAULT_SETTINGS = {'dry_run': False, 'foreign_keys': True, 'drop_if_exists': False}
|
32
|
|
-
|
33
|
22
|
## @brief Constructs a MongoDbMigrationHandler
|
34
|
23
|
# @param conn_args dict : a dictionary containing the connection options
|
35
|
24
|
# @param **kwargs : extra arguments
|
36
|
|
- def __init__(self, host, port, db_name, username, password, **kwargs):
|
37
|
|
- conn_args = {'host': host, 'port': port, 'db_name': db_name,
|
38
|
|
- 'username': username, 'password': password}
|
39
|
|
-
|
40
|
|
- self.database = connect(host=conn_args['host'], port=conn_args['port'], db_name=conn_args['db_name'],
|
41
|
|
- username=conn_args['username'], password=conn_args['password'])
|
42
|
|
-
|
43
|
|
- self.dry_run = kwargs['dry_run'] if 'dry_run' in kwargs else \
|
44
|
|
- MigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['dry_run']
|
|
25
|
+ def __init__(self, host, port, db_name, username, password,
|
|
26
|
+ charset='utf-8', dry_run = False, drop_if_exists = False):
|
45
|
27
|
|
46
|
|
- self.foreign_keys = kwargs['foreign_keys'] if 'foreign_keys' in kwargs else \
|
47
|
|
- MigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['foreign_keys']
|
48
|
|
-
|
49
|
|
- self.drop_if_exists = kwargs['drop_if_exists'] if 'drop_is_exists' in kwargs else \
|
50
|
|
- MigrationHandler.MIGRATION_HANDLER_DEFAULT_SETTINGS['drop_if_exists']
|
|
28
|
+ self.database = connect(host, port, db_name, username, password)
|
|
29
|
+ self.dry_run = dry_run
|
|
30
|
+ self.drop_if_exists = drop_if_exists
|
|
31
|
+ self.charset = charset # Useless ?
|
51
|
32
|
|
52
|
|
- self.init_collections_names = None
|
53
|
33
|
logger.debug("MongoDb migration handler instanciated on db : \
|
54
|
34
|
%s@%s:%s" % (db_name, host, port))
|
55
|
35
|
|
56
|
|
- def _set_init_collection_names(self, emclass_list):
|
57
|
|
- collection_names = ['relation']
|
58
|
|
- for dynclass in emclass_list:
|
59
|
|
- if not dynclass.is_abstract() \
|
60
|
|
- and isinstance(dynclass._ro_datasource,MongoDbDatasource) \
|
61
|
|
- and isinstance(dynclass._rw_datasource, MongoDbDatasource):
|
62
|
|
- collection_names.append(dynclass.__name__)
|
63
|
|
- self.init_collections_names = collection_names
|
64
|
|
-
|
65
|
36
|
## @brief Installs the basis collections of the database
|
66
|
37
|
def init_db(self, emclass_list):
|
67
|
38
|
for collection_name in [ object_collection_name(cls)
|
|
@@ -71,10 +42,11 @@ class MigrationHandler(object):
|
71
|
42
|
## @brief Creates a collection in the database
|
72
|
43
|
# @param collection_name str
|
73
|
44
|
# @param charset str : default value is "utf8"
|
74
|
|
- # @param if_exists str : defines the behavior to have if the collection to create already exists (default value : "nothing")
|
75
|
|
- def _create_collection(self, collection_name, charset='utf8', if_exists=COMMANDS_IFEXISTS_NOTHING):
|
76
|
|
- if collection_name in self.database.collection_names(include_system_collections=False):
|
77
|
|
- if if_exists == MigrationHandler.COMMANDS_IFEXISTS_DROP:
|
|
45
|
+ def _create_collection(self, collection_name):
|
|
46
|
+ existing = self.database.collection_names(
|
|
47
|
+ include_system_collections=False)
|
|
48
|
+ if collection_name in existing:
|
|
49
|
+ if self.drop_if_exists:
|
78
|
50
|
self._delete_collection(collection_name)
|
79
|
51
|
logger.debug("Collection %s deleted before creating \
|
80
|
52
|
it again" % collection_name)
|
|
@@ -139,9 +111,8 @@ Doing nothing..." % collection_name)
|
139
|
111
|
## @brief deletes a collection corresponding to a given uid
|
140
|
112
|
# @see register_change()
|
141
|
113
|
def _emclass_delete(self, model, uid, initial_state, new_state):
|
142
|
|
- if uid not in self.init_collections_names:
|
143
|
|
- collection_name = object_collection_name(model.classes(uid))
|
144
|
|
- self._delete_collection(collection_name)
|
|
114
|
+ collection_name = object_collection_name(model.classes(uid))
|
|
115
|
+ self._delete_collection(collection_name)
|
145
|
116
|
|
146
|
117
|
## @brief creates a new field in a collection
|
147
|
118
|
# @see register_change()
|