def do_upgrade():
    """ Implement your upgrades here  """
    #run_sql("DELETE FROM bwlOBJECT")
    #run_sql("DELETE FROM bwlOBJECTLOGGING")
    #run_sql("DELETE FROM bwlWORKFLOW")
    #run_sql("DELETE FROM bwlWORKFLOWLOGGING")
    #os.system("rm -Rf /home/lnielsen/envs/zenodonext/var/data/deposit/storage/*")

    logger = logging.getLogger('invenio_upgrader')
    from invenio.webdeposit_models import Deposition, DepositionDraft, \
        DepositionType, DepositionFile
    from invenio.webdeposit_storage import DepositionStorage
    from invenio.webdeposit_load_forms import forms
    from invenio.bibworkflow_config import CFG_OBJECT_VERSION, CFG_WORKFLOW_STATUS

    default_type = DepositionType.get_default()


    for row in run_sql("SELECT publicationid, projectid, uid, id_bibrec FROM eupublication"):
        try:
            logger.info("Migrating publication id %s from user %s..." % (row[0], row[2]))
            old = OldDeposition(*row)
            dep = Deposition(None, user_id=old.user_id)

            # Add files
            backend = DepositionStorage(dep.id)
            for f in old.get_files().values():
                 fp = open(f, 'r')
                 df = DepositionFile(backend=backend)
                 df.save(fp, filename=os.path.basename(f))
                 fp.close()
                 dep.add_file(df)

            metadata = old.get_metadata()
            is_submitted = metadata['__status__'] == 'submitted'
            modified = datetime.fromtimestamp(metadata['__md__'])
            created = datetime.fromtimestamp(metadata['__cd__'])

            dep.workflow_object.created = created
            dep.workflow_object.modified = modified

            # Add draftg
            draft = dep.get_or_create_draft('_default', form_class=forms['ZenodoForm'])
            draft.validate = True

            m = map_metadata(metadata)
            recid = m.get('recid', None)
            draft.process(m, complete_form=True)

            if is_submitted:
                if not recid:
                    raise RuntimeError("Something is wrong")
                draft.completed = True
                dep.workflow_object.version = CFG_OBJECT_VERSION.FINAL

                sip = dep.create_sip()
                sip.metadata['recid'] = recid

                sip.metadata['fft'] = sip.metadata['files']
                del sip.metadata['files']

                sip.package = old.get_marcxml()
                sip.seal()

            dep.save()

            dep.workflow_object.created = created
            dep.workflow_object.modified = modified

            db.session.add(dep.workflow_object)
            db.session.commit()
        except Exception, e:
            import traceback
            warnings.warn("Failed to migrate %s (from user %s): %s" % (row[0], row[2], traceback.format_exc(e)))
def do_upgrade():
    """ Implement your upgrades here  """
    #run_sql("DELETE FROM bwlOBJECT")
    #run_sql("DELETE FROM bwlOBJECTLOGGING")
    #run_sql("DELETE FROM bwlWORKFLOW")
    #run_sql("DELETE FROM bwlWORKFLOWLOGGING")
    #os.system("rm -Rf /home/lnielsen/envs/zenodonext/var/data/deposit/storage/*")

    logger = logging.getLogger('invenio_upgrader')
    from invenio.webdeposit_models import Deposition, DepositionDraft, \
        DepositionType, DepositionFile
    from invenio.webdeposit_storage import DepositionStorage
    from invenio.webdeposit_load_forms import forms
    from invenio.bibworkflow_config import CFG_OBJECT_VERSION, CFG_WORKFLOW_STATUS

    default_type = DepositionType.get_default()


    for row in run_sql("SELECT publicationid, projectid, uid, id_bibrec FROM eupublication"):
        try:
            logger.info("Migrating publication id %s from user %s..." % (row[0], row[2]))
            old = OldDeposition(*row)
            dep = Deposition(None, user_id=old.user_id)

            # Add files
            backend = DepositionStorage(dep.id)
            for f in old.get_files().values():
                 fp = open(f, 'r')
                 df = DepositionFile(backend=backend)
                 df.save(fp, filename=os.path.basename(f))
                 fp.close()
                 dep.add_file(df)

            metadata = old.get_metadata()
            is_submitted = metadata['__status__'] == 'submitted'
            modified = datetime.fromtimestamp(metadata['__md__'])
            created = datetime.fromtimestamp(metadata['__cd__'])

            dep.workflow_object.created = created
            dep.workflow_object.modified = modified

            # Add draftg
            draft = dep.get_or_create_draft('_default', form_class=forms['ZenodoForm'])
            draft.validate = True

            m = map_metadata(metadata)
            recid = m.get('recid', None)
            draft.process(m, complete_form=True)

            if is_submitted:
                if not recid:
                    raise RuntimeError("Something is wrong")
                draft.completed = True
                dep.workflow_object.version = CFG_OBJECT_VERSION.FINAL

                sip = dep.create_sip()
                sip.metadata['recid'] = recid

                sip.metadata['fft'] = sip.metadata['files']
                del sip.metadata['files']

                sip.package = old.get_marcxml()
                sip.seal()

            dep.save()

            dep.workflow_object.created = created
            dep.workflow_object.modified = modified

            db.session.add(dep.workflow_object)
            db.session.commit()
        except Exception, e:
            import traceback
            warnings.warn("Failed to migrate %s (from user %s): %s" % (row[0], row[2], traceback.format_exc(e)))