[sql gen] Properly create UNIQUE index on non string column. Closes #13728492
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 16 Jun 2016 17:45:16 +0200
changeset 11289 3e69bccc2022
parent 11288 ab0a1863248e
child 11291 7c565548fb09
child 11293 63c589e6b076
child 11372 4482e94daabe
[sql gen] Properly create UNIQUE index on non string column. Closes #13728492 Unique was previously detected in a loop triggered only for String columns.
misc/migration/3.22.3_Any.py
server/schema2sql.py
server/test/data-schema2sql/schema/schema.py
server/test/unittest_schema2sql.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/migration/3.22.3_Any.py	Thu Jun 16 17:45:16 2016 +0200
@@ -0,0 +1,11 @@
+from yams.constraints import UniqueConstraint
+
+for rschema in schema.relations():
+    if rschema.rule or not rschema.final:
+        continue
+    for rdef in rschema.rdefs.values():
+        if (rdef.object != 'String'
+                and any(isinstance(cstr, UniqueConstraint) for cstr in rdef.constraints)):
+            table = 'cw_{0}'.format(rdef.subject)
+            column = 'cw_{0}'.format(rdef.rtype)
+            repo.system_source.create_index(cnx, table, column, unique=True)
--- a/server/schema2sql.py	Fri Jun 17 14:44:59 2016 +0200
+++ b/server/schema2sql.py	Thu Jun 16 17:45:16 2016 +0200
@@ -226,15 +226,15 @@
     """return a sql type string corresponding to the relation definition"""
     constraints = list(rdef.constraints)
     unique, sqltype = False, None
-    if rdef.object.type == 'String':
-        for constraint in constraints:
-            if isinstance(constraint, SizeConstraint):
-                if constraint.max is not None:
-                    size_constrained_string = dbhelper.TYPE_MAPPING.get(
-                        'SizeConstrainedString', 'varchar(%s)')
-                    sqltype = size_constrained_string % constraint.max
-            elif isinstance(constraint, UniqueConstraint):
-                unique = True
+    for constraint in constraints:
+        if isinstance(constraint, UniqueConstraint):
+            unique = True
+        elif (isinstance(constraint, SizeConstraint)
+              and rdef.object.type == 'String'
+              and constraint.max is not None):
+            size_constrained_string = dbhelper.TYPE_MAPPING.get(
+                'SizeConstrainedString', 'varchar(%s)')
+            sqltype = size_constrained_string % constraint.max
     if sqltype is None:
         sqltype = sql_type(dbhelper, rdef)
     if creating and unique:
--- a/server/test/data-schema2sql/schema/schema.py	Fri Jun 17 14:44:59 2016 +0200
+++ b/server/test/data-schema2sql/schema/schema.py	Thu Jun 16 17:45:16 2016 +0200
@@ -64,7 +64,7 @@
 class Societe(EntityType):
     nom  = String(maxsize=64, fulltextindexed=True)
     web = String(maxsize=128)
-    tel  = Int()
+    tel  = Int(unique=True)
     fax  = Int(constraints=[BoundaryConstraint('<=', Attribute('tel'))])
     rncs = String(maxsize=32)
     ad1  = String(maxsize=128)
--- a/server/test/unittest_schema2sql.py	Fri Jun 17 14:44:59 2016 +0200
+++ b/server/test/unittest_schema2sql.py	Thu Jun 16 17:45:16 2016 +0200
@@ -122,7 +122,7 @@
 CREATE TABLE Societe(
  nom varchar(64),
  web varchar(128),
- tel integer,
+ tel integer UNIQUE,
  fax integer,
  rncs varchar(32),
  ad1 varchar(128),