[migractions] remove any session related leftovers
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 13 Jun 2014 13:54:28 +0200
changeset 10353 d9a1e7939ee6
parent 10352 bab2befaac9b
child 10354 635cfac73d28
[migractions] remove any session related leftovers Also, the migration handler really wants a "repo" session anyway. This highlights a bug in unittest_wfobjs.test_duplicated_transition where an old connection is reused. It probably used to work because it was made of session + set_cnxset magic dust. Related to #3933480.
devtools/testlib.py
entities/test/unittest_wfobjs.py
server/__init__.py
server/migractions.py
server/test/unittest_migractions.py
test/unittest_cwctl.py
--- a/devtools/testlib.py	Fri Jun 13 12:07:51 2014 +0200
+++ b/devtools/testlib.py	Fri Jun 13 13:54:28 2014 +0200
@@ -261,7 +261,7 @@
     @contextmanager
     def shell(self):
         from cubicweb.server.migractions import ServerMigrationHelper
-        with repoapi.ClientConnection(self._session) as cnx:
+        with self._session.new_cnx() as cnx:
             mih = ServerMigrationHelper(None, repo=self._repo, cnx=cnx,
                                         interactive=False,
                                         # hack so it don't try to load fs schema
--- a/entities/test/unittest_wfobjs.py	Fri Jun 13 12:07:51 2014 +0200
+++ b/entities/test/unittest_wfobjs.py	Fri Jun 13 13:54:28 2014 +0200
@@ -87,12 +87,12 @@
             shell.rollback()
             # no pb if not in the same workflow
             wf2 = add_wf(shell, 'Company')
-            foo = wf.add_state(u'foo', initial=True)
-            bar = wf.add_state(u'bar')
-            wf.add_transition(u'baz', (foo,), bar, ('managers',))
+            foo = wf2.add_state(u'foo', initial=True)
+            bar = wf2.add_state(u'bar')
+            wf2.add_transition(u'baz', (foo,), bar, ('managers',))
             shell.commit()
             # gnark gnark
-            biz = wf.add_transition(u'biz', (bar,), foo)
+            biz = wf2.add_transition(u'biz', (bar,), foo)
             shell.commit()
             with self.assertRaises(ValidationError) as cm:
                 biz.cw_set(name=u'baz')
--- a/server/__init__.py	Fri Jun 13 12:07:51 2014 +0200
+++ b/server/__init__.py	Fri Jun 13 13:54:28 2014 +0200
@@ -289,6 +289,7 @@
     config._cubes = None # avoid assertion error
     repo = get_repository(config=config)
     with connect(repo, login, password=pwd) as cnx:
+        cnx = cnx._cnx # client connection -> repo connection
         with cnx.security_enabled(False, False):
             repo.system_source.eid = ssource.eid # redo this manually
             handler = config.migration_handler(schema, interactive=False,
--- a/server/migractions.py	Fri Jun 13 12:07:51 2014 +0200
+++ b/server/migractions.py	Fri Jun 13 13:54:28 2014 +0200
@@ -125,6 +125,8 @@
         self.fs_schema = schema
         self._synchronized = set()
 
+    # overriden from base MigrationHelper ######################################
+
     def set_cnx(self):
         try:
             login = self.repo.config.default_admin_config['login']
@@ -150,7 +152,6 @@
                 sys.exit(0)
         self.session = self.repo._get_session(self.cnx.sessionid)
 
-    # overriden from base MigrationHelper ######################################
 
     @cached
     def repo_connect(self):
@@ -177,7 +178,7 @@
             super(ServerMigrationHelper, self).migrate(vcconf, toupgrade, options)
 
     def cmd_process_script(self, migrscript, funcname=None, *args, **kwargs):
-        with self.cnx._cnx.ensure_cnx_set:
+        with self.cnx.ensure_cnx_set:
             try:
                 return super(ServerMigrationHelper, self).cmd_process_script(
                       migrscript, funcname, *args, **kwargs)
@@ -286,12 +287,10 @@
         print '-> database restored.'
 
     def commit(self):
-        if hasattr(self, 'cnx'):
-            self.cnx.commit(free_cnxset=False)
+        self.cnx.commit()
 
     def rollback(self):
-        if hasattr(self, 'cnx'):
-            self.cnx.rollback(free_cnxset=False)
+        self.cnx.rollback()
 
     def rqlexecall(self, rqliter, ask_confirm=False):
         for rql, kwargs in rqliter:
@@ -309,7 +308,7 @@
                         'schema': self.repo.get_schema(),
                         'cnx': self.cnx,
                         'fsschema': self.fs_schema,
-                        'session' : self.cnx._cnx,
+                        'session' : self.cnx,
                         'repo' : self.repo,
                         })
         return context
