Last active
October 30, 2017 15:15
-
-
Save zzacharo/abc66242bb08d8c0545bb2326a7d3841 to your computer and use it in GitHub Desktop.
update workflows to have `is-update` extra_data flag by default to False
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from invenio_db import db | |
from invenio_workflows import workflow_object_class | |
from invenio_workflows.models import ObjectStatus | |
from flask import current_app | |
from flask_sqlalchemy import SQLAlchemy | |
def update_workflow(): | |
query_ses = SQLAlchemy(current_app).session | |
start_time = time.time() | |
print 'Start of update' | |
for index, workflow_model in enumerate(query_ses.query(workflow_object_class.dbmodel).filter_by(status=ObjectStatus.COMPLETED).yield_per(100)): | |
batch_start_time = time.time() | |
workflow = workflow_object_class(model=workflow_model) | |
if not workflow.extra_data.get('is-update'): | |
workflow_model_to_be_modified = db.session.query(workflow_object_class.dbmodel).filter_by(id=workflow.id).one() | |
workflow_obj_to_be_modified = workflow_object_class(model=workflow_model_to_be_modified) | |
workflow_obj_to_be_modified.extra_data['is-update'] = False | |
workflow_obj_to_be_modified.save() | |
if index % 100 == 0: | |
print 'commit batch changes for {} records'.format(index) | |
db.session.commit() | |
batch_end_time = time.time() | |
print 'Elapsed batch time {}'.format(batch_end_time - batch_start_time) | |
print 'Elapsed overall time so far {}'.format(batch_end_time - start_time) | |
db.session.commit() | |
end_time = time.time() | |
print 'End of update' | |
print 'Elapsed overall time {}'.format(end_time - start_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works inside an
inspirehep
app context (e.g after calling inspirehep shell). It's meant to be used in https://github.com/inspirehep/inspire-next .