[migration/3.21] idempotency fixes
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 02 Jul 2015 15:31:48 +0200
changeset 10484 7f15552b7463
parent 10483 c8dbb845b465
child 10485 9d2416a504d3
[migration/3.21] idempotency fixes Drop constraints before (re)creating them This is postgresql-specific syntax, sqlserver needs work.
misc/migration/3.21.0_Any.py
--- a/misc/migration/3.21.0_Any.py	Thu Jul 02 12:19:49 2015 +0200
+++ b/misc/migration/3.21.0_Any.py	Thu Jul 02 15:31:48 2015 +0200
@@ -28,7 +28,8 @@
             'WHERE eid_from NOT IN (SELECT eid FROM entities) '
             'OR    eid_to   NOT IN (SELECT eid FROM entities)' % args)
 
-    # XXX drop if exists?
+    sql('ALTER TABLE %(r)s_relation DROP CONSTRAINT IF EXISTS %(r)s_relation_eid_from_fkey' % args)
+    sql('ALTER TABLE %(r)s_relation DROP CONSTRAINT IF EXISTS %(r)s_relation_eid_to_fkey' % args)
     sql('ALTER TABLE %(r)s_relation ADD CONSTRAINT %(r)s_relation_eid_from_fkey '
         'FOREIGN KEY (eid_from) REFERENCES entities (eid)' % args)
     sql('ALTER TABLE %(r)s_relation ADD CONSTRAINT %(r)s_relation_eid_to_fkey '
@@ -47,8 +48,7 @@
             sql('UPDATE cw_%(e)s SET cw_%(r)s = NULL WHERE '
                 'cw_%(r)s NOT IN (SELECT eid FROM entities)' % args)
 
-        # XXX if exists?
-        #sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT %(c)s' % args)
+        sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT IF EXISTS %(c)s' % args)
         sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s '
             'FOREIGN KEY (cw_%(r)s) references entities(eid)'
             % args)
@@ -56,6 +56,7 @@
 
 def add_foreign_key_etype(eschema):
     args = {'e': eschema.type}
+    sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT IF EXISTS cw_%(e)s_cw_eid_fkey' % args)
     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT cw_%(e)s_cw_eid_fkey '
         'FOREIGN KEY (cw_eid) REFERENCES entities (eid)' % args)
 
@@ -97,5 +98,6 @@
         continue
     cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
             cstr, helper, prefix='cw_')
+    sql('ALTER TABLE %s%s DROP CONSTRAINT IF EXISTS %s' % ('cw_', rdef.subject.type, cstrname))
     sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' % ('cw_', rdef.subject.type, cstrname, check))
 commit()