[migration] fix migration script for 3.0.0
authorDavid Douard <david.douard@logilab.fr>
Mon, 12 May 2014 12:38:24 +0200
changeset 1508 235c766b52ff
parent 1503 70da3e54219b
child 1512 1e65c501a8f6
[migration] fix migration script for 3.0.0 - most migration steps are managed by narval (4.0.0) - ensure conversion of log attribute into log_file does not consume too much memory
migration/3.0.0_Any.py
--- a/migration/3.0.0_Any.py	Wed Oct 23 16:09:14 2013 +0200
+++ b/migration/3.0.0_Any.py	Mon May 12 12:38:24 2014 +0200
@@ -3,74 +3,34 @@
 from cubicweb.server.session import Session
 
 schema.rebuild_infered_relations()
-# TestExecution now have a workflow
-rql('SET WF workflow_of TE, TE default_workflow WF WHERE P default_workflow WF, '
-    'P name "Plan", TE name "TestExecution"')
-commit()
 
 # transform log into log_files
-rset = rql('Any X, F WHERE X is IN (TestExecution, CheckResult), X log F')
 rename_relation_type('log_file','execution_archive')
-commit()
+add_relation_type('log_file')
 
-# for each TestExecution...
-te_eids = rql('DISTINCT Any X WHERE '
-    'X is TestExecution, '
-    'NOT TI wf_info_for X, '
-    'NOT X in_state S'
-    )
-
-# set it in correct initial state
-for (eid,) in te_eids:
-    session.entity_from_eid(eid).cw_adapt_to('IWorkflowable').set_initial_state('ready')
-
-commit()
 
-# fire all necessary transitions
-to_update=[]
-for (eid,) in te_eids:
-    te = session.entity_from_eid(eid)
-    if te.starttime:
-        tri = te.cw_adapt_to('IWorkflowable').fire_transition('start')
-        to_update.append((tri.eid, te.starttime))
-    if te.endtime:
-        if te.status == 'success':
-            tri = te.cw_adapt_to('IWorkflowable').fire_transition('end')
-            to_update.append((tri.eid, te.endtime))
-        elif te.status == 'killed':
-            tri = te.cw_adapt_to('IWorkflowable').fire_transition('kill')
-            to_update.append((tri.eid, te.endtime))
-        else:
-            tri = te.cw_adapt_to('IWorkflowable').fire_transition('fail')
-            to_update.append((tri.eid, te.endtime))
+eids = []
+if confirm('Upgrade all log attributes to File objects (if you say no here, execution logs will be lost)?'):
+    for i, (eid,) in enumerate(rql('Any X WHERE X is IN (TestExecution, CheckResult), NOT X log NULL')):
+        data = rql('Any F WHERE X log F, X eid %(eid)s', {'eid': eid})[0][0]
+        rql('INSERT File F: F data_name "log_file.txt", F data %(data)s, '
+            'F data_encoding "utf-8", F data_format "text/plain", '
+            'X log_file F WHERE X eid %(eid)s',
+            {'data': Binary(data.encode('utf-8')), 'eid': eid})
 
-# set correct timestamps
-for eid, date in to_update:
-    rql('SET TI creation_date %(date)s WHERE TI eid %(eid)s',
-        {'date': date, 'eid': eid})
-commit()
+        if not i%1000:
+            print i, "..."
+            commit(ask_confirm=False)
+            print "OK"
+    commit(ask_confirm=False)
 
-add_relation_type('log_file')
 drop_attribute('CheckResult', 'log')
 drop_attribute('TestExecution', 'log')
-drop_attribute('TestExecution', 'starttime')
-drop_attribute('TestExecution', 'endtime')
-drop_attribute('TestExecution', 'execution_status')
-drop_attribute('TestExecution', 'arguments')
+
 
 sync_schema_props_perms('CheckResult')
 sync_schema_props_perms('TestExecution')
 
-if confirm('Upgrade all log_files to file objects?'):
-    for xeid, fcontent in rset:
-        if fcontent is not None:
-            log_file = create_entity('File', data_name=u"log_file.txt",
-                                     data=Binary(fcontent.encode('utf-8')),
-                                     data_encoding='utf-8',
-                                     ask_confirm=False)
-            log_file.cw_set(reverse_log_file=xeid)
-    commit()
-
 from cubes.apycot import recipes
 r_script_names = {
         u'apycot.recipe.quick': recipes.quick_script,