[schema2sql] properly consider skip_relations parameters.
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 01 Jul 2015 12:03:00 +0200
changeset 10481 6ac4b1726e9f
parent 10478 026b4ee032fb
child 10482 88119421a09c
[schema2sql] properly consider skip_relations parameters. This is the same bug as yams #286912. Also, introducing a unique function telling if the relation should have a table or not unhide some other problems related to computed relation, or missing skipped relations information.
server/schema2sql.py
server/test/unittest_schema2sql.py
--- a/server/schema2sql.py	Fri Jun 26 14:30:24 2015 +0200
+++ b/server/schema2sql.py	Wed Jul 01 12:03:00 2015 +0200
@@ -1,7 +1,7 @@
-# copyright 2004-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2004-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
-# This file is part of yams.
+# This file is part of cubicweb.
 #
 # yams is free software: you can redistribute it and/or modify it under the
 # terms of the GNU Lesser General Public License as published by the Free
@@ -31,6 +31,10 @@
 # SET_DEFAULT to True
 SET_DEFAULT = False
 
+def rschema_has_table(rschema, skip_relations):
+    """Return True if the given schema should have a table in the database"""
+    return not (rschema.final or rschema.inlined or rschema.rule or rschema.type in skip_relations)
+
 
 def schema2sql(dbhelper, schema, skip_entities=(), skip_relations=(), prefix=''):
     """write to the output stream a SQL schema to store the objects
@@ -45,9 +49,8 @@
         w(eschema2sql(dbhelper, eschema, skip_relations, prefix=prefix))
     for rtype in sorted(schema.relations()):
         rschema = schema.rschema(rtype)
-        if rschema.final or rschema.inlined or rschema.rule:
-            continue
-        w(rschema2sql(rschema))
+        if rschema_has_table(rschema, skip_relations):
+            w(rschema2sql(rschema))
     return '\n'.join(output)
 
 
@@ -66,9 +69,8 @@
             w(stmt)
     for rtype in sorted(schema.relations()):
         rschema = schema.rschema(rtype)
-        if rschema.final or rschema.inlined:
-            continue
-        w(droprschema2sql(rschema))
+        if rschema_has_table(rschema, skip_relations):
+            w(droprschema2sql(rschema))
     return '\n'.join(output)
 
 
@@ -246,7 +248,7 @@
 def rschema2sql(rschema):
     assert not rschema.rule
     return _SQL_SCHEMA % {'table': '%s_relation' % rschema.type}
-    
+
 
 def droprschema2sql(rschema):
     """return sql to drop a relation type's table"""
@@ -268,9 +270,8 @@
         w(grant_eschema(eschema, user, set_owner, prefix=prefix))
     for rtype in sorted(schema.relations()):
         rschema = schema.rschema(rtype)
-        if rschema.final or rschema.inlined:
-            continue
-        w(grant_rschema(rschema, user, set_owner))
+        if rschema_has_table(rschema, skip_relations=()):  # XXX skip_relations should be specified
+            w(grant_rschema(rschema, user, set_owner))
     return '\n'.join(output)
 
 
--- a/server/test/unittest_schema2sql.py	Fri Jun 26 14:30:24 2015 +0200
+++ b/server/test/unittest_schema2sql.py	Wed Jul 01 12:03:00 2015 +0200
@@ -271,22 +271,13 @@
 
 CREATE INDEX travaille_relation_from_idx ON travaille_relation(eid_from);
 CREATE INDEX travaille_relation_to_idx ON travaille_relation(eid_to);
-
-CREATE TABLE works_for_relation (
-  eid_from INTEGER NOT NULL REFERENCES entities (eid),
-  eid_to INTEGER NOT NULL REFERENCES entities (eid),
-  CONSTRAINT works_for_relation_p_key PRIMARY KEY(eid_from, eid_to)
-);
-
-CREATE INDEX works_for_relation_from_idx ON works_for_relation(eid_from);
-CREATE INDEX works_for_relation_to_idx ON works_for_relation(eid_to);
 """
 
 class SQLSchemaTC(TestCase):
 
     def test_known_values(self):
         dbhelper = get_db_helper('postgres')
-        output = schema2sql.schema2sql(dbhelper, schema)
+        output = schema2sql.schema2sql(dbhelper, schema, skip_relations=('works_for',))
         self.assertMultiLineEqual(EXPECTED_DATA_NO_DROP.strip(), output.strip())