server/migractions.py
branchstable
changeset 3700 fd550e4dc515
parent 3689 deb13e88e037
child 3707 78596919ede3
equal deleted inserted replaced
3699:20ba545e00e1 3700:fd550e4dc515
    52     """
    52     """
    53 
    53 
    54     def __init__(self, config, schema, interactive=True,
    54     def __init__(self, config, schema, interactive=True,
    55                  repo=None, cnx=None, verbosity=1, connect=True):
    55                  repo=None, cnx=None, verbosity=1, connect=True):
    56         MigrationHelper.__init__(self, config, interactive, verbosity)
    56         MigrationHelper.__init__(self, config, interactive, verbosity)
       
    57         # no config on shell to a remote instance
    57         if not interactive:
    58         if not interactive:
    58             assert cnx
    59             assert cnx
    59             assert repo
    60             assert repo
    60         if cnx is not None:
    61         if cnx is not None:
    61             assert repo
    62             assert repo
    62             self._cnx = cnx
    63             self._cnx = cnx
    63             self.repo = repo
    64             self.repo = repo
    64             self.session.data['rebuild-infered'] = False
    65             if config is not None:
       
    66                 self.session.data['rebuild-infered'] = False
    65         elif connect:
    67         elif connect:
    66             self.repo_connect()
    68             self.repo_connect()
    67         if not schema:
    69         if not schema:
    68             schema = config.load_schema(expand_cubes=True)
    70             schema = config.load_schema(expand_cubes=True)
    69         self.fs_schema = schema
    71         self.fs_schema = schema
   231             self.session.data['rebuild-infered'] = False
   233             self.session.data['rebuild-infered'] = False
   232             return self._cnx
   234             return self._cnx
   233 
   235 
   234     @property
   236     @property
   235     def session(self):
   237     def session(self):
   236         return self.repo._get_session(self.cnx.sessionid)
   238         if self.config is not None:
       
   239             return self.repo._get_session(self.cnx.sessionid)
       
   240         # no access to session on remote instance
       
   241         return None
   237 
   242 
   238     def commit(self):
   243     def commit(self):
   239         if hasattr(self, '_cnx'):
   244         if hasattr(self, '_cnx'):
   240             self._cnx.commit()
   245             self._cnx.commit()
   241 
   246 
   266         return context
   271         return context
   267 
   272 
   268     @cached
   273     @cached
   269     def group_mapping(self):
   274     def group_mapping(self):
   270         """cached group mapping"""
   275         """cached group mapping"""
   271         self.session.set_pool()
   276         return ss.group_mapping(self._cw)
   272         return ss.group_mapping(self.session)
       
   273 
   277 
   274     def exec_event_script(self, event, cubepath=None, funcname=None,
   278     def exec_event_script(self, event, cubepath=None, funcname=None,
   275                           *args, **kwargs):
   279                           *args, **kwargs):
   276         if cubepath:
   280         if cubepath:
   277             apc = osp.join(cubepath, 'migration', '%s.py' % event)
   281             apc = osp.join(cubepath, 'migration', '%s.py' % event)
   979                    for the specified entity type(s); set it to false in
   983                    for the specified entity type(s); set it to false in
   980                    the case of a subworkflow
   984                    the case of a subworkflow
   981 
   985 
   982          :rtype: `Workflow`
   986          :rtype: `Workflow`
   983         """
   987         """
   984         self.session.set_pool() # ensure pool is set
       
   985         wf = self.cmd_create_entity('Workflow', name=unicode(name),
   988         wf = self.cmd_create_entity('Workflow', name=unicode(name),
   986                                     **kwargs)
   989                                     **kwargs)
   987         if not isinstance(wfof, (list, tuple)):
   990         if not isinstance(wfof, (list, tuple)):
   988             wfof = (wfof,)
   991             wfof = (wfof,)
   989         for etype in wfof:
   992         for etype in wfof:
   999             self.commit()
  1002             self.commit()
  1000         return wf
  1003         return wf
  1001 
  1004 
  1002     # XXX remove once cmd_add_[state|transition] are removed
  1005     # XXX remove once cmd_add_[state|transition] are removed
  1003     def _get_or_create_wf(self, etypes):
  1006     def _get_or_create_wf(self, etypes):
  1004         self.session.set_pool() # ensure pool is set
       
  1005         if not isinstance(etypes, (list, tuple)):
  1007         if not isinstance(etypes, (list, tuple)):
  1006             etypes = (etypes,)
  1008             etypes = (etypes,)
  1007         rset = self.rqlexec('Workflow X WHERE X workflow_of ET, ET name %(et)s',
  1009         rset = self.rqlexec('Workflow X WHERE X workflow_of ET, ET name %(et)s',
  1008                             {'et': etypes[0]})
  1010                             {'et': etypes[0]})
  1009         if rset:
  1011         if rset:
  1039                                        requiredgroups=(), conditions=(),
  1041                                        requiredgroups=(), conditions=(),
  1040                                        reset=True, commit=False):
  1042                                        reset=True, commit=False):
  1041         """set or add (if `reset` is False) groups and conditions for a
  1043         """set or add (if `reset` is False) groups and conditions for a
  1042         transition
  1044         transition
  1043         """
  1045         """
  1044         self.session.set_pool() # ensure pool is set
  1046         tr = self._cw.entity_from_eid(treid)
  1045         tr = self.session.entity_from_eid(treid)
       
  1046         tr.set_transition_permissions(requiredgroups, conditions, reset)
  1047         tr.set_transition_permissions(requiredgroups, conditions, reset)
  1047         if commit:
  1048         if commit:
  1048             self.commit()
  1049             self.commit()
  1049 
  1050 
  1050     @deprecated('[3.5] use entity.fire_transition("transition") or entity.change_state("state")')
  1051     @deprecated('[3.5] use entity.fire_transition("transition") or entity.change_state("state")')
  1051     def cmd_set_state(self, eid, statename, commit=False):
  1052     def cmd_set_state(self, eid, statename, commit=False):
  1052         self.session.set_pool() # ensure pool is set
  1053         self._cw.entity_from_eid(eid).change_state(statename)
  1053         self.session.entity_from_eid(eid).change_state(statename)
       
  1054         if commit:
  1054         if commit:
  1055             self.commit()
  1055             self.commit()
  1056 
  1056 
  1057     # CWProperty handling ######################################################
  1057     # CWProperty handling ######################################################
  1058 
  1058 
  1072             self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s',
  1072             self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s',
  1073                          {'k': pkey, 'v': value}, ask_confirm=False)
  1073                          {'k': pkey, 'v': value}, ask_confirm=False)
  1074 
  1074 
  1075     # other data migration commands ###########################################
  1075     # other data migration commands ###########################################
  1076 
  1076 
       
  1077     @property
       
  1078     def _cw(self):
       
  1079         session = self.session
       
  1080         if session is not None:
       
  1081             session.set_pool()
       
  1082             return session
       
  1083         return self.cnx.request()
       
  1084 
  1077     def cmd_create_entity(self, etype, *args, **kwargs):
  1085     def cmd_create_entity(self, etype, *args, **kwargs):
  1078         """add a new entity of the given type"""
  1086         """add a new entity of the given type"""
  1079         commit = kwargs.pop('commit', False)
  1087         commit = kwargs.pop('commit', False)
  1080         self.session.set_pool()
  1088         entity = self._cw.create_entity(etype, *args, **kwargs)
  1081         entity = self.session.create_entity(etype, *args, **kwargs)
       
  1082         if commit:
  1089         if commit:
  1083             self.commit()
  1090             self.commit()
  1084         return entity
  1091         return entity
  1085 
  1092 
  1086     @deprecated('use create_entity')
  1093     @deprecated('use create_entity')
  1112     def rqlexec(self, rql, kwargs=None, cachekey=None, ask_confirm=True):
  1119     def rqlexec(self, rql, kwargs=None, cachekey=None, ask_confirm=True):
  1113         """rql action"""
  1120         """rql action"""
  1114         if not isinstance(rql, (tuple, list)):
  1121         if not isinstance(rql, (tuple, list)):
  1115             rql = ( (rql, kwargs), )
  1122             rql = ( (rql, kwargs), )
  1116         res = None
  1123         res = None
  1117         self.session.set_pool()
       
  1118         for rql, kwargs in rql:
  1124         for rql, kwargs in rql:
  1119             if kwargs:
  1125             if kwargs:
  1120                 msg = '%s (%s)' % (rql, kwargs)
  1126                 msg = '%s (%s)' % (rql, kwargs)
  1121             else:
  1127             else:
  1122                 msg = rql
  1128                 msg = rql
  1123             if not ask_confirm or self.confirm('execute rql: %s ?' % msg):
  1129             if not ask_confirm or self.confirm('execute rql: %s ?' % msg):
  1124                 try:
  1130                 try:
  1125                     res = self.session.execute(rql, kwargs, cachekey)
  1131                     res = self._cw.execute(rql, kwargs, cachekey)
  1126                 except Exception, ex:
  1132                 except Exception, ex:
  1127                     if self.confirm('error: %s\nabort?' % ex):
  1133                     if self.confirm('error: %s\nabort?' % ex):
  1128                         raise
  1134                         raise
  1129         return res
  1135         return res
  1130 
  1136 
  1209         else:
  1215         else:
  1210             msg = rql
  1216             msg = rql
  1211         if self.ask_confirm:
  1217         if self.ask_confirm:
  1212             if not self._h.confirm('execute rql: %s ?' % msg):
  1218             if not self._h.confirm('execute rql: %s ?' % msg):
  1213                 raise StopIteration
  1219                 raise StopIteration
  1214         self._h.session.set_pool()
       
  1215         try:
  1220         try:
  1216             rset = self._h.session.execute(rql, kwargs)
  1221             rset = self._h._cw.execute(rql, kwargs)
  1217         except Exception, ex:
  1222         except Exception, ex:
  1218             if self._h.confirm('error: %s\nabort?' % ex):
  1223             if self._h.confirm('error: %s\nabort?' % ex):
  1219                 raise
  1224                 raise
  1220             else:
  1225             else:
  1221                 raise StopIteration
  1226                 raise StopIteration