[migration] always rebuild infered relation

This was skipped for some bad reason (see 12ad88615a12 which introduce the change). Fix for #231956 in Yams is necessary to allow this cset: during a migration, we want to always reinfer relations while allowing explicit redefinition of an infered relation later. Else, we may run the migration with missing part of the schema (the one that should have been infered).

Closes #3685463

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changeset8f28ddd23de9
branchstable
phasedraft
hiddenyes
parent revision#69dcda164d63 Backout "[web/navigation] use add_onload instead of inline javascript href"
child revision<not specified>
files modified by this revision
hooks/syncschema.py
server/migractions.py
server/test/unittest_migractions.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1395927141 -3600
# Thu Mar 27 14:32:21 2014 +0100
# Branch stable
# Node ID 8f28ddd23de98de15815213283fa2bb2ddc27412
# Parent 69dcda164d63ff0e0f95cd569aa671b191e46231
[migration] always rebuild infered relation

This was skipped for some bad reason (see 12ad88615a12 which introduce the
change). Fix for #231956 in Yams is necessary to allow this cset: during a
migration, we want to always reinfer relations while allowing explicit
redefinition of an infered relation later. Else, we may run the migration with
missing part of the schema (the one that should have been infered).

Closes #3685463

diff --git a/hooks/syncschema.py b/hooks/syncschema.py
@@ -1,6 +1,6 @@
1 -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4  #
5  # This file is part of CubicWeb.
6  #
7  # CubicWeb is free software: you can redistribute it and/or modify it under the
@@ -194,17 +194,15 @@
8          for eschema in self.session.repo.schema.entities():
9              if not eschema.final:
10                  clear_cache(eschema, 'ordered_relations')
11 
12      def postcommit_event(self):
13 -        rebuildinfered = self.session.data.get('rebuild-infered', True)
14          repo = self.session.repo
15          # commit event should not raise error, while set_schema has chances to
16          # do so because it triggers full vreg reloading
17          try:
18 -            if rebuildinfered:
19 -                repo.schema.rebuild_infered_relations()
20 +            repo.schema.rebuild_infered_relations()
21              # trigger vreg reload
22              repo.set_schema(repo.schema)
23              # CWUser class might have changed, update current session users
24              cwuser_cls = self.session.vreg['etypes'].etype_class('CWUser')
25              for session in repo._sessions.itervalues():
diff --git a/server/migractions.py b/server/migractions.py
@@ -1,6 +1,6 @@
26 -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
27 +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
28  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
29  #
30  # This file is part of CubicWeb.
31  #
32  # CubicWeb is free software: you can redistribute it and/or modify it under the
@@ -100,11 +100,10 @@
33          elif connect:
34              self.repo_connect()
35          # no config on shell to a remote instance
36          if config is not None and (cnx or connect):
37              repo = self.repo
38 -            self.session.data['rebuild-infered'] = False
39              # register a hook to clear our group_mapping cache and the
40              # self._synchronized set when some group is added or updated
41              ClearGroupMap.mih = self
42              ClearGroupMap.mih_register(repo)
43              CW_EVENT_MANAGER.bind('after-registry-reload',
@@ -290,11 +289,10 @@
44                      login, pwd = manager_userpasswd()
45                  except (KeyboardInterrupt, EOFError):
46                      print 'aborting...'
47                      sys.exit(0)
48              self.session.keep_cnxset_mode('transaction')
49 -            self.session.data['rebuild-infered'] = False
50              return self._cnx
51 
52      @property
53      def session(self):
54          if self.config is not None:
diff --git a/server/test/unittest_migractions.py b/server/test/unittest_migractions.py
@@ -1,6 +1,6 @@
55 -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
56 +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
57  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
58  #
59  # This file is part of CubicWeb.
60  #
61  # CubicWeb is free software: you can redistribute it and/or modify it under the
@@ -223,11 +223,10 @@
62          self.assertTrue('Folder2' in self.schema)
63          self.assertTrue('Old' in self.schema)
64          self.assertTrue(self.session.execute('CWEType X WHERE X name "Folder2"'))
65          self.assertTrue('filed_under2' in self.schema)
66          self.assertTrue(self.session.execute('CWRType X WHERE X name "filed_under2"'))
67 -        self.schema.rebuild_infered_relations()
68          self.assertEqual(sorted(str(rs) for rs in self.schema['Folder2'].subject_relations()),
69                            ['created_by', 'creation_date', 'cw_source', 'cwuri',
70                             'description', 'description_format',
71                             'eid',
72                             'filed_under2', 'has_text',
@@ -269,11 +268,10 @@
73          self.mh.cmd_rename_attribute('New', 'name', 'new_name')
74 
75      def test_add_drop_relation_type(self):
76          self.mh.cmd_add_entity_type('Folder2', auto=False)
77          self.mh.cmd_add_relation_type('filed_under2')
78 -        self.schema.rebuild_infered_relations()
79          self.assertTrue('filed_under2' in self.schema)
80          # Old will be missing as it has been renamed into 'New' in the migrated
81          # schema while New hasn't been added here.
82          self.assertEqual(sorted(str(e) for e in self.schema['filed_under2'].subjects()),
83                           sorted(str(e) for e in self.schema.entities()
@@ -326,21 +324,15 @@
84                            ['Affaire', 'Division', 'Note', 'Societe', 'SubDivision'])
85          self.mh.cmd_drop_relation_definition('Affaire', 'concerne', 'Societe')
86          self.assertEqual(sorted(str(e) for e in self.schema['concerne'].subjects()),
87                            ['Affaire', 'Personne'])
88          self.assertEqual(sorted(str(e) for e in self.schema['concerne'].objects()),
89 -                          ['Affaire', 'Division', 'Note', 'SubDivision'])
90 -        self.schema.rebuild_infered_relations() # need to be explicitly called once everything is in place
91 -        self.assertEqual(sorted(str(e) for e in self.schema['concerne'].objects()),
92                            ['Affaire', 'Note'])
93          self.mh.cmd_add_relation_definition('Affaire', 'concerne', 'Societe')
94          self.assertEqual(sorted(str(e) for e in self.schema['concerne'].subjects()),
95                            ['Affaire', 'Personne'])
96          self.assertEqual(sorted(str(e) for e in self.schema['concerne'].objects()),
97 -                          ['Affaire', 'Note', 'Societe'])
98 -        self.schema.rebuild_infered_relations() # need to be explicitly called once everything is in place
99 -        self.assertEqual(sorted(str(e) for e in self.schema['concerne'].objects()),
100                            ['Affaire', 'Division', 'Note', 'Societe', 'SubDivision'])
101          # trick: overwrite self.maxeid to avoid deletion of just reintroduced types
102          self.maxeid = self.session.execute('Any MAX(X)')[0][0]
103 
104      def test_rename_relation(self):
@@ -597,16 +589,14 @@
105          self.assertEqual(str(cm.exception), "can't remove cube file, used as a dependency")
106 
107      @tag('longrun')
108      def test_introduce_base_class(self):
109          self.mh.cmd_add_entity_type('Para')
110 -        self.mh.repo.schema.rebuild_infered_relations()
111          self.assertEqual(sorted(et.type for et in self.schema['Para'].specialized_by()),
112                            ['Note'])
113          self.assertEqual(self.schema['Note'].specializes().type, 'Para')
114          self.mh.cmd_add_entity_type('Text')
115 -        self.mh.repo.schema.rebuild_infered_relations()
116          self.assertEqual(sorted(et.type for et in self.schema['Para'].specialized_by()),
117                            ['Note', 'Text'])
118          self.assertEqual(self.schema['Text'].specializes().type, 'Para')
119          # test columns have been actually added
120          text = self.session.execute('INSERT Text X: X para "hip", X summary "hop", X newattr "momo"').get_entity(0, 0)
@@ -626,16 +616,12 @@
121          # after_delete_entity(CWEType) hook, since in that case the MemSchemaSpecializesDel
122          # operation would be removed before, but I'm not sure this is a desired behaviour.
123          #
124          # also we need more tests about introducing/removing base classes or
125          # specialization relationship...
126 -        self.session.data['rebuild-infered'] = True
127 -        try:
128 -            self.session.execute('DELETE X specializes Y WHERE Y name "Para"')
129 -            self.session.commit(free_cnxset=False)
130 -        finally:
131 -            self.session.data['rebuild-infered'] = False
132 +        self.session.execute('DELETE X specializes Y WHERE Y name "Para"')
133 +        self.session.commit(free_cnxset=False)
134          self.assertEqual(sorted(et.type for et in self.schema['Para'].specialized_by()),
135                            [])
136          self.assertEqual(self.schema['Note'].specializes(), None)
137          self.assertEqual(self.schema['Text'].specializes(), None)
138