@@ -957,7 +956,6 @@
                              % (rtype, new.eid, oldeid), ask_confirm=False)
             # delete relations using SQL to avoid relations content removal
             # triggered by schema synchronization hooks.
-            session = self.session
             for rdeftype in ('CWRelation', 'CWAttribute'):
                 thispending = set( (eid for eid, in self.sqlexec(
                     'SELECT cw_eid FROM cw_%s WHERE cw_from_entity=%%(eid)s OR '
@@ -967,10 +965,10 @@
                 # get some validation error on commit since integrity hooks
                 # may think some required relation is missing... This also ensure
                 # repository caches are properly cleanup
-                hook.CleanupDeletedEidsCacheOp.get_instance(session).union(thispending)
+                hook.CleanupDeletedEidsCacheOp.get_instance(self.cnx).union(thispending)
                 # and don't forget to remove record from system tables
                 entities = [self.cnx.entity_from_eid(eid, rdeftype) for eid in thispending]
-                self.repo.system_source.delete_info_multi(self.cnx._cnx, entities)
+                self.repo.system_source.delete_info_multi(self.cnx, entities)
                 self.sqlexec('DELETE FROM cw_%s WHERE cw_from_entity=%%(eid)s OR '
                              'cw_to_entity=%%(eid)s' % rdeftype,
                              {'eid': oldeid}, ask_confirm=False)
@@ -1391,7 +1389,7 @@
         indexable entity types
         """
         from cubicweb.server.checkintegrity import reindex_entities
-        reindex_entities(self.repo.schema, self.cnx._cnx, etypes=etypes)
+        reindex_entities(self.repo.schema, self.cnx, etypes=etypes)
 
     @contextmanager
     def cmd_dropped_constraints(self, etype, attrname, cstrtype=None,
@@ -1490,7 +1488,7 @@
         self.sqlexec(sql, ask_confirm=False)
         dbhelper = self.repo.system_source.dbhelper
         sqltype = dbhelper.TYPE_MAPPING[newtype]
-        cursor = self.cnx._cnx.cnxset.cu
+        cursor = self.cnx.cnxset.cu
         dbhelper.change_col_type(cursor, 'cw_%s'  % etype, 'cw_%s' % attr, sqltype, allownull)
         if commit:
             self.commit()
--- a/server/test/unittest_migractions.py	Fri Jun 13 12:07:51 2014 +0200
+++ b/server/test/unittest_migractions.py	Fri Jun 13 13:54:28 2014 +0200
@@ -72,7 +72,7 @@
 
     @contextmanager
     def mh(self):
-        with self.admin_access.client_cnx() as cnx:
+        with self.admin_access.repo_cnx() as cnx:
             yield cnx, ServerMigrationHelper(self.repo.config, migrschema,
                                              repo=self.repo, cnx=cnx,
                                              interactive=False)
--- a/test/unittest_cwctl.py	Fri Jun 13 12:07:51 2014 +0200
+++ b/test/unittest_cwctl.py	Fri Jun 13 13:54:28 2014 +0200
@@ -44,7 +44,7 @@
 
     def test_process_script_args_context(self):
         repo = self.repo
-        with self.admin_access.client_cnx() as cnx:
+        with self.admin_access.repo_cnx() as cnx:
             mih = ServerMigrationHelper(None, repo=repo, cnx=cnx,
                                         interactive=False,
                                         # hack so it don't try to load fs schema