server/schema2sql.py
changeset 10481 6ac4b1726e9f
parent 10444 fb7c1013189e
child 10671 e08102f45237
equal deleted inserted replaced
10478:026b4ee032fb 10481:6ac4b1726e9f
     1 # copyright 2004-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2004-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of yams.
     4 # This file is part of cubicweb.
     5 #
     5 #
     6 # yams is free software: you can redistribute it and/or modify it under the
     6 # yams is free software: you can redistribute it and/or modify it under the
     7 # terms of the GNU Lesser General Public License as published by the Free
     7 # terms of the GNU Lesser General Public License as published by the Free
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
     9 # any later version.
     9 # any later version.
    29 
    29 
    30 # default are usually not handled at the sql level. If you want them, set
    30 # default are usually not handled at the sql level. If you want them, set
    31 # SET_DEFAULT to True
    31 # SET_DEFAULT to True
    32 SET_DEFAULT = False
    32 SET_DEFAULT = False
    33 
    33 
       
    34 def rschema_has_table(rschema, skip_relations):
       
    35     """Return True if the given schema should have a table in the database"""
       
    36     return not (rschema.final or rschema.inlined or rschema.rule or rschema.type in skip_relations)
       
    37 
    34 
    38 
    35 def schema2sql(dbhelper, schema, skip_entities=(), skip_relations=(), prefix=''):
    39 def schema2sql(dbhelper, schema, skip_entities=(), skip_relations=(), prefix=''):
    36     """write to the output stream a SQL schema to store the objects
    40     """write to the output stream a SQL schema to store the objects
    37     corresponding to the given schema
    41     corresponding to the given schema
    38     """
    42     """
    43         if eschema.final or eschema.type in skip_entities:
    47         if eschema.final or eschema.type in skip_entities:
    44             continue
    48             continue
    45         w(eschema2sql(dbhelper, eschema, skip_relations, prefix=prefix))
    49         w(eschema2sql(dbhelper, eschema, skip_relations, prefix=prefix))
    46     for rtype in sorted(schema.relations()):
    50     for rtype in sorted(schema.relations()):
    47         rschema = schema.rschema(rtype)
    51         rschema = schema.rschema(rtype)
    48         if rschema.final or rschema.inlined or rschema.rule:
    52         if rschema_has_table(rschema, skip_relations):
    49             continue
    53             w(rschema2sql(rschema))
    50         w(rschema2sql(rschema))
       
    51     return '\n'.join(output)
    54     return '\n'.join(output)
    52 
    55 
    53 
    56 
    54 def dropschema2sql(dbhelper, schema, skip_entities=(), skip_relations=(), prefix=''):
    57 def dropschema2sql(dbhelper, schema, skip_entities=(), skip_relations=(), prefix=''):
    55     """write to the output stream a SQL schema to store the objects
    58     """write to the output stream a SQL schema to store the objects
    64         stmts = dropeschema2sql(dbhelper, eschema, skip_relations, prefix=prefix)
    67         stmts = dropeschema2sql(dbhelper, eschema, skip_relations, prefix=prefix)
    65         for stmt in stmts:
    68         for stmt in stmts:
    66             w(stmt)
    69             w(stmt)
    67     for rtype in sorted(schema.relations()):
    70     for rtype in sorted(schema.relations()):
    68         rschema = schema.rschema(rtype)
    71         rschema = schema.rschema(rtype)
    69         if rschema.final or rschema.inlined:
    72         if rschema_has_table(rschema, skip_relations):
    70             continue
    73             w(droprschema2sql(rschema))
    71         w(droprschema2sql(rschema))
       
    72     return '\n'.join(output)
    74     return '\n'.join(output)
    73 
    75 
    74 
    76 
    75 def eschema_attrs(eschema, skip_relations):
    77 def eschema_attrs(eschema, skip_relations):
    76     attrs = [attrdef for attrdef in eschema.attribute_definitions()
    78     attrs = [attrdef for attrdef in eschema.attribute_definitions()
   244 
   246 
   245 
   247 
   246 def rschema2sql(rschema):
   248 def rschema2sql(rschema):
   247     assert not rschema.rule
   249     assert not rschema.rule
   248     return _SQL_SCHEMA % {'table': '%s_relation' % rschema.type}
   250     return _SQL_SCHEMA % {'table': '%s_relation' % rschema.type}
   249     
   251 
   250 
   252 
   251 def droprschema2sql(rschema):
   253 def droprschema2sql(rschema):
   252     """return sql to drop a relation type's table"""
   254     """return sql to drop a relation type's table"""
   253     # not necessary to drop indexes, that's implictly done when dropping
   255     # not necessary to drop indexes, that's implictly done when dropping
   254     # the table
   256     # the table
   266         if eschema.final or etype in skip_entities:
   268         if eschema.final or etype in skip_entities:
   267             continue
   269             continue
   268         w(grant_eschema(eschema, user, set_owner, prefix=prefix))
   270         w(grant_eschema(eschema, user, set_owner, prefix=prefix))
   269     for rtype in sorted(schema.relations()):
   271     for rtype in sorted(schema.relations()):
   270         rschema = schema.rschema(rtype)
   272         rschema = schema.rschema(rtype)
   271         if rschema.final or rschema.inlined:
   273         if rschema_has_table(rschema, skip_relations=()):  # XXX skip_relations should be specified
   272             continue
   274             w(grant_rschema(rschema, user, set_owner))
   273         w(grant_rschema(rschema, user, set_owner))
       
   274     return '\n'.join(output)
   275     return '\n'.join(output)
   275 
   276 
   276 
   277 
   277 def grant_eschema(eschema, user, set_owner=True, prefix=''):
   278 def grant_eschema(eschema, user, set_owner=True, prefix=''):
   278     output = []
   279     output = []