863 attrs = ','.join([SQL_PREFIX + rschema.type |
863 attrs = ','.join([SQL_PREFIX + rschema.type |
864 for rschema in schema[newname].subject_relations() |
864 for rschema in schema[newname].subject_relations() |
865 if (rschema.final or rschema.inlined) |
865 if (rschema.final or rschema.inlined) |
866 and not rschema in PURE_VIRTUAL_RTYPES]) |
866 and not rschema in PURE_VIRTUAL_RTYPES]) |
867 self.sqlexec('INSERT INTO %s%s(%s) SELECT %s FROM %s%s' % ( |
867 self.sqlexec('INSERT INTO %s%s(%s) SELECT %s FROM %s%s' % ( |
868 SQL_PREFIX, newname, attrs, attrs, SQL_PREFIX, oldname)) |
868 SQL_PREFIX, newname, attrs, attrs, SQL_PREFIX, oldname), |
|
869 ask_confirm=False) |
869 # old entity type has not been added to the schema, can't gather it |
870 # old entity type has not been added to the schema, can't gather it |
870 new = schema.eschema(newname) |
871 new = schema.eschema(newname) |
871 oldeid = self.rqlexec('CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
872 oldeid = self.rqlexec('CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
872 ask_confirm=False)[0][0] |
873 ask_confirm=False)[0][0] |
873 # backport old type relations to new type |
874 # backport old type relations to new type |
874 # XXX workflows, other relations? |
875 # XXX workflows, other relations? |
875 self.rqlexec('SET X from_entity NET WHERE X from_entity OET, ' |
876 for r1, rr1 in [('from_entity', 'to_entity'), |
876 'NOT EXISTS(X2 from_entity NET, X relation_type XRT, X2 relation_type XRT, ' |
877 ('to_entity', 'from_entity')]: |
877 'X to_entity XTE, X2 to_entity XTE), ' |
878 self.rqlexec('SET X %(r1)s NET WHERE X %(r1)s OET, ' |
878 'OET eid %(o)s, NET eid %(n)s', |
879 'NOT EXISTS(X2 %(r1)s NET, X relation_type XRT, ' |
879 {'o': oldeid, 'n': new.eid}, ask_confirm=False) |
880 'X2 relation_type XRT, X %(rr1)s XTE, X2 %(rr1)s XTE), ' |
880 self.rqlexec('SET X to_entity NET WHERE X to_entity OET, ' |
881 'OET eid %%(o)s, NET eid %%(n)s' % locals(), |
881 'NOT EXISTS(X2 to_entity NET, X relation_type XRT, X2 relation_type XRT, ' |
882 {'o': oldeid, 'n': new.eid}, ask_confirm=False) |
882 'X from_entity XTE, X2 from_entity XTE), ' |
883 # backport is / is_instance_of relation to new type |
883 'OET eid %(o)s, NET eid %(n)s', |
884 for rtype in ('is', 'is_instance_of'): |
884 {'o': oldeid, 'n': new.eid}, ask_confirm=False) |
885 self.sqlexec('UPDATE %s_relation SET eid_to=%s WHERE eid_to=%s' |
|
886 % (rtype, new.eid, oldeid), ask_confirm=False) |
|
887 # delete relations using SQL to avoid relations content removal |
|
888 # triggered by schema synchronization hooks |
|
889 self.sqlexec('DELETE FROM cw_CWRelation ' |
|
890 'WHERE cw_from_entity=%(eid)s OR cw_to_entity=%(eid)s', |
|
891 {'eid': oldeid}, ask_confirm=False) |
885 # remove the old type: use rql to propagate deletion |
892 # remove the old type: use rql to propagate deletion |
886 self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
893 self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
887 ask_confirm=False) |
894 ask_confirm=False) |
888 else: |
895 else: |
889 self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s', |
896 self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s', |