misc/migration/3.21.0_Any.py
changeset 10484 7f15552b7463
parent 10483 c8dbb845b465
child 10490 76ab3c71aff2
equal deleted inserted replaced
10483:c8dbb845b465 10484:7f15552b7463
    26         print '%s references %d unknown entities, deleting' % (rschema, count)
    26         print '%s references %d unknown entities, deleting' % (rschema, count)
    27         sql('DELETE FROM %(r)s_relation '
    27         sql('DELETE FROM %(r)s_relation '
    28             'WHERE eid_from NOT IN (SELECT eid FROM entities) '
    28             'WHERE eid_from NOT IN (SELECT eid FROM entities) '
    29             'OR    eid_to   NOT IN (SELECT eid FROM entities)' % args)
    29             'OR    eid_to   NOT IN (SELECT eid FROM entities)' % args)
    30 
    30 
    31     # XXX drop if exists?
    31     sql('ALTER TABLE %(r)s_relation DROP CONSTRAINT IF EXISTS %(r)s_relation_eid_from_fkey' % args)
       
    32     sql('ALTER TABLE %(r)s_relation DROP CONSTRAINT IF EXISTS %(r)s_relation_eid_to_fkey' % args)
    32     sql('ALTER TABLE %(r)s_relation ADD CONSTRAINT %(r)s_relation_eid_from_fkey '
    33     sql('ALTER TABLE %(r)s_relation ADD CONSTRAINT %(r)s_relation_eid_from_fkey '
    33         'FOREIGN KEY (eid_from) REFERENCES entities (eid)' % args)
    34         'FOREIGN KEY (eid_from) REFERENCES entities (eid)' % args)
    34     sql('ALTER TABLE %(r)s_relation ADD CONSTRAINT %(r)s_relation_eid_to_fkey '
    35     sql('ALTER TABLE %(r)s_relation ADD CONSTRAINT %(r)s_relation_eid_to_fkey '
    35         'FOREIGN KEY (eid_to) REFERENCES entities (eid)' % args)
    36         'FOREIGN KEY (eid_to) REFERENCES entities (eid)' % args)
    36 
    37 
    45                 assert not '%(e)s.%(r)s references unknown entities, fix manually'
    46                 assert not '%(e)s.%(r)s references unknown entities, fix manually'
    46             print '%(e)s.%(r)s references unknown entities, deleting'
    47             print '%(e)s.%(r)s references unknown entities, deleting'
    47             sql('UPDATE cw_%(e)s SET cw_%(r)s = NULL WHERE '
    48             sql('UPDATE cw_%(e)s SET cw_%(r)s = NULL WHERE '
    48                 'cw_%(r)s NOT IN (SELECT eid FROM entities)' % args)
    49                 'cw_%(r)s NOT IN (SELECT eid FROM entities)' % args)
    49 
    50 
    50         # XXX if exists?
    51         sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT IF EXISTS %(c)s' % args)
    51         #sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT %(c)s' % args)
       
    52         sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s '
    52         sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s '
    53             'FOREIGN KEY (cw_%(r)s) references entities(eid)'
    53             'FOREIGN KEY (cw_%(r)s) references entities(eid)'
    54             % args)
    54             % args)
    55 
    55 
    56 
    56 
    57 def add_foreign_key_etype(eschema):
    57 def add_foreign_key_etype(eschema):
    58     args = {'e': eschema.type}
    58     args = {'e': eschema.type}
       
    59     sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT IF EXISTS cw_%(e)s_cw_eid_fkey' % args)
    59     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT cw_%(e)s_cw_eid_fkey '
    60     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT cw_%(e)s_cw_eid_fkey '
    60         'FOREIGN KEY (cw_eid) REFERENCES entities (eid)' % args)
    61         'FOREIGN KEY (cw_eid) REFERENCES entities (eid)' % args)
    61 
    62 
    62 
    63 
    63 add_foreign_keys()
    64 add_foreign_keys()
    95     cstr = rdef.constraint_by_eid(cwconstraint.eid)
    96     cstr = rdef.constraint_by_eid(cwconstraint.eid)
    96     if cstr.type() not in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
    97     if cstr.type() not in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
    97         continue
    98         continue
    98     cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
    99     cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
    99             cstr, helper, prefix='cw_')
   100             cstr, helper, prefix='cw_')
       
   101     sql('ALTER TABLE %s%s DROP CONSTRAINT IF EXISTS %s' % ('cw_', rdef.subject.type, cstrname))
   100     sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' % ('cw_', rdef.subject.type, cstrname, check))
   102     sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' % ('cw_', rdef.subject.type, cstrname, check))
   101 commit()
   103 commit()