cubicweb/misc/scripts/migration_helper.py
changeset 11185 40e74d6d7e99
child 11767 432f87a63057
equal deleted inserted replaced
11184:2a7b98d91736 11185:40e74d6d7e99
       
     1 # copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb 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
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 
       
    19 """Helper functions for migrations that aren't reliable enough or too dangerous
       
    20 to be available in the standard migration environment
       
    21 """
       
    22 from __future__ import print_function
       
    23 
       
    24 __docformat__ = "restructuredtext en"
       
    25 
       
    26 def drop_entity_types_fast(*etypes, **kwargs):
       
    27     """drop an entity type bypassing all hooks
       
    28 
       
    29     here be dragons.
       
    30     """
       
    31     # XXX cascade deletion through composite relations?
       
    32 
       
    33     for etype in etypes:
       
    34 
       
    35         if etype not in schema:
       
    36             print('%s does not exist' % etype)
       
    37             continue
       
    38         etype = schema[etype]
       
    39 
       
    40         # ignore attributes and inlined rels since they'll be dropped anyway
       
    41         srels = [x.type for x in etype.subject_relations() if x.eid and not (x.final or x.inlined)]
       
    42 
       
    43         orels = [x.type for x in etype.object_relations() if x.eid and not x.inlined]
       
    44         inlined_rels = [x for x in etype.object_relations() if x.eid and x.inlined]
       
    45 
       
    46         # eids to be deleted could be listed in some other entity tables through inlined relations
       
    47         for rtype in inlined_rels:
       
    48             for subjtype in rtype.subjects(etype):
       
    49                 if subjtype in etypes:
       
    50                     continue
       
    51                 sql('UPDATE cw_%(stype)s SET cw_%(rtype)s = NULL '
       
    52                     'WHERE cw_%(rtype)s IN (SELECT eid FROM entities WHERE type = %%s)' %
       
    53                     {'stype': subjtype.type, 'rtype': rtype.type},
       
    54                     (etype.type,))
       
    55 
       
    56         for rel in srels:
       
    57             if all(subj in etypes for subj in rel.subjects()) or all(obj in etypes for obj in rel.objects()):
       
    58                 sql('DELETE FROM %s_relation' % rel.type)
       
    59             else:
       
    60                 sql('DELETE FROM %s_relation WHERE eid_from IN (SELECT eid FROM entities WHERE type = %%s)' % rel.type, (etype.type,))
       
    61         for rel in orels:
       
    62             if all(subj in etypes for subj in rel.subjects()) or all(obj in etypes for obj in rel.objects()):
       
    63                 sql('DELETE FROM %s_relation' % rel.type)
       
    64             else:
       
    65                 sql('DELETE FROM %s_relation WHERE eid_to IN (SELECT eid FROM entities WHERE type = %%s)' % rel, (etype.type,))
       
    66 
       
    67         sql('DELETE FROM appears WHERE uid IN (SELECT eid FROM entities WHERE type = %s)', (etype.type,))
       
    68         sql('DELETE FROM cw_%s' % etype.type)
       
    69         sql('DELETE FROM entities WHERE type = %s', (etype.type,))
       
    70 
       
    71     for etype in etypes:
       
    72         drop_entity_type(etype, **kwargs)