server/migractions.py
changeset 10685 44cb0e9df181
parent 10669 155c29e0ed1c
child 10799 ec97974b9010
equal deleted inserted replaced
10684:7e9c9a32f24f 10685:44cb0e9df181
    39 from datetime import datetime
    39 from datetime import datetime
    40 from glob import glob
    40 from glob import glob
    41 from copy import copy
    41 from copy import copy
    42 from warnings import warn
    42 from warnings import warn
    43 from contextlib import contextmanager
    43 from contextlib import contextmanager
       
    44 
       
    45 from six import PY2, text_type
    44 
    46 
    45 from logilab.common.deprecation import deprecated
    47 from logilab.common.deprecation import deprecated
    46 from logilab.common.decorators import cached, clear_cache
    48 from logilab.common.decorators import cached, clear_cache
    47 
    49 
    48 from yams.constraints import SizeConstraint
    50 from yams.constraints import SizeConstraint
   151                 sys.exit(0)
   153                 sys.exit(0)
   152         self.session = self.repo._get_session(self.cnx.sessionid)
   154         self.session = self.repo._get_session(self.cnx.sessionid)
   153 
   155 
   154     def cube_upgraded(self, cube, version):
   156     def cube_upgraded(self, cube, version):
   155         self.cmd_set_property('system.version.%s' % cube.lower(),
   157         self.cmd_set_property('system.version.%s' % cube.lower(),
   156                               unicode(version))
   158                               text_type(version))
   157         self.commit()
   159         self.commit()
   158 
   160 
   159     def shutdown(self):
   161     def shutdown(self):
   160         if self.repo is not None:
   162         if self.repo is not None:
   161             self.repo.shutdown()
   163             self.repo.shutdown()
   991             self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname},
   993             self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname},
   992                          ask_confirm=False)
   994                          ask_confirm=False)
   993         # elif simply renaming an entity type
   995         # elif simply renaming an entity type
   994         else:
   996         else:
   995             self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s',
   997             self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s',
   996                          {'newname' : unicode(newname), 'on' : oldname},
   998                          {'newname' : text_type(newname), 'on' : oldname},
   997                          ask_confirm=False)
   999                          ask_confirm=False)
   998         if commit:
  1000         if commit:
   999             self.commit()
  1001             self.commit()
  1000 
  1002 
  1001     def cmd_add_relation_type(self, rtype, addrdef=True, commit=True):
  1003     def cmd_add_relation_type(self, rtype, addrdef=True, commit=True):
  1194             restriction.append('X relation_type RT, RT name "%s"' % rtype)
  1196             restriction.append('X relation_type RT, RT name "%s"' % rtype)
  1195         assert restriction
  1197         assert restriction
  1196         values = []
  1198         values = []
  1197         for k, v in kwargs.items():
  1199         for k, v in kwargs.items():
  1198             values.append('X %s %%(%s)s' % (k, k))
  1200             values.append('X %s %%(%s)s' % (k, k))
  1199             if isinstance(v, str):
  1201             if PY2 and isinstance(v, str):
  1200                 kwargs[k] = unicode(v)
  1202                 kwargs[k] = unicode(v)
  1201         rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction))
  1203         rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction))
  1202         self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2)
  1204         self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2)
  1203         if commit:
  1205         if commit:
  1204             self.commit()
  1206             self.commit()
  1227         elif not oldvalue is None:
  1229         elif not oldvalue is None:
  1228             if not size is None:
  1230             if not size is None:
  1229                 self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,'
  1231                 self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,'
  1230                              'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",'
  1232                              'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",'
  1231                              'S name "%s", R name "%s"' % (etype, rtype),
  1233                              'S name "%s", R name "%s"' % (etype, rtype),
  1232                              {'v': unicode(SizeConstraint(size).serialize())},
  1234                              {'v': text_type(SizeConstraint(size).serialize())},
  1233                              ask_confirm=self.verbosity>=2)
  1235                              ask_confirm=self.verbosity>=2)
  1234             else:
  1236             else:
  1235                 self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,'
  1237                 self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,'
  1236                              'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",'
  1238                              'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",'
  1237                              'S name "%s", R name "%s"' % (etype, rtype),
  1239                              'S name "%s", R name "%s"' % (etype, rtype),
  1264                    for the specified entity type(s); set it to false in
  1266                    for the specified entity type(s); set it to false in
  1265                    the case of a subworkflow
  1267                    the case of a subworkflow
  1266 
  1268 
  1267          :rtype: `Workflow`
  1269          :rtype: `Workflow`
  1268         """
  1270         """
  1269         wf = self.cmd_create_entity('Workflow', name=unicode(name),
  1271         wf = self.cmd_create_entity('Workflow', name=text_type(name),
  1270                                     **kwargs)
  1272                                     **kwargs)
  1271         if not isinstance(wfof, (list, tuple)):
  1273         if not isinstance(wfof, (list, tuple)):
  1272             wfof = (wfof,)
  1274             wfof = (wfof,)
  1273         def _missing_wf_rel(etype):
  1275         def _missing_wf_rel(etype):
  1274             return 'missing workflow relations, see make_workflowable(%s)' % etype
  1276             return 'missing workflow relations, see make_workflowable(%s)' % etype
  1275         for etype in wfof:
  1277         for etype in wfof:
  1276             eschema = self.repo.schema[etype]
  1278             eschema = self.repo.schema[etype]
  1277             etype = unicode(etype)
  1279             etype = text_type(etype)
  1278             if ensure_workflowable:
  1280             if ensure_workflowable:
  1279                 assert 'in_state' in eschema.subjrels, _missing_wf_rel(etype)
  1281                 assert 'in_state' in eschema.subjrels, _missing_wf_rel(etype)
  1280                 assert 'custom_workflow' in eschema.subjrels, _missing_wf_rel(etype)
  1282                 assert 'custom_workflow' in eschema.subjrels, _missing_wf_rel(etype)
  1281                 assert 'wf_info_for' in eschema.objrels, _missing_wf_rel(etype)
  1283                 assert 'wf_info_for' in eschema.objrels, _missing_wf_rel(etype)
  1282             rset = self.rqlexec(
  1284             rset = self.rqlexec(
  1283                 'SET X workflow_of ET WHERE X eid %(x)s, ET name %(et)s',
  1285                 'SET X workflow_of ET WHERE X eid %(x)s, ET name %(et)s',
  1284                 {'x': wf.eid, 'et': unicode(etype)}, ask_confirm=False)
  1286                 {'x': wf.eid, 'et': text_type(etype)}, ask_confirm=False)
  1285             assert rset, 'unexistant entity type %s' % etype
  1287             assert rset, 'unexistant entity type %s' % etype
  1286             if default:
  1288             if default:
  1287                 self.rqlexec(
  1289                 self.rqlexec(
  1288                     'SET ET default_workflow X WHERE X eid %(x)s, ET name %(et)s',
  1290                     'SET ET default_workflow X WHERE X eid %(x)s, ET name %(et)s',
  1289                     {'x': wf.eid, 'et': unicode(etype)}, ask_confirm=False)
  1291                     {'x': wf.eid, 'et': text_type(etype)}, ask_confirm=False)
  1290         if commit:
  1292         if commit:
  1291             self.commit()
  1293             self.commit()
  1292         return wf
  1294         return wf
  1293 
  1295 
  1294     def cmd_get_workflow_for(self, etype):
  1296     def cmd_get_workflow_for(self, etype):
  1315         given value.
  1317         given value.
  1316 
  1318 
  1317         To set a user specific property value, use appropriate method on CWUser
  1319         To set a user specific property value, use appropriate method on CWUser
  1318         instance.
  1320         instance.
  1319         """
  1321         """
  1320         value = unicode(value)
  1322         value = text_type(value)
  1321         try:
  1323         try:
  1322             prop = self.rqlexec(
  1324             prop = self.rqlexec(
  1323                 'CWProperty X WHERE X pkey %(k)s, NOT X for_user U',
  1325                 'CWProperty X WHERE X pkey %(k)s, NOT X for_user U',
  1324                 {'k': unicode(pkey)}, ask_confirm=False).get_entity(0, 0)
  1326                 {'k': text_type(pkey)}, ask_confirm=False).get_entity(0, 0)
  1325         except Exception:
  1327         except Exception:
  1326             self.cmd_create_entity('CWProperty', pkey=unicode(pkey), value=value)
  1328             self.cmd_create_entity('CWProperty', pkey=text_type(pkey), value=value)
  1327         else:
  1329         else:
  1328             prop.cw_set(value=value)
  1330             prop.cw_set(value=value)
  1329 
  1331 
  1330     # other data migration commands ###########################################
  1332     # other data migration commands ###########################################
  1331 
  1333