defaultval migration for sqlite
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 16 Sep 2013 11:58:40 +0200
changeset 9300 5f10cd13224d
parent 9299 c5eed908117d
child 9301 84ff860a966b
defaultval migration for sqlite
misc/migration/3.18.0_Any.py
--- a/misc/migration/3.18.0_Any.py	Fri Aug 23 15:06:27 2013 +0200
+++ b/misc/migration/3.18.0_Any.py	Mon Sep 16 11:58:40 2013 +0200
@@ -31,24 +31,47 @@
 
 dbh = repo.system_source.dbhelper
 driver = config.sources()['system']['db-driver']
+
 if driver == 'postgres' or driver.startswith('sqlserver'):
+
     sql('ALTER TABLE cw_cwattribute ADD new_defaultval %s' % dbh.TYPE_MAPPING['Bytes'])
+
+    for cwattr in rql('CWAttribute X').entities():
+        olddefault = cwattr.defaultval
+        if olddefault is not None:
+            req = "UPDATE cw_cwattribute SET new_defaultval = %(val)s WHERE cw_eid = %(eid)s"
+            args = {'val': dbh.binary_value(convert_defaultval(cwattr, olddefault).getvalue()), 'eid': cwattr.eid}
+            sql(req, args, ask_confirm=False)
+
+    sql('ALTER TABLE cw_cwattribute DROP COLUMN cw_defaultval')
+    if config.sources()['system']['db-driver'] == 'postgres':
+        sql('ALTER TABLE cw_cwattribute RENAME COLUMN new_defaultval TO cw_defaultval')
+    else:
+        sql("sp_rename 'cw_cwattribute.new_defaultval', 'cw_defaultval', 'COLUMN'")
+
+elif driver == 'sqlite':
+
+    import re
+    create = sql("SELECT sql FROM sqlite_master WHERE name = 'cw_CWAttribute'")[0][0]
+    create = re.sub('cw_defaultval varchar[^,]*,', 'cw_defaultval bytea,', create, re.I)
+    create = re.sub('cw_CWAttribute', 'tmp_cw_CWAttribute', create, re.I)
+    sql(create)
+    sql("INSERT INTO tmp_cw_CWAttribute SELECT * FROM cw_CWAttribute")
+    for cwattr in rql('CWAttribute X').entities():
+        olddefault = cwattr.defaultval
+        if olddefault is None:
+            continue
+        req = "UPDATE tmp_cw_CWAttribute SET cw_defaultval = %(val)s WHERE cw_eid = %(eid)s"
+        args = {'val': dbh.binary_value(convert_defaultval(cwattr, olddefault).getvalue()),
+                'eid': cwattr.eid}
+        sql(req, args, ask_confirm=False)
+
+    sql('DROP TABLE cw_CWAttribute')
+    sql('ALTER TABLE tmp_cw_CWAttribute RENAME TO cw_CWAttribute')
+
 else:
     assert False, 'upgrade not supported on this database backend'
 
-for cwattr in rql('CWAttribute X').entities():
-    olddefault = cwattr.defaultval
-    if olddefault is not None:
-        req = "UPDATE cw_cwattribute SET new_defaultval = %(val)s WHERE cw_eid = %(eid)s"
-        args = {'val': dbh.binary_value(convert_defaultval(cwattr, olddefault).getvalue()), 'eid': cwattr.eid}
-        sql(req, args, ask_confirm=False)
-
-sql('ALTER TABLE cw_cwattribute DROP COLUMN cw_defaultval')
-if config.sources()['system']['db-driver'] == 'postgres':
-    sql('ALTER TABLE cw_cwattribute RENAME COLUMN new_defaultval TO cw_defaultval')
-else:
-    sql("sp_rename 'cw_cwattribute.new_defaultval', 'cw_defaultval', 'COLUMN'")
-
 # Set object type to "Bytes" for CWAttribute's "defaultval" attribute
 rql('SET X to_entity B WHERE X is CWAttribute, X from_entity Y, Y name "CWAttribute", '
     'X relation_type Z, Z name "defaultval", B name "Bytes"')