# HG changeset patch # User Sylvain Thénault # Date 1466091916 -7200 # Node ID 3e69bccc202287f208126ab5e401887df3838fc6 # Parent ab0a1863248e2ccf2b3bbd59083b229447ba6d44 [sql gen] Properly create UNIQUE index on non string column. Closes #13728492 Unique was previously detected in a loop triggered only for String columns. diff -r ab0a1863248e -r 3e69bccc2022 misc/migration/3.22.3_Any.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) diff -r ab0a1863248e -r 3e69bccc2022 server/schema2sql.py --- 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: diff -r ab0a1863248e -r 3e69bccc2022 server/test/data-schema2sql/schema/schema.py --- 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) diff -r ab0a1863248e -r 3e69bccc2022 server/test/unittest_schema2sql.py --- 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),