cubicweb/misc/migration/3.23.0_Any.py
changeset 11360 49aca289134f
parent 11215 4e79acdc36a6
child 11363 e5fe836df6f1
--- a/cubicweb/misc/migration/3.23.0_Any.py	Mon Jun 20 17:59:43 2016 +0200
+++ b/cubicweb/misc/migration/3.23.0_Any.py	Wed Jun 22 07:57:13 2016 +0200
@@ -1,5 +1,42 @@
+
+from functools import partial
+
+from yams.constraints import UniqueConstraint
+
+from cubicweb.schema import PURE_VIRTUAL_RTYPES
+from cubicweb.server.schema2sql import build_index_name, check_constraint
+
+sql = partial(sql, ask_confirm=False)
+
+source = repo.system_source
+
+for rschema in schema.relations():
+    if rschema.rule or rschema in PURE_VIRTUAL_RTYPES:
+        continue
+    if rschema.final or rschema.inlined:
+        for rdef in rschema.rdefs.values():
+            table = 'cw_{0}'.format(rdef.subject)
+            column = 'cw_{0}'.format(rdef.rtype)
+            if any(isinstance(cstr, UniqueConstraint) for cstr in rdef.constraints):
+                old_name = '%s_%s_key' % (table.lower(), column.lower())
+                sql('ALTER TABLE %s DROP CONSTRAINT %s' % (table, old_name))
+                source.create_index(cnx, table, column, unique=True)
+            if rschema.inlined or rdef.indexed:
+                old_name = '%s_%s_idx' % (table.lower(), column.lower())
+                sql('DROP INDEX IF EXISTS %s' % old_name)
+                source.create_index(cnx, table, column)
+    else:
+        table = '{0}_relation'.format(rschema)
+        sql('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s_p_key' % (table, table))
+        sql('ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY(eid_from, eid_to)'
+            % (table, build_index_name(table, ['eid_from', 'eid_to'], 'key_')))
+        for column in ('from', 'to'):
+            sql('DROP INDEX IF EXISTS %s_%s_idx' % (table, column))
+            sql('CREATE INDEX %s ON %s(eid_%s);'
+                % (build_index_name(table, ['eid_' + column], 'idx_'), table, column))
+
+
 # we changed constraint serialization, which also changes their name
-from cubicweb.server.schema2sql import check_constraint
 
 helper = repo.system_source.dbhelper
 
@@ -22,3 +59,4 @@
     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s CHECK(%(v)s)' % args)
 
 commit()
+