[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 31 Jul 2009 14:18:53 +0200
branchstable
changeset 2588 3a590ff82e99
parent 2587 79bbc5045288
child 2589 92f2bc945261
child 2688 afa99d53401c
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
misc/migration/3.3.5_Any.py
server/__init__.py
server/test/unittest_repository.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/migration/3.3.5_Any.py	Fri Jul 31 14:18:53 2009 +0200
@@ -0,0 +1,8 @@
+# some entities have been added before schema entities, fix the 'is' and
+# 'is_instance_of' relations
+for rtype in ('is', 'is_instance_of'):
+    sql('INSERT INTO %s_relation '
+        'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET '
+        'WHERE X.type=ET.cw_name AND NOT EXISTS('
+        '      SELECT 1 from is_relation '
+        '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype)
--- a/server/__init__.py	Fri Jul 31 12:04:32 2009 +0200
+++ b/server/__init__.py	Fri Jul 31 14:18:53 2009 +0200
@@ -92,14 +92,11 @@
         else:
             login, pwd = unicode(source['db-user']), source['db-password']
     print '-> inserting default user and default groups.'
-    needisfix = []
     for group in BASEGROUPS:
         rset = session.execute('INSERT CWGroup X: X name %(name)s',
                                {'name': unicode(group)})
-        needisfix.append( (rset.rows[0][0], rset.description[0][0]) )
     rset = session.execute('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s',
                            {'login': login, 'pwd': pwd})
-    needisfix.append( (rset.rows[0][0], rset.description[0][0]) )
     session.execute('SET U in_group G WHERE G name "managers"')
     session.commit()
     # reloging using the admin user
@@ -109,13 +106,6 @@
     handler = config.migration_handler(schema, interactive=False,
                                        cnx=cnx, repo=repo)
     initialize_schema(config, schema, handler)
-    # admin user and groups have been added before schema entities, fix the 'is'
-    # relation
-    for eid, etype in needisfix:
-        handler.session.unsafe_execute('SET X is E WHERE X eid %(x)s, E name %(name)s',
-                                       {'x': eid, 'name': etype}, 'x')
-        handler.session.unsafe_execute('SET X is_instance_of E WHERE X eid %(x)s, E name %(name)s',
-                                       {'x': eid, 'name': etype}, 'x')
     # insert versions
     handler.cmd_add_entity('CWProperty', pkey=u'system.version.cubicweb',
                            value=unicode(config.cubicweb_version()))
@@ -123,6 +113,15 @@
         handler.cmd_add_entity('CWProperty',
                                pkey=u'system.version.%s' % cube.lower(),
                                value=unicode(config.cube_version(cube)))
+    # some entities have been added before schema entities, fix the 'is' and
+    # 'is_instance_of' relations
+    for rtype in ('is', 'is_instance_of'):
+        handler.sqlexec(
+            'INSERT INTO %s_relation '
+            'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET '
+            'WHERE X.type=ET.cw_name AND NOT EXISTS('
+            '      SELECT 1 from is_relation '
+            '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype)
     # yoo !
     cnx.commit()
     config.enabled_sources = None
--- a/server/test/unittest_repository.py	Fri Jul 31 12:04:32 2009 +0200
+++ b/server/test/unittest_repository.py	Fri Jul 31 14:18:53 2009 +0200
@@ -324,6 +324,10 @@
         self.assertRaises(BadConnectionId, repo.set_shared_data, cnxid, 'data', 0)
         self.assertRaises(BadConnectionId, repo.get_shared_data, cnxid, 'data')
 
+    def test_schema_is_relation(self):
+        no_is_rset = self.execute('Any X WHERE NOT X is ET')
+        self.failIf(no_is_rset, no_is_rset.description)
+
 
 class DataHelpersTC(RepositoryBasedTC):