server/migractions.py
changeset 9511 241b1232ed7f
parent 9478 2d7521881d3d
child 9555 370a7c40864f
equal deleted inserted replaced
9510:a5231a557ac0 9511:241b1232ed7f
    51 from cubicweb.predicates import is_instance
    51 from cubicweb.predicates import is_instance
    52 from cubicweb.schema import (ETYPE_NAME_MAP, META_RTYPES, VIRTUAL_RTYPES,
    52 from cubicweb.schema import (ETYPE_NAME_MAP, META_RTYPES, VIRTUAL_RTYPES,
    53                              PURE_VIRTUAL_RTYPES,
    53                              PURE_VIRTUAL_RTYPES,
    54                              CubicWebRelationSchema, order_eschemas)
    54                              CubicWebRelationSchema, order_eschemas)
    55 from cubicweb.cwvreg import CW_EVENT_MANAGER
    55 from cubicweb.cwvreg import CW_EVENT_MANAGER
    56 from cubicweb.dbapi import get_repository, _repo_connect
    56 from cubicweb import repoapi
    57 from cubicweb.migration import MigrationHelper, yes
    57 from cubicweb.migration import MigrationHelper, yes
    58 from cubicweb.server import hook, schemaserial as ss
    58 from cubicweb.server import hook, schemaserial as ss
    59 from cubicweb.server.utils import manager_userpasswd
    59 from cubicweb.server.utils import manager_userpasswd
    60 from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX
    60 from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX
    61 
    61 
   123 
   123 
   124     # overriden from base MigrationHelper ######################################
   124     # overriden from base MigrationHelper ######################################
   125 
   125 
   126     @cached
   126     @cached
   127     def repo_connect(self):
   127     def repo_connect(self):
   128         self.repo = get_repository(config=self.config)
   128         self.repo = repoapi.get_repository(config=self.config)
   129         return self.repo
   129         return self.repo
   130 
   130 
   131     def cube_upgraded(self, cube, version):
   131     def cube_upgraded(self, cube, version):
   132         self.cmd_set_property('system.version.%s' % cube.lower(),
   132         self.cmd_set_property('system.version.%s' % cube.lower(),
   133                               unicode(version))
   133                               unicode(version))
   266                 pwd = self.repo.config.default_admin_config['password']
   266                 pwd = self.repo.config.default_admin_config['password']
   267             except KeyError:
   267             except KeyError:
   268                 login, pwd = manager_userpasswd()
   268                 login, pwd = manager_userpasswd()
   269             while True:
   269             while True:
   270                 try:
   270                 try:
   271                     self._cnx = _repo_connect(self.repo, login, password=pwd)
   271                     self._cnx = repoapi.connect(self.repo, login, password=pwd)
   272                     if not 'managers' in self._cnx.user(self.session).groups:
   272                     if not 'managers' in self._cnx.user(self.session).groups:
   273                         print 'migration need an account in the managers group'
   273                         print 'migration need an account in the managers group'
   274                     else:
   274                     else:
   275                         break
   275                         break
   276                 except AuthenticationError:
   276                 except AuthenticationError:
   325                         'rql': self.rqlexec,
   325                         'rql': self.rqlexec,
   326                         'rqliter': self.rqliter,
   326                         'rqliter': self.rqliter,
   327                         'schema': self.repo.get_schema(),
   327                         'schema': self.repo.get_schema(),
   328                         'cnx': self.cnx,
   328                         'cnx': self.cnx,
   329                         'fsschema': self.fs_schema,
   329                         'fsschema': self.fs_schema,
   330                         'session' : self.session,
   330                         'session' : self.cnx._cnx,
   331                         'repo' : self.repo,
   331                         'repo' : self.repo,
   332                         })
   332                         })
   333         return context
   333         return context
   334 
   334 
   335     @cached
   335     @cached
   336     def group_mapping(self):
   336     def group_mapping(self):
   337         """cached group mapping"""
   337         """cached group mapping"""
   338         return ss.group_mapping(self._cw)
   338         return ss.group_mapping(self.cnx)
   339 
   339 
   340     @cached
   340     @cached
   341     def cstrtype_mapping(self):
   341     def cstrtype_mapping(self):
   342         """cached constraint types mapping"""
   342         """cached constraint types mapping"""
   343         return ss.cstrtype_mapping(self._cw)
   343         return ss.cstrtype_mapping(self.cnx)
   344 
   344 
   345     def cmd_exec_event_script(self, event, cube=None, funcname=None,
   345     def cmd_exec_event_script(self, event, cube=None, funcname=None,
   346                               *args, **kwargs):
   346                               *args, **kwargs):
   347         """execute a cube event scripts  `migration/<event>.py` where event
   347         """execute a cube event scripts  `migration/<event>.py` where event
   348         is one of 'precreate', 'postcreate', 'preremove' and 'postremove'.
   348         is one of 'precreate', 'postcreate', 'preremove' and 'postremove'.
   807             return
   807             return
   808         confirm = self.verbosity >= 2
   808         confirm = self.verbosity >= 2
   809         groupmap = self.group_mapping()
   809         groupmap = self.group_mapping()
   810         cstrtypemap = self.cstrtype_mapping()
   810         cstrtypemap = self.cstrtype_mapping()
   811         # register the entity into CWEType
   811         # register the entity into CWEType
   812         execute = self._cw.execute
   812         execute = self.cnx.execute
   813         ss.execschemarql(execute, eschema, ss.eschema2rql(eschema, groupmap))
   813         ss.execschemarql(execute, eschema, ss.eschema2rql(eschema, groupmap))
   814         # add specializes relation if needed
   814         # add specializes relation if needed
   815         specialized = eschema.specializes()
   815         specialized = eschema.specializes()
   816         if specialized:
   816         if specialized:
   817             try:
   817             try:
  1036         committing depends on the `commit` argument value).
  1036         committing depends on the `commit` argument value).
  1037 
  1037 
  1038         """
  1038         """
  1039         reposchema = self.repo.schema
  1039         reposchema = self.repo.schema
  1040         rschema = self.fs_schema.rschema(rtype)
  1040         rschema = self.fs_schema.rschema(rtype)
  1041         execute = self._cw.execute
  1041         execute = self.cnx.execute
  1042         if rtype in reposchema:
  1042         if rtype in reposchema:
  1043             print 'warning: relation type %s is already known, skip addition' % (
  1043             print 'warning: relation type %s is already known, skip addition' % (
  1044                 rtype)
  1044                 rtype)
  1045         else:
  1045         else:
  1046             # register the relation into CWRType and insert necessary relation
  1046             # register the relation into CWRType and insert necessary relation
  1108         if (subjtype, objtype) in self.repo.schema.rschema(rtype).rdefs:
  1108         if (subjtype, objtype) in self.repo.schema.rschema(rtype).rdefs:
  1109             print 'warning: relation %s %s %s is already known, skip addition' % (
  1109             print 'warning: relation %s %s %s is already known, skip addition' % (
  1110                 subjtype, rtype, objtype)
  1110                 subjtype, rtype, objtype)
  1111             return
  1111             return
  1112         rdef = self._get_rdef(rschema, subjtype, objtype)
  1112         rdef = self._get_rdef(rschema, subjtype, objtype)
  1113         ss.execschemarql(self._cw.execute, rdef,
  1113         ss.execschemarql(self.cnx.execute, rdef,
  1114                          ss.rdef2rql(rdef, self.cstrtype_mapping(),
  1114                          ss.rdef2rql(rdef, self.cstrtype_mapping(),
  1115                                      self.group_mapping()))
  1115                                      self.group_mapping()))
  1116         if commit:
  1116         if commit:
  1117             self.commit()
  1117             self.commit()
  1118 
  1118 
  1335         else:
  1335         else:
  1336             prop.cw_set(value=value)
  1336             prop.cw_set(value=value)
  1337 
  1337 
  1338     # other data migration commands ###########################################
  1338     # other data migration commands ###########################################
  1339 
  1339 
  1340     @property
       
  1341     def _cw(self):
       
  1342         session = self.session
       
  1343         if session is not None:
       
  1344             session.set_cnxset()
       
  1345             return session
       
  1346         return self.cnx.request()
       
  1347 
       
  1348     def cmd_storage_changed(self, etype, attribute):
  1340     def cmd_storage_changed(self, etype, attribute):
  1349         """migrate entities to a custom storage. The new storage is expected to
  1341         """migrate entities to a custom storage. The new storage is expected to
  1350         be set, it will be temporarily removed for the migration.
  1342         be set, it will be temporarily removed for the migration.
  1351         """
  1343         """
  1352         from logilab.common.shellutils import ProgressBar
  1344         from logilab.common.shellutils import ProgressBar
  1366         print
  1358         print
  1367         source.set_storage(etype, attribute, storage)
  1359         source.set_storage(etype, attribute, storage)
  1368 
  1360 
  1369     def cmd_create_entity(self, etype, commit=False, **kwargs):
  1361     def cmd_create_entity(self, etype, commit=False, **kwargs):
  1370         """add a new entity of the given type"""
  1362         """add a new entity of the given type"""
  1371         entity = self._cw.create_entity(etype, **kwargs)
  1363         entity = self.cnx.create_entity(etype, **kwargs)
  1372         if commit:
  1364         if commit:
  1373             self.commit()
  1365             self.commit()
  1374         return entity
  1366         return entity
  1375 
  1367 
  1376     def cmd_find_entities(self, etype, **kwargs):
  1368     def cmd_find_entities(self, etype, **kwargs):
  1377         """find entities of the given type and attribute values"""
  1369         """find entities of the given type and attribute values"""
  1378         return self._cw.find_entities(etype, **kwargs)
  1370         return self.cnx.find_entities(etype, **kwargs)
  1379 
  1371 
  1380     def cmd_find_one_entity(self, etype, **kwargs):
  1372     def cmd_find_one_entity(self, etype, **kwargs):
  1381         """find one entity of the given type and attribute values.
  1373         """find one entity of the given type and attribute values.
  1382 
  1374 
  1383         raise :exc:`cubicweb.req.FindEntityError` if can not return one and only
  1375         raise :exc:`cubicweb.req.FindEntityError` if can not return one and only
  1384         one entity.
  1376         one entity.
  1385         """
  1377         """
  1386         return self._cw.find_one_entity(etype, **kwargs)
  1378         return self.cnx.find_one_entity(etype, **kwargs)
  1387 
  1379 
  1388     def cmd_update_etype_fti_weight(self, etype, weight):
  1380     def cmd_update_etype_fti_weight(self, etype, weight):
  1389         if self.repo.system_source.dbdriver == 'postgres':
  1381         if self.repo.system_source.dbdriver == 'postgres':
  1390             self.sqlexec('UPDATE appears SET weight=%(weight)s '
  1382             self.sqlexec('UPDATE appears SET weight=%(weight)s '
  1391                          'FROM entities as X '
  1383                          'FROM entities as X '
  1440         should only be used for low level stuff undoable with existing higher
  1432         should only be used for low level stuff undoable with existing higher
  1441         level actions
  1433         level actions
  1442         """
  1434         """
  1443         if not ask_confirm or self.confirm('Execute sql: %s ?' % sql):
  1435         if not ask_confirm or self.confirm('Execute sql: %s ?' % sql):
  1444             try:
  1436             try:
  1445                 cu = self.session.system_sql(sql, args)
  1437                 cu = self.cnx._cnx.system_sql(sql, args)
  1446             except Exception:
  1438             except Exception:
  1447                 ex = sys.exc_info()[1]
  1439                 ex = sys.exc_info()[1]
  1448                 if self.confirm('Error: %s\nabort?' % ex, pdb=True):
  1440                 if self.confirm('Error: %s\nabort?' % ex, pdb=True):
  1449                     raise
  1441                     raise
  1450                 return
  1442                 return
  1458                 ask_confirm=False):
  1450                 ask_confirm=False):
  1459         """rql action"""
  1451         """rql action"""
  1460         if not isinstance(rql, (tuple, list)):
  1452         if not isinstance(rql, (tuple, list)):
  1461             rql = ( (rql, kwargs), )
  1453             rql = ( (rql, kwargs), )
  1462         res = None
  1454         res = None
  1463         execute = self._cw.execute
  1455         execute = self.cnx.execute
  1464         for rql, kwargs in rql:
  1456         for rql, kwargs in rql:
  1465             if kwargs:
  1457             if kwargs:
  1466                 msg = '%s (%s)' % (rql, kwargs)
  1458                 msg = '%s (%s)' % (rql, kwargs)
  1467             else:
  1459             else:
  1468                 msg = rql
  1460                 msg = rql