|
1 sync_schema_props_perms('defaultval') |
|
2 |
|
3 def convert_defaultval(cwattr, default): |
|
4 from decimal import Decimal |
|
5 import yams |
|
6 from cubicweb import Binary |
|
7 if default is None: |
|
8 return |
|
9 atype = cwattr.to_entity[0].name |
|
10 if atype == 'Boolean': |
|
11 assert default in ('True', 'False'), default |
|
12 default = default == 'True' |
|
13 elif atype in ('Int', 'BigInt'): |
|
14 default = int(default) |
|
15 elif atype == 'Float': |
|
16 default = float(default) |
|
17 elif atype == 'Decimal': |
|
18 default = Decimal(default) |
|
19 elif atype in ('Date', 'Datetime', 'TZDatetime', 'Time'): |
|
20 try: |
|
21 # handle NOW and TODAY, keep them stored as strings |
|
22 yams.KEYWORD_MAP[atype][default.upper()] |
|
23 default = default.upper() |
|
24 except KeyError: |
|
25 # otherwise get an actual date or datetime |
|
26 default = yams.DATE_FACTORY_MAP[atype](default) |
|
27 else: |
|
28 assert atype == 'String', atype |
|
29 default = unicode(default) |
|
30 return Binary.zpickle(default) |
|
31 |
|
32 dbh = repo.system_source.dbhelper |
|
33 driver = config.sources()['system']['db-driver'] |
|
34 if driver == 'postgres' or driver.startswith('sqlserver'): |
|
35 sql('ALTER TABLE cw_cwattribute ADD new_defaultval %s' % dbh.TYPE_MAPPING['Bytes']) |
|
36 else: |
|
37 assert False, 'upgrade not supported on this database backend' |
|
38 |
|
39 for cwattr in rql('CWAttribute X').entities(): |
|
40 olddefault = cwattr.defaultval |
|
41 if olddefault is not None: |
|
42 req = "UPDATE cw_cwattribute SET new_defaultval = %(val)s WHERE cw_eid = %(eid)s" |
|
43 args = {'val': dbh.binary_value(convert_defaultval(cwattr, olddefault).getvalue()), 'eid': cwattr.eid} |
|
44 sql(req, args, ask_confirm=False) |
|
45 |
|
46 sql('ALTER TABLE cw_cwattribute DROP COLUMN cw_defaultval') |
|
47 if config.sources()['system']['db-driver'] == 'postgres': |
|
48 sql('ALTER TABLE cw_cwattribute RENAME COLUMN new_defaultval TO cw_defaultval') |
|
49 else: |
|
50 sql("sp_rename 'cw_cwattribute.new_defaultval', 'cw_defaultval', 'COLUMN'") |
|
51 |
|
52 # Set object type to "Bytes" for CWAttribute's "defaultval" attribute |
|
53 rql('SET X to_entity B WHERE X is CWAttribute, X from_entity Y, Y name "CWAttribute", ' |
|
54 'X relation_type Z, Z name "defaultval", B name "Bytes"') |
|
55 |
|
56 from yams import buildobjs as ybo |
|
57 schema.add_relation_def(ybo.RelationDefinition('CWAttribute', 'defaultval', 'Bytes')) |
|
58 schema.del_relation_def('CWAttribute', 'defaultval', 'String') |
|
59 |
|
60 commit() |