migration/3.0.0_Any.py
author Julien Cristau <julien.cristau@logilab.fr>
Fri, 25 Apr 2014 15:55:10 +0200
changeset 1494 81e883113eef
parent 1478 0f8d3902bb11
child 1504 4d90e84490d1
permissions -rw-r--r--
[migration] a few simplifications and fixes - use rename_relation_type instead of deprecated rename_relation - look at Plan's default workflow, not any workflow - we don't need to log in as narval, managers should be able to fire transitions - avoid a few useless sync_schema_props_perms calls - drop TestExecution.arguments

# -*- coding: utf-8 -*-
from cubicweb import Binary
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()

# 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))

# 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()

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,
        u'apycot.recipe.full': recipes.full_script,
        u'apycot.recipe.scenario_runner': recipes.scenario_runner_script,
        }

warning_msg = """
### WARNING, THIS RECIPE SCRIPT WAS ADDED
### DURING A MIGRATION SCRIPT AND MIGHT
### NOT MATCH WHAT IS SHOWN IN THE LOGS
"""

for r_name in r_script_names:
    ## update script in recipe
    rql('SET X script %(script)s WHERE X is Recipe, X script "#to be updated", '
        'X name %(name)s',
        {'script': r_script_names[r_name],
         'name': r_name})
    ## update script for existing TestExecution and Chekresults
    rql('SET X script %(script)s WHERE X execution_of Y, Y name %(name)s',
        {'script': warning_msg + r_script_names[r_name],
         'name': r_name